# Searching in nested objects without knowing the full path

**URL:** <https://discuss.elastic.co/t/searching-in-nested-objects-without-knowing-the-full-path/116776>\
**Category:** Elasticsearch\
**Created:** [January 24, 2018, 12:36am UTC](https://discuss.elastic.co/t/searching-in-nested-objects-without-knowing-the-full-path/116776 "2018-01-24T00:36:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emilio\_Srougo](https://avatars.discourse-cdn.com/v4/letter/e/848f3c/32.png) [@Emilio\_Srougo](https://discuss.elastic.co/u/Emilio_Srougo)\
**Post date:** [January 24, 2018, 12:36am UTC](https://discuss.elastic.co/t/searching-in-nested-objects-without-knowing-the-full-path/116776/1 "2018-01-24T00:36:46Z")

</div>

Hi,  
I have a document that looks like this:

```auto
"name": {
      "use": "official",
      "family": "Donald",
      "given": [
        "Duck"
      ]
  }

```

And I'm making an API where the clients will send me queries like this:  
`?given=Duck` and also like this `?name=Duck`, That one should also look in name.family, so this: `?name=Donald` should also work.  
How can ElasticSearch help me with this?

---

<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:** [January 24, 2018, 9:12am UTC](https://discuss.elastic.co/t/searching-in-nested-objects-without-knowing-the-full-path/116776/2 "2018-01-24T09:12:15Z")

</div>

I'm unsure I understood the use case correctly but probably using `bool` with `should` clauses would work?

---

<div class="post-metadata">

**Author:** ![Emilio\_Srougo](https://avatars.discourse-cdn.com/v4/letter/e/848f3c/32.png) [@Emilio\_Srougo](https://discuss.elastic.co/u/Emilio_Srougo)\
**Post date:** [January 24, 2018, 4:27pm UTC](https://discuss.elastic.co/t/searching-in-nested-objects-without-knowing-the-full-path/116776/3 "2018-01-24T16:27:50Z")

</div>

Sorry @dadoonet , I didn't make my self clear enough.  
The problem is that I don't know the full path to the field that the client wants to search in.  
For example, the client could only tell me `given=eve`, while the real path is `name.given` so the question is, how to search in a field I don't know the full path to in advance.  
Another side of the question is the case where the client tells me `name=eve` or `name=smith` while the real path is `name.given` and `name.family`. So the question is now also, how to search in all the fields of a nested object?

---

<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:** [January 24, 2018, 5:37pm UTC](https://discuss.elastic.co/t/searching-in-nested-objects-without-knowing-the-full-path/116776/4 "2018-01-24T17:37:55Z")

</div>

By default, if you search with a query string query like `_search?q=foo` it will search for `foo` in every field.

You can have finer control by using the `copy_to` feature. It might help if I got correctly your use case.

---

<div class="post-metadata">

**Author:** ![Emilio\_Srougo](https://avatars.discourse-cdn.com/v4/letter/e/848f3c/32.png) [@Emilio\_Srougo](https://discuss.elastic.co/u/Emilio_Srougo)\
**Post date:** [January 24, 2018, 9:05pm UTC](https://discuss.elastic.co/t/searching-in-nested-objects-without-knowing-the-full-path/116776/5 "2018-01-24T21:05:14Z")

</div>

> [@dadoonet](#):
>
> if you search with a query string query like \_search?q=foo it will search for foo in every field.

@dadoonet  
Thanks, but what I need is to search only in the fields of the `name` object. Such as `given` and `family` in:

```auto
{ 
  type: "person",
  name: {
    given: "eve",
    family: "smith"
  },
  address: {...}
}

```

But ignore address and everything else.  
I need to achieve this without the need to set a `copy_to` mapping

---

<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:** [January 24, 2018, 9:21pm UTC](https://discuss.elastic.co/t/searching-in-nested-objects-without-knowing-the-full-path/116776/6 "2018-01-24T21:21:39Z")

</div>

What about:

```
GET /_search
{
    "query": {
        "query_string" : {
            "fields" : ["city.*"],
            "query" : "this AND that OR thus"
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![Surender\_Nandal](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@Surender\_Nandal](https://discuss.elastic.co/u/Surender_Nandal)\
**Post date:** [February 6, 2018, 1:11pm UTC](https://discuss.elastic.co/t/searching-in-nested-objects-without-knowing-the-full-path/116776/7 "2018-02-06T13:11:40Z")

</div>

Hello David,

What if we don't know the path (depth of intersted object) at all? For e.g. if a key i want to query on is available at different levels in an object.

---

<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 6, 2018, 1:11pm UTC](https://discuss.elastic.co/t/searching-in-nested-objects-without-knowing-the-full-path/116776/8 "2018-03-06T13:11:44Z")

</div>

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