Top_hits aggregation distinct nested documents


(Evaldas Buinauskas) #1

Is there a way to remove dupes from nested top_hits aggregation? My current query:

GET /analysis_elasticsearch/_search
{
  "size": 0,
  "aggs": {
    "Survey": {
      "nested": {
        "path": "Survey"
      },
      "aggs": {
        "TopHits": {
          "top_hits": {
            "size": 10,
            "_source": ["Survey.SurveyID", "Survey.SurveyName"], 
            "sort": "Survey.SurveyID"
          }
        }
      }
    }
  }
}

which returns following result:

{
  "took": 13,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1000,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "Survey": {
      "doc_count": 1000,
      "TopHits": {
        "hits": {
          "total": 1000,
          "max_score": null,
          "hits": [
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            },
            {
              "_nested": {
                "field": "Survey",
                "offset": 0
              },
              "_score": null,
              "_source": {
                "Survey": {
                  "SurveyID": "2",
                  "SurveyName": "Eating And Drinking"
                }
              },
              "sort": [
                2
              ]
            }
          ]
        }
      }
    }
  }
}

I'd like to keep only unique nested records. I know I could use terms aggregation to get unique SurveyID and then do terms aggregation once more to get their names, but this doesn't feel right.

Is there a way to get this done using top_hits agg?

Elasticsearch version: 5.1.1
I'm fine with using Painless to get desired result.


(system) #2

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