1

For example, I have data containing the following:

{
  author: "test",
  books: [
    {
      name: "first book",
      cost: 50
    },
    {
      name: "second book",
      cost: 100
    }
  ]
}

I want to search the author which has ALL books with cost > 40. How would the query for that look like? The field books is mapped as nested property.

1 Answer 1

1

For author names with cost of one book greater than 40 (in hits), something as below in query would work

POST http://192.168.0.68:9200/library/Book/_search
{
   "fields": ["author"], 
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "nested": {
               "path": "books",
               "filter": {
                  "range": {
                     "books.cost": {
                        "gt": 40
                     }
                  }
               }
            }
         }
      }
   }
}

For all books having cost greater than 40, I had to handle the collection of nested field manually in client side after getting the response.

Not sure if script applies here to apply filter to all nested objects.

Reference

document not in nested documents elasticsearch

Sign up to request clarification or add additional context in comments.

1 Comment

Yep was using that query as well for ANY books greater than 40, which works well. However, ALL seem to be a harder issue. Client side filter would be my last resort though, if it is not at all possible in elastic search. Thanks!

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.