# How to search within the same nested object

**URL:** <https://discuss.elastic.co/t/how-to-search-within-the-same-nested-object/42503>\
**Category:** Elasticsearch\
**Created:** [February 23, 2016, 2:29pm UTC](https://discuss.elastic.co/t/how-to-search-within-the-same-nested-object/42503 "2016-02-23T14:29:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremias\_Wolff](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@Jeremias\_Wolff](https://discuss.elastic.co/u/Jeremias_Wolff)\
**Post date:** [February 23, 2016, 2:29pm UTC](https://discuss.elastic.co/t/how-to-search-within-the-same-nested-object/42503/1 "2016-02-23T14:29:25Z")

</div>

Hej Girls and Boys,

i've got a little problem by finding the correct search query.

we have the following structure:

```
{
    "id":150163,
    "message_id":"<1yh4hj.icedbugudhz5z7t>",
    "created_at":"2015-07-22 06:07:35",
    "updated_at":"2015-07-26 19:20:38",
    "subscriber_id":2956,
    "values":[
        {
            "id":12320400,
            "custom_attribute_id":22,
            "value":"news@example.org",
            "model_type":"EmailHeader",
            "model_id":150163,
            "created_at":"2015-07-22 05:04:13",
            "updated_at":"2015-07-22 05:04:13",
            "deleted_at":null,
            "custom_attribute":{
                "id":22,
                "name":"from_address",
                "displayname":"from_address",
                "type":"text",
                "default_value":"",
                "options":"",
                "created_at":"10.06.15 ?.. 05:23",
                "updated_at":"26.11.15 ?.. 10:28",
                "deleted_at":"23.02.16 ?.. 10:34",
                "group_id":1,
                "required":0,
                "position":0,
                "created_by":1,
                "updated_by":1
            }
        },
        {
            "id":12320401,
            "custom_attribute_id":23,
            "value":"foo@example.org",
            "model_type":"EmailHeader",
            "model_id":150163,
            "created_at":"2015-07-22 05:04:13",
            "updated_at":"2015-07-22 05:04:13",
            "deleted_at":null,
            "custom_attribute":{
                "id":23,
                "name":"to_address",
                "displayname":"to_address",
                "type":"text",
                "default_value":"",
                "options":"",
                "created_at":"10.06.15 ?.. 05:23",
                "updated_at":"26.11.15 ?.. 10:28",
                "deleted_at":"23.02.16 ?.. 10:34",
                "group_id":1,
                "required":0,
                "position":0,
                "created_by":1,
                "updated_by":1
            }
        }
    ]
}

```

and I have this search query:

```
curl -XGET "http://localhost:9200/_search?pretty&size=1" -d '{
     "query" : {
         "query_string" : {
         	"query": "(values.custom_attribute_id:22 AND values.value:\"foo@example.org\")"
         }
     }
 }'

```

but this shouldn't match, because the string "foo@example.org" matches agains values.custom\_attribute\_id **23** so this query should return no result.

---

<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:** [February 23, 2016, 2:48pm UTC](https://discuss.elastic.co/t/how-to-search-within-the-same-nested-object/42503/2 "2016-02-23T14:48:03Z")

</div>

It's because elastic flattens all the data for the same field.

In that case, you need to use Nested docs. See [https://www.elastic.co/guide/en/elasticsearch/guide/master/nested-objects.html](https://www.elastic.co/guide/en/elasticsearch/guide/master/nested-objects.html)

Note that you will have to use specific queries (Query DSL).

---

<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:** [July 5, 2017, 11:14pm UTC](https://discuss.elastic.co/t/how-to-search-within-the-same-nested-object/42503/3 "2017-07-05T23:14:13Z")

</div>


