Search code examples
elasticsearch

Boost by integer field in Elasticsearch


I'm new in Elasticsearch and I have an issue.

I have some documents in my Elasticsearch index representing products. Following this query, I'm trying to boost results if the products have stocks or not (prior to products with stock > 0) but I have products with stock > 0 in the first results

    {
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "should": [
                            {
                                "simple_query_string": {
                                    "query": "plaque",
                                    "analyzer": "french",
                                    "fields": [
                                        "label^100",
                                        "label.1term^200",
                                        "label.3term^150",
                                        "label_term_1^50",
                                        "label_term_2^50",
                                        "label_term_3^50",
                                        "brand_analyzed_french^200"
                                    ],
                                    "lenient": true,
                                    "boost": 100,
                                    "minimum_should_match": "100%"
                                }
                            },
                            {
                                "simple_query_string": {
                                    "query": "plaque~1",
                                    "analyzer": "french",
                                    "fields": [
                                        "label^100",
                                        "label.1term^200",
                                        "label.3term^150",
                                        "label_term_1^50",
                                        "label_term_2^50",
                                        "label_term_3^50",
                                        "brand_analyzed_french^200"
                                    ],
                                    "lenient": true,
                                    "minimum_should_match": "100%"
                                }
                            }
                        ]
                    }
                }
            ],
            "filter": [
                {
                    "bool": {
                        "should": {
                            "bool": {
                                "must_not": {
                                    "match": {
                                        "stock": 0
                                    }
                                }
                            }
                        },
                        "must": [
                            {
                                "term": {
                                    "store_uid": 1
                                }
                            }
                        ],
                        "must_not": [
                            {
                                "term": {
                                    "FDS_out_stock": true
                                }
                            },
                            {
                                "term": {
                                    "is_base": true
                                }
                            },
                            {
                                "term": {
                                    "is_replaced": true
                                }
                            },
                            {
                                "term": {
                                    "life_cycle": "NEW"
                                }
                            },
                            {
                                "term": {
                                    "life_cycle": "DRF"
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

Do you know why? Thanks


Solution

  • You can use function score query with field value factor option.

    GET /_search
    {
      "query": {
        "function_score": {
          "field_value_factor": {
            "field": "my-int",
            "factor": 1.2,
            "modifier": "sqrt",
            "missing": 1
          }
        }
      }
    }
    

    It will translate into the following formula for scoring:

    sqrt(1.2 * doc['my-int'].value)