Searching in nested objects without knowing the full path

Hi,
I have a document that looks like this:

"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?

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

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?

1 Like

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.

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

{ 
  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

What about:

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

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.

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