检查Elastic Search中数组是否包含具有特定字段的对象?

Dav*_*ham 2 search filter match elasticsearch

我是 Elastic Search 的新手,我的数据具有以下形式:

索引映射

{
    "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"
                            }
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

指数数据

{
    "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
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想创建一个查询,以便可以过滤数组中键“value_label”的值为“Wedge”的所有对象。我怎样才能做到这一点?

编辑:找到解决方案!下面发帖。感谢@EsCoder。

{
    "query": {
        "bool": {
            "filter": [
                {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "attribute_heel-style.value_label": "wedge"
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ESC*_*der 7

对象数组并不像您期望的那样工作:您无法独立于数组中的其他对象来查询每个对象。如果您需要能够执行此操作,那么您应该使用嵌套数据类型而不是对象数据类型。

详细解释请参考ES官方文档关于Arrays的内容。

添加包含索引数据、映射、搜索查询和搜索结果的工作示例

应用嵌套数据类型后,您必须重新索引数据

索引映射:

{
  "mappings": {
    "properties": {
      "attribute_heel-style": {
        "type": "nested"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

指数数据:

{
  "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"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

搜索查询:

{
  "query": {
    "nested": {
      "path": "attribute_heel-style",
      "query": {
        "bool": {
          "filter": [
            {
              "match": {
                "attribute_heel-style.value_label": "Wedge"
              }
            }
          ]
        }
      },
    "inner_hits":{}
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

搜索结果:

"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
                  }
                }
              ]
            }
          }
        }
      }
Run Code Online (Sandbox Code Playgroud)

更新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"
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)