Eyal Ch
Eyal Ch

Reputation: 10066

elasticsearch complex query on nested object

i have a list of books, each book has nested tag:

    "hits": [
          {
            "_index": "",
            "_type": "",
            "_id": "",
            "_score": ,
            "_source": {
              "name": "book1",
              "tags": [
                {
                  "t": "tagA",
                  "w": 100
                },
                {
                  "t": "tagB",
                  "w": 0
                },                
              ],

              "active": true,
            }
          },
          {
            "_index": "",
            "_type": "",
            "_id": "",
            "_score": ,
            "_source": {
              "name": "book2",
              "tags": [
                {
                  "t": "tagA",
                  "w": 100
                },
                {
                  "t": "tagB",
                  "w": 0
                },                
              ],

              "active": true,
            }
          },
       {
            "_index": "",
            "_type": "",
            "_id": "",
            "_score": ,
            "_source": {
              "name": "book3",
              "tags": [
                {
                  "t": "tagC",
                  "w": 100
                },
                {
                  "t": "tagB",
                  "w": 0
                },                
              ],

              "active": false,
            }
          }]

first, i tried to get all 'active' books with a specific tag, this can get by this query:

GET /index/type/_search
{
  "query": {
    "bool": {
      "must_not": {"term" : { "active" : false}},
      "must":
      [
        {
        "nested": {
            "path": "tags", 
            "query": {
              "bool": {
                "must": [ 
                  {
                    "match": {
                      "tags.t": "tagB"
                    }
                  }
                ]
              }
            }
          }
        }
     ]
    }
  }
}

for the above, book1 and book2 returned.

but what i am trying to get now is become more complicated. i am trying to get 'active' books with a specific tag (tagB). but if 'tagC' is in book, then book can return also if it is not active.

so for this question, book1, book2, book3 will return.

how can i do this query in elasticsearch?

Upvotes: 1

Views: 55

Answers (1)

user3775217
user3775217

Reputation: 4803

Try this, a should clause for both conditions

{
    "query": {
        "bool": {

            "should": [
                {
                    "nested": {
                        "path": "tags",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "match": {
                                            "tags.t": "tagC"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "active": true
                                }
                            },
                            {
                                "nested": {
                                    "path": "tags",
                                    "query": {
                                        "bool": {
                                            "must": [
                                                {
                                                    "match": {
                                                        "tags.t": "tagB"
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

Upvotes: 1

Related Questions