Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query string with boost fields in Elastic Search

I am using Query String with Boost Fields in Elastic Search 1.7. It is working fine but in some scenario, I am not getting expected result.
Query:

query
{
   "from": 0,
   "size": 10,
   "explain": true,
   "query": {
      "function_score": {
         "query": {
            "query_string": {
               "query": "account and data",
               "fields": [
                  "title^5"
                  "authors^4",
                  "year^5",
                  "topic^6"
               ],
               "default_operator": "and",
               "analyze_wildcard": true
            }
         },
         "score_mode": "sum",
         "boost_mode": "sum",
         "max_boost": 100
      }
   }
}

Sample Data :

{
   "took": 50,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 4,
      "max_score": 12.833213,
      "hits": [
         {
            "_id": "19850",
            "_score": 12.833213,
            "_source": {
               "ID": "19850",
               "Year": "2010",
               "Title": "account and data :..."
            }
         },
         {
            "_id": "16896",
            "_score": 11.867042,
            "_source": {
               "ID": "16896",
               "Year": "2014",
               "Title": "effectivness of data..."
            }
         },
         {
            "_id": "59862",
            "_score": 9.706333,
            "_source": {
               "ID": "59862",
               "Year": "2007",
               "Title": "best system to..."
            }
         },
         {
            "_id": "18501",
            "_score": 9.685843,
            "_source": {
               "ID": "18501",
               "Year": "2010",
               "Title": "management of..."
            }
         }
      ]
}

I am getting above sample data by using query and that is as per expectation. But now, If I increase weight of year to 100 then I expect 4th result at 3rd position and 3rd result at 4th position. I tried many things but I don't know what I am doing wrong.

like image 858
Jeeten Parmar Avatar asked Feb 14 '26 20:02

Jeeten Parmar


1 Answers

The boost is only used when the query matches the field you are boosting and it multiplies the score elastic search computes with the boosting you defined. In your query you are looking for "account and data" and that doesn't match any year so the boosting in the year will not be used.

Are you trying to take the year into account for ordering? If that is the case you can try adding the field_value_factor to your query like this:

"query" : {
    "function_score": {
        "query": { <your query goes here> },
        "field_value_factor": {
            "field": "year"
         }
    }
}

This will multiply the year with the score elastic search computes so it will take the year into account without necessary ordering by the year. You can read more about it here https://www.elastic.co/guide/en/elasticsearch/guide/current/boosting-by-popularity.html.

You can always use the explain tool to figure out how elastic search came up with the score and thus returned the results in that order. https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html

like image 183
Sofia Braun Avatar answered Feb 17 '26 14:02

Sofia Braun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!