Tzoiker
Tzoiker

Reputation: 1412

Elasticsearch aggregation using top_hits field with script ordering

I have a set of documents with src, txt and flt fields. I want to query by txt field in the following way:

  1. Group (bucketize) by src;
  2. In each bucket calculate top 1 most relevant document;
  3. Order each bucket by the _score * doc.flt value.

So far I have implemented 1 and 2, but not 3. Even if 3 may be not very efficient, I still want to have such an option. My query looks like:

{
    "query" : {
        'match' : {
            'text' : {
                'query' : <some text>,
                'fuzziness' : 'AUTO',
                'operator' : 'and'
            }
        }
    },
    "aggs": {
        "by_src": {
            "terms": {
                "field": "src",
                "size" : 10,
                "order" : {"top_score" : "desc"}
            },
            "aggs": {
                "top_hits" : {
                    "top_hits" : {
                        "sort": { "_score": {  "order": "desc" } },
                        "size" : 1
                    }
                },
                "top_score": {
                    "max" : {
                        "script" : "_score",
                    }
                }
            }
        }
    }
}

Upvotes: 0

Views: 1239

Answers (1)

groo
groo

Reputation: 4448

I believe it's failing because you don't need to use _source field to apply the sort to each bucket, just apply the sort by the field name:

{
  "query" : {
    'match' : {
        'text' : {
            'query' : <some text>,
            'fuzziness' : 'AUTO',
            'operator' : 'and'
        }
    }
},
"aggs": {
    "by_src": {
        "terms": {
            "field": "src",
            "size" : 10,
            "order" : {"top_score" : "desc"}
        },
        "aggs": {
            "top_hits" : {
                "top_hits" : {
                    "sort":[{
                        "flt": {"order": "desc"}
                    }],
                    "size" : 1
                }
            },
            "top_score": {
                "max" : {
                    "script" : "_score",
                }
            }
        }
    }
  }
}

I am assuming your document has a field called flt that you want to use to sort. Naturally you can also change the sorting to asc if it's what you need.

Upvotes: 1

Related Questions