Search code examples
ruby-on-railsrubyruby-on-rails-3elasticsearchtire

Elasticsearch: How to get total score as sum of boost points?


I am sending query like this:(using query { all })

{
  "query": {
    "custom_filters_score": {
      "query": {
        "match_all": {}
      },
      "filters": [
        {
          "filter": {
            "range": {
              "last_contact_at": {
                "gte": "2013-06-21T12:02:25Z"
              }
            }
          },
          "boost": 1
        },
        {
          "filter": {
            "range": {
              "available_at": {
                "gte": "2013-06-28"
              }
            }
          },
          "boost": 2
        },
        {
          "filter": {
            "term": {
              "company_type": "intern"
            }
          },
          "boost": 10
        },
        {
          "filter": {
            "range": {
              "friends_count": {
                "from": 1
              }
            }
          },
          "boost": 3
        },
        {
          "filter": {
            "term": {
              "blacklisted": true
            }
          },
          "boost": -100
        },
        {
          "filter": {
            "term": {
              "focuses": "ruby php"
            }
          },
          "boost": 5
        },
        {
          "filter": {
            "term": {
              "tags": "ruby php"
            }
          },
          "boost": 4
        }
      ],
      "score_mode": "total"
    }
  }
}

and I get following response with max_score: 16 which is sum of my boost points.

Response

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 10,
      "max_score": 16,
...

And when I am sending the same custom_filters_score but with query { string term }

{
  "query": {
    "custom_filters_score": {
      "query": {
        "query_string": {
          "query": "ruby php"
        }
      },
      "filters": [
        {
          "filter": {
            "range": {
              "last_contact_at": {
                "gte": "2013-06-21T12:03:46Z"
              }
            }
          },
          "boost": 1
        },
        {
          "filter": {
            "range": {
              "available_at": {
                "gte": "2013-06-28"
              }
            }
          },
          "boost": 2
        },
        {
          "filter": {
            "term": {
              "company_type": "intern"
            }
          },
          "boost": 10
        },
        {
          "filter": {
            "range": {
              "friends_count": {
                "from": 1
              }
            }
          },
          "boost": 3
        },
        {
          "filter": {
            "term": {
              "blacklisted": true
            }
          },
          "boost": -100
        },
        {
          "filter": {
            "term": {
              "focuses": "ruby php"
            }
          },
          "boost": 5
        },
        {
          "filter": {
            "term": {
              "tags": "ruby php"
            }
          },
          "boost": 4
        }
      ],
      "score_mode": "total"
    }
  }
}

and I get following response with max_score: 8.990471

Response

{
   "took": 4,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 6,
      "max_score": 8.990471,
...

How to achieve max_score as sum of my boost points and also show only results which contains term ?

SearchService: https://gist.github.com/8f13ce2be820d9ef1959


Solution

  • From the es docs it looks like:

    1. The query is run and a set of hits generated
    2. The filters are run on the set of hits to give them a score.

    It looks like the document with the score of 16 (from the match_all query) doesnt match the "query_string":{"query":"ruby php"} in your second search, that is why the max_score is less.

    Also you say that 16, is the sum of all your boost points, but when I add up the score form the filters I get 25, or -75 if you include the blacklist...