How to display "ALL" the nested documents in an object in separate rows from elasticsearch?

338 views Asked by At

I have a nested object in the following form:

{
  "name": "Multi G. Enre",
  "books": [
    {
      "name": "Guns and lasers",
      "genre": "scifi",
      "publisher": "orbit"
    },
    {
      "name": "Dead in the night",
      "genre": "thriller",
      "publisher": "penguin"
    }
  ]
}

I tried the following JSON query for the above document:

{
 "query": {
  "filtered": {
  "query": {
    "match_all": {}
  },
  "filter": {
    "nested": {
      "path": "books",
      "query": {
        "filtered": {
          "query": {
            "match_all": {}
          },
          "filter": {
            "and": [
              {
                "term": {
                  "books.publisher": "penguin"
                }
              },
              {
                "term": {
                  "books.genre": "thriller"
                }
               }
             ]
            }
          }
        }
      }
    }
   }
  }
} 

So,I would like to see the second nested document i.e. "Dead in the night" as the result but, for anything I search only the first document i.e. "Guns and lasers" is displayed in the table in elasticsearch head plugin. So, is there any way I can display the nested documents separately based on the search query and not just the first document? I'm new to elasticsearch,so would appreciate any type of responses. THANKYOU!

1

There are 1 answers

2
Val On BEST ANSWER

You need to use inner_hits in your query.

Moreover, if you want to only retrieve the matching nested document and nothing else, you can add "_source":["books"] to your query and only the matching nested books will be returned, nothing else.

UPDATE

Sorry, I misunderstood your comment. You can add "_source": false and the top-level document will not be returned. Only the nested matching document.