# Only return object from query

**URL:** <https://discuss.elastic.co/t/only-return-object-from-query/122230>\
**Category:** Elasticsearch\
**Created:** [March 2, 2018, 10:42am UTC](https://discuss.elastic.co/t/only-return-object-from-query/122230 "2018-03-02T10:42:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lh\_lw](https://avatars.discourse-cdn.com/v4/letter/l/9de053/32.png) [@lh\_lw](https://discuss.elastic.co/u/lh_lw)\
**Post date:** [March 2, 2018, 10:42am UTC](https://discuss.elastic.co/t/only-return-object-from-query/122230/1 "2018-03-02T10:42:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 2, 2018, 3:28pm UTC](https://discuss.elastic.co/t/only-return-object-from-query/122230/2 "2018-03-02T15:28:25Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html) field type for the `Book` field.  
Then you can use [inner\_hits](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/search-request-inner-hits.html).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 30, 2018, 3:28pm UTC](https://discuss.elastic.co/t/only-return-object-from-query/122230/3 "2018-03-30T15:28:33Z")

</div>

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