Manoj
Manoj

Reputation: 2452

ElasticSearch nested bool queries

I'm using ElasticSearch 5.6. I have following JSON document.

    {
          "cards": [{
                        "tag_categories": [
                            {
                                "is_sensitive": true,
                                "category": "Users",
                                "tags": [
                                    {
                                        "is_selected": true,
                                        "name": "user1"
                                    },
                                    {
                                        "is_selected": true,
                                        "name": "user2"
                                    },
                                    {
                                        "is_selected": false,
                                        "name": "user3"
                                    }
                                ]
                            }
                        ],
                        "risk": "medium",
                        "position": 1,
                        "placement": 4,
                        "title": "test title",

                    }, ...]
       }

I want to return this document if all the given users names and corresponding is_selected value is true.

This is my query.

{
  "_source": {
    "excludes": ["cards.pages"]
  },
  "query": {
    "bool": {
      "must": [{
        "match": {
          "_all": "hello world"
    }
  }],
  "filter": [{
      "nested": {
        "path": "cards.tag_categories.tags",
        "query": {
          "bool": {
            "must": [
                {
                "bool": {
                  "must": [
                    {
                      "match": {
                        "cards.tag_categories.tags.name": "user2"
                      }
                    },
                    {
                      "match": {
                        "cards.tag_categories.tags.is_selected": true
                      }
                    }

                  ]
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "match": {
                        "cards.tag_categories.tags.name": "user1"
                      }
                    },
                    {
                      "match": {
                        "cards.tag_categories.tags.is_selected": true
                      }
                    }

                  ]
                }
              }

            ]
          }
        }
      }
    },
    {
      "term": {
        "name.keyword": "hello name"
      }
    },
    {
      "term": {
        "title.keyword": "hello title"
      }
    }

  ]
   }
  }
}

I have added two child bool queries to match each set of user name and is_selected values. Parent bool query will get the AND and return the document if true.

In the above example query the document should be returned as user1 and user2 match the condition. But it doesn't happen.

If I compare a single user with his is_selected value the document returns. eg: user1.

I will be thankful if anyone can show me where I've made the mistake.

Upvotes: 3

Views: 13840

Answers (1)

Manoj
Manoj

Reputation: 2452

I added separate nested blocks and it worked!

{
  "_source": {
  "excludes": ["cards.pages"]
 },
 "query": {
"bool": {
  "must": [{
    "match": {
      "_all": "hello world"
    }
  }],
  "filter": [
    {
      "nested": {
        "path": "cards.tag_categories.tags",
        "query": {
          "bool": {
            "must": [
                {
                  "term": {
                    "cards.tag_categories.tags.name.keyword": "user1"
                  }
                },
                {
                  "term": {
                    "cards.tag_categories.tags.is_selected": true
                  }
                }

            ]
          }
        }
      }
    },
    {
      "nested": {
        "path": "cards.tag_categories.tags",
        "query": {
          "bool": {
            "must": [
                {
                  "term": {
                    "cards.tag_categories.tags.name.keyword": "user2"
                  }
                },
                {
                  "term": {
                    "cards.tag_categories.tags.is_selected": true
                  }
                }

            ]
          }
        }
      }
    },

    {
      "term": {
        "name.keyword": "hello name"
      }
    },
    {
      "term": {
        "title.keyword": "hello title"
      }
    }

  ]
}

} }

Upvotes: 5

Related Questions