0

I have an index in elasticsearch with documents that look like this:

 "hits": [
        {
            "_index": "my-index2",
            "_type": "my-type",
            "_id": "1",
            "_score": 1,
            "_source": {
                "entities": {
                    "persons": [
                        "Kobe Bryant",
                        "Michael Jordan"
                    ],
                    "dates": [
                        "Yesterday"
                    ],
                    "locations": [
                        "Munich",
                        "New York"
                    ]
                },
                "my_field": "Kobe Bryant was one of the best basketball players of all times. Not even Michael Jordan has ever scored 81 points in one game. Munich is really an awesome city, but New York is as well. Yesterday has been the hottest day of the year."
            }
        }

Is it possible to use the aggregate function to aggregate by fields in the entities object? I tried this and it didn't work

{
    "aggs" : {
        "avg_date" : {
            "avg" : {
                "script" : {
                    "source" : "doc.entities.dates"
                }
            }
        }
    }
}

The error said that my index doesn't have an entities field.

EDIT: With the following term aggregation query:

{
    "aggs" : {
        "dates" : {
            "terms" : { "field" : "entities.dates" }
        }
    }
}

I get an error saying

Fielddata is disabled on text fields by default. Set fielddata=true on [entities.dates] in order to load fielddata in memory by uninverting the inverted index.

I can set fielddata=true like the error says I should however the documentation warns against this because it uses a lot of heap space.Is there another way I can do this query?

EDIT 2: Solved this by setting all fields in entities to keywords in the index.

ninesalt
  • 4,054
  • 5
  • 35
  • 75

0 Answers0