0
votes

I have to make aggregation about actor's movies by tag

For exemple, I want to see all movie's tag for Robert Deni Jr. ex : action (10) blockbuster (4) romance (2)

If my user filter Robert Deni Jr. movies by a tag (action) filters need to be update. If I check romance then : action (1) blockbuster(2) romance (2)

This is my mapping :

  "mapping": {
    "actor": {
      "properties": {
        "id": {
          "type": "keyword"
        },
        "name": {
          "type": "text"
        },
        "movies": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "keyword"
            },
            "studio": {
              "type": "nested",
              "properties": {
                "id": {
                  "type": "keyword"
                },
                "label": {
                  "type": "text"
                }
              }
            },
            "tags": {
              "type": "nested",
              "properties": {
                "id": {
                  "type": "keyword"
                },
                "label": {
                  "type": "text"
                }
              }
            }
          }
        }
      }
    }
  }
}

And this is my query

{
  "query": {
    "bool": {
      "filter": [
        {
          "match": {
            "id": {
              "query": 587
            }
          }
        },
        {
          "bool": {
            "must": [
              {
                "nested": {
                  "path": "movies",
                  "query": {
                    "nested": {
                      "path": "movies.tags",
                      "query": {
                        "term": {
                          "movies.tags.id": {
                            "value": 5189,
                            "boost": 1
                          }
                        }
                      }
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  },
  "aggs": {
    "tags": {
      "nested": {
        "path": "movies.tags"
      },
      "aggs": {
        "tags": {
          "terms": {
            "field": "movies.tags.id",
            "size": 100
          }
        }
      }
    }
  }
}

My aggregation ignore the given tag. I can filter by a tag or not, the aggregation results don't change.

Any ideas ?

1

1 Answers

0
votes

Is there a reason you'd nest your tag query twice? First you say movies and then movie.tags which is probably why you're not getting the desired results. Try this one

{
    "query":{
        "bool":{
            "filter":[
                {
                    "match":{
                        "id":{
                            "query":587
                        }
                    }
                },
                {
                    "bool":{
                        "must":[
                            {
                                "nested":{
                                    "path":"movies",
                                    "query":{
                                        "term":{
                                            "movies.tags.id":{
                                                "value":5189,
                                                "boost":1
                                            }
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    },
    "aggs":{
        "tags":{
            "nested":{
                "path":"movies.tags"
            },
            "aggs":{
                "tags":{
                    "terms":{
                        "field":"movies.tags.id",
                        "size":100
                    }
                }
            }
        }
    }
}

If it doesn't work, please share a slice of your data.

Also, note that when you apply query restrictions, your aggregations will be affected too. If you instead want to aggregate across your whole index without the Robert Downey limitation (which is typically the case), you can use filtered aggregations.