Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

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

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"
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...