ElasticSearch - release with subterminal aggregation with array fields

I have the following two documents:

{  
"title":"The Avengers",
"year":2012,
"casting":[  
    {  
    "name":"Robert Downey Jr.",
    "category":"Actor",
    },
    {  
    "name":"Chris Evans",
    "category":"Actor",
    }
]
}

      

and

{  
"title":"The Judge",
"year":2014,
"casting":[  
    {  
    "name":"Robert Downey Jr.",
    "category":"Producer",
    },
    {  
    "name":"Robert Duvall",
    "category":"Actor",
    }
]
}

      

I would like to perform an aggregation based on two fields: casting.name and casting.category.

I tried using the Term Aggregation function based on the casting.name field using subaggregation, which is another conditional convention based on the casting.category field.

The problem is that for the "Chris Evans" record, ElasticSearch has set buckets for ALL categories (Actor, Producer), whereas it should only set 1 bucket (Actor).

There seems to be a Cartesian product between all casting.category events and all occurrences of casting.name. It behaves like this with array fields (casting), whereas I have no problem with simple fields (like title or year).

I also tried to use nested aggregates, but it may not be correct and ElasticSearch throws an error stating that casting.category is not a nested field.

Any idea here?

+3


source to share


1 answer


Elasticsearch will flatten the nested objects, so inside you get:

{  
"title":"The Judge",
"year":2014,
"casting.name": ["Robert Downey Jr.","Robert Duvall"],
"casting.category": ["Producer", "Actor"]
}

      

if you want to keep the relationship you will need to use nested objects or parent child relationships



To do nested mapping, you need to do something like this:

  "mappings": {
    "movies": {
      "properties": {
        "title" : { "type": "string" },
        "year" : { "type": "integer" },
        "casting": {
          "type": "nested", 
          "properties": {
            "name":    { "type": "string" },
            "category": { "type": "string" }
          }
        }
      }
    }
  }

      

+2


source







All Articles