Home > front end >  ElasticSearch - Search for value on nested object under any key
ElasticSearch - Search for value on nested object under any key

Time:12-14

I have documents indexed in ES with the following structure:

doc 1:

{
 "map": {
   "field1": ["foo"],
   "field2": ["bar"]
 }
}

doc2

{
 "map": {
   "fieldN": ["foo"],
 }
}

I need to search all the documents that match a specific value under any key in the "map" object. Since the fields in "map" are dynamic, the value can be found under any key.

I tried different queries but none of them seems to work since it looks like for all the cases, I need to specify the field explicitly (ex.: map.field1 = "foo")

I would hope to be able to do a search like this:

{ 
  "fields": ["map.*"], 
  "query": "foo" 
}

Any recommendations on how to approach this type of search?

CodePudding user response:

You can use multi-match query, to search for a query term on map.* fields

{
  "query": {
    "multi_match" : {
      "query":    "foo", 
      "fields": [ "map.*" ] 
    }
  }
}
  • Related