Nested Aggregation Top Hits and sort

I have a nested structure at ES. Tags parameters is type equals nested. I want to sort by evaluationCount in tags and min price. after I wanna getting top_hits. How can I do this?

example data is here:

   {"price":185,"tags":[{"evaluationCount":46,"tag":6},{"evaluationCount":824,"tag":7}],"destinationCity":"IST",},
{"price":285,"tags":[{"evaluationCount":16,"tag":6},{"evaluationCount":864,"tag":7}],"destinationCity":"LON",}

My query

  {
    "size": 0,
    "sort": [
      {
        "price": "asc"
      },
      {
        "tags.evaluationCount": "desc"
      }
    ],
    "aggs": {
      "tags-nested": {
        "nested": {
          "path": "tags"
        },
        "aggs": {
          "inner": {
            "filter": {
              "terms": {
                "tags.tag": [
                  1,
                  2
                ]
              }
            },
            "aggs": {
              "tag_id_term": {
                "terms": {
                  "field": "tags.tag",
                  "order": {
                    "_count": "desc"
                  }
                },
                "aggs": {
                  "by_top_hit": {
                    "top_hits": {
                      "size": 1
                    }
                  }
                }
              },
              "events.name_count": {
                "max": {
                  "field": "tags.evaluationCount"
                }
              }
            }
          }
        }
      }
    }
  }

Response

  "aggregations": {
    "tags-nested": {
      "doc_count": 120,
      "inner": {
        "doc_count": 8,
        "events.name_count": {
          "value": 1199965
        },
        "tag_id_term": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": 2,
              "doc_count": 6,
              "by_top_hit": {
                "hits": {
                  "total": 6,
                  "max_score": 1,
                  "hits": [
                    {
                      "_nested": {
                        "field": "tags",
                        "offset": 1
                      },
                      "_score": 1,
                      "_source": {
                        "evaluationCount": 2849,
                        "tag": 2
                      }
                    }
                  ]
                }
              }
            },
            {
              "key": 1,
              "doc_count": 2,
              "by_top_hit": {
                "hits": {
                  "total": 2,
                  "max_score": 1,
                  "hits": [
                    {
                      "_nested": {
                        "field": "tags",
                        "offset": 0
                      },
                      "_score": 1,
                      "_source": {
                        "evaluationCount": 564,
                        "tag": 1
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  }

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