Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 671
Manoj Avatar asked Dec 08 '25 08:12

Manoj


1 Answers

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"
      }
    }

  ]
}

} }

like image 104
Manoj Avatar answered Dec 12 '25 08:12

Manoj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!