Only return object from query

I want Elasticsearch to return only the matched object if I index a document containing an array.

For Example:

PUT /testindex/books/1
{
    "Book" : [
      {
        "id": 12,
        "title" : "First book",
        "author" : [
            {
                "firstname" : "Frank",
                "surname": "Smith",
                "id": 3
            },
            {
                "firstname" : "Kate",
                "surname" : "Walker",
                "id" : 12
            }
        ]
      },
    {
        "id" : 13,
        "title" : "Second book",
        "author" : [
            {
                "firstname" : "Frank",
                "surname": "Smith",
                "id": 3
            }
        ]
      }
      ]
}

If I now search by firstname like that:

GET /testindex/books/_search
{
  "_source": "Book.author", 
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "Book.author.firstname": "Frank"
          }
        }
      ]
    }
  }
}

I would expect to only get the matching author objects returned. Instead, I get the whole document returned.

Why not indexing different documents instead? That'd be much easier.

Anyway, to do what you want with this model, you need to use nested field type for the Book field.
Then you can use inner_hits.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.