Check if array contains object with a certain field in Elastic Search?

Viewed 2279

I am new to Elastic Search, and my data has the form:

Index Mapping

{
    "company:product:index": {
        "aliases": {},
        "mappings": {
            "company:product:mapping": {
                "properties": {
                    "attribute_heel-style": {
                        "properties": {
                            "label": {
                                "type": "keyword"
                            },
                            "name": {
                                "type": "keyword"
                            },
                            "source": {
                                "type": "keyword"
                            },
                            "value": {
                                "type": "keyword"
                            },
                            "value_label": {
                                "type": "keyword"
                            }
                        }
                    }
                }
            }
        }
    }
}

Index Data

{
    "attribute_heel-style": [
        {
            "name": "heel-style",
            "label": "Heel Style",
            "value": "stiletto",
            "value_label": "Stiletto"
        },
        {
            "name": "heel-style",
            "label": "Heel Style",
            "value": "wedge"
            "value_label": "Wedge"
        },
        ... // a bunch of other objects in this array as well
    ]
}

I'd like to create a query so that I can filter all objects within the array who have the value "Wedge" for the key "value_label". How can I do this?

Edit: Found the solution! Posting below. Thank you to @EsCoder.

{
    "query": {
        "bool": {
            "filter": [
                {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "attribute_heel-style.value_label": "wedge"
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}
1 Answers

Arrays of objects do not work as you would expect: you cannot query each object independently of the other objects in the array. If you need to be able to do this then you should use the nested data type instead of the object data type.

Refer to this ES official document on Arrays to get a detailed explanation.

Adding a working example with index data, mapping, search query, and search result

You have to reindex your data, after applying nested data type

Index Mapping:

{
  "mappings": {
    "properties": {
      "attribute_heel-style": {
        "type": "nested"
      }
    }
  }
}

Index Data:

{
  "attribute_heel-style": [
    {
      "name": "heel-style",
      "label": "Heel Style",
      "value": "stiletto",
      "value_label": "Stiletto"
    },
    {
      "name": "heel-style",
      "label": "Heel Style",
      "value": "wedge",
      "value_label": "Wedge"
    }
  ]
}

Search Query:

{
  "query": {
    "nested": {
      "path": "attribute_heel-style",
      "query": {
        "bool": {
          "filter": [
            {
              "match": {
                "attribute_heel-style.value_label": "Wedge"
              }
            }
          ]
        }
      },
    "inner_hits":{}
    }
  }
}

Search Result:

"inner_hits": {
          "attribute_heel-style": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": 0.0,
              "hits": [
                {
                  "_index": "65585632",
                  "_type": "_doc",
                  "_id": "1",
                  "_nested": {
                    "field": "attribute_heel-style",
                    "offset": 1
                  },
                  "_score": 0.0,
                  "_source": {
                    "name": "heel-style",
                    "label": "Heel Style",
                    "value": "wedge",
                    "value_label": "Wedge"     // note this
                  }
                }
              ]
            }
          }
        }
      }

Update 1:

{
  "mappings": {
    "company:product:mapping": {
      "properties": {
        "attribute_heel-style": {
          "type": "nested",             // note this
          "properties": {
            "label": {
              "type": "keyword"
            },
            "name": {
              "type": "keyword"
            },
            "source": {
              "type": "keyword"
            },
            "value": {
              "type": "keyword"
            },
            "value_label": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}
Related