0

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!

Aditi Ananya
  • 73
  • 1
  • 1
  • 7
  • It's a [known shortcoming](https://github.com/mobz/elasticsearch-head/issues/53) of the head plugin, it doesn't mean your query is wrong. Now maybe there's a way using [`inner_hits`](https://www.elastic.co/guide/en/elasticsearch/reference/1.6/search-request-inner-hits.html) – Val Jun 11 '15 at 11:01
  • @Val Thankyou so much.It worked. :) the nested inner document that is actually matched is returned along with the first document. But there is just one question, is there any way i can return just the matched nested inner document and nothing else? But yes, thanx a lot again. – Aditi Ananya Jun 12 '15 at 06:14

1 Answers1

0

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.

Val
  • 207,596
  • 13
  • 358
  • 360
  • I tried this out . I used "_source":["books"] at the beginning of my query. But,this also returns exactly the same result as it returned when i used only the "inner_hits" i.e the matched nested document along with the first document. – Aditi Ananya Jun 12 '15 at 12:35
  • Thankyou so much !! Now, this works pretty fine :) Only the nested matching document is being returned.. Thanks a lot Val !!! :) – Aditi Ananya Jun 15 '15 at 05:05