# Querying Nested Object returns all objects

**URL:** <https://discuss.elastic.co/t/querying-nested-object-returns-all-objects/176632>\
**Category:** Elasticsearch\
**Created:** [April 12, 2019, 1:23pm UTC](https://discuss.elastic.co/t/querying-nested-object-returns-all-objects/176632 "2019-04-12T13:23:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhaumik55231](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@bhaumik55231](https://discuss.elastic.co/u/bhaumik55231)\
**Post date:** [April 12, 2019, 1:23pm UTC](https://discuss.elastic.co/t/querying-nested-object-returns-all-objects/176632/1 "2019-04-12T13:23:18Z")

</div>

I was testing the example given on [elasticsearch nested object documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html). In that example, why it returns both records even though i'm querying for values that only matches with 1 record. Please see below: -

**Mapping: -**

```
PUT my_index
{
  "mappings": {
    "_doc":{
      "properties": {
        "user": {
          "type": "nested" 
        }
      }
    }
  }
}

PUT my_index/_doc/1
{
 "group" : "fans",
  "user" : [
    {
      "first" : "John",
      "last" : "Smith"
    },
    {
      "first" : "Alice",
      "last" : "White"
    }
  ]
}
```

**Query: -**

```
GET my_index/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            { "match": { "user.first": "John" }},
            { "match": { "user.last": "Smith" }}
          ]
        }
      }
    }
  }
}
```

**Actual Output: -**

```
"hits": {
    "total": 1,
    "max_score": 1.3862944,
    "hits": [
      {
        "_index": "my_index",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.3862944,
        "_source": {
          "group": "fans",
          "user": [
            {
              "first": "John",
              "last": "Smith"
            },
            {
              "first": "Alice",
              "last": "White"
            }
          ]
        }
      }
    ]
  }
}
```

**Desired Output:-**

```
"hits": {
    "total": 1,
    "max_score": 1.3862944,
    "hits": [
      {
        "_index": "my_index",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.3862944,
        "_source": {
          "group": "fans",
          "user": [
            {
              "first": "John",
              "last": "Smith"
            }
          ]
        }
      }
    ]
  }
}
```

If something is wrong with my query, then what would be the correct query to get the desired output shown above?

Thank You,  
Bhaumik

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 12, 2019, 5:46pm UTC](https://discuss.elastic.co/t/querying-nested-object-returns-all-objects/176632/2 "2019-04-12T17:46:58Z")

</div>

Hey,

first, please take the time to properly format your code, as you can use markdown. This makes your post a million times more readable and thus also more likely to get an answer. Thanks!

Regarding your problem, you should take a look at the [inner\_hits functionality](https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-request-inner-hits.html)

--Alex

---

<div class="post-metadata">

**Author:** ![bhaumik55231](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@bhaumik55231](https://discuss.elastic.co/u/bhaumik55231)\
**Post date:** [April 12, 2019, 6:22pm UTC](https://discuss.elastic.co/t/querying-nested-object-returns-all-objects/176632/3 "2019-04-12T18:22:33Z")

</div>

Thank you Alexandar and sorry about the code format, I have updated it.

---

<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:** [May 10, 2019, 6:22pm UTC](https://discuss.elastic.co/t/querying-nested-object-returns-all-objects/176632/4 "2019-05-10T18:22:43Z")

</div>

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