Ordering bucket aggregations by rescore


(Brian Sawyer) #1

Hi all,

I'm trying to retrieve the top-n buckets of documents that share the same value in a field (terms agg), where the score for each bucket is the maximum scoring document according to a rescore function (order by max agg). I also get all (or most) of the documents from each bucket as well using the top_hits agg. This gives me everything I need from a single query.

Unfortunately the "_score" script used inside of the max agg does not include the score after the rescore query. Is that a bug in Elasticsearch? Can somebody think of a better way for me to approach this problem?

Thanks

Example query and results follow:

Query:

POST test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "general_field": "test"
          }
        },
        {
          "match": {
            "group_field": "a"
          }
        }
      ]
    }
  },
  "rescore": {
    "query": {
      "rescore_query": {
        "match": {
          "group_field": "b"
        }
      },
      "query_weight": 0.0
    }
  },
  "aggs": {
    "group_agg": {
      "terms": {
        "field": "group_field",
        "size": 10,
        "order": {
          "order_max_score": "desc"
        }
      },
      "aggs": {
        "order_max_score": {
          "max": {
            "script": "_score"
          }
        },
        "top_hits": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  },
  "size": 0
}

Results - Notice in the aggregations that due to the rescore query the results should be in the opposite order:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "a",
          "doc_count": 1,
          "order_max_score": {
            "value": 0.8754687309265137
          },
          "top_hits": {
            "hits": {
              "total": 1,
              "max_score": 1.4e-45,
              "hits": [
                {
                  "_index": "test",
                  "_type": "test",
                  "_id": "z1QnLmEB1I-r5xEoPg9n",
                  "_score": 0,
                  "_source": {
                    "group_field": "a",
                    "general_field": "test"
                  }
                }
              ]
            }
          }
        },
        {
          "key": "b",
          "doc_count": 1,
          "order_max_score": {
            "value": 0.18232156336307526
          },
          "top_hits": {
            "hits": {
              "total": 1,
              "max_score": 0.6931472,
              "hits": [
                {
                  "_index": "test",
                  "_type": "test",
                  "_id": "0FQnLmEB1I-r5xEoRg9r",
                  "_score": 0.6931472,
                  "_source": {
                    "group_field": "b",
                    "general_field": "test"
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.