3

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.

sehe
  • 374,641
  • 47
  • 450
  • 633
Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111

1 Answers1

4

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

Sofia Braun
  • 194
  • 11
  • Do I need to add all fields whichever I want to take into account for ordering ? – Jeeten Parmar Jul 11 '17 at 08:05
  • You need to add the fields you want to take into account for ordering. but the `field_value_factor` `field` attribute takes only one field. If you want to make it a combination of two fields and add custom logic you should use instead the `script_score` as is mentioned here https://www.elastic.co/guide/en/elasticsearch/guide/current/function-score-query.html – Sofia Braun Jul 11 '17 at 10:21