1

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?

1 Answer 1

1

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"
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.