简体   繁体   中英

Elasticsearch aggregations on array

I have an array:

people: ["Darrell","Karen","Gary"]

Indexed as such:

indexes :people, type: 'string', include_in_all: false

I want to be able to search aggregations on the individual items in this array, but with this structure, a search for "Darrel" will also return "Karen" and "Gary" in the bucket results. And a search for "Karen" will not return any of the items in the array in the results.

There are some suggestions here http://coderify.com/aggregates-array-field-and-autocomplete-funcionality-in-elasticsearch/ , but I'm not able to change my structure in this way. Any other suggestions?

You need to use nested aggregation and filter aggregation to achieve this.

But before that , you might need to remodel your data as following

{
  "people": [
    {
      "name": "Darrell"
    },
    {
      "name": "Karen"
    },
    {
      "name": "Gary"
    }
  ]
}

Next make the field people as nested in the mapping. Now each people inner object would be treated as a inner document.

Now use the following aggregation to get what you need -

{
  "aggs": {
    "innerDOcuments": {
      "nested": {
        "path": "people"
      },
      "aggs": {
        "filterPeople": {
          "filter": {
            "name": "karen"
          },
          "aggs": {
            "peoples": {
              "terms": {
                "field": "name"
              }
            }
          }
        }
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM