[英]Check if array contains object with a certain field in Elastic Search?
我是 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"
}
}
}
}
}
}
}
}
索引數據
{
"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
]
}
我想創建一個查詢,以便我可以過濾數組中鍵“value_label”的值為“Wedge”的所有對象。 我怎樣才能做到這一點?
編輯:找到解決方案。 下面發帖。 感謝@EsCoder。
{
"query": {
"bool": {
"filter": [
{
"bool": {
"must": [
{
"term": {
"attribute_heel-style.value_label": "wedge"
}
}
]
}
}
]
}
}
}
對象的 Arrays 無法按預期工作:您不能獨立於數組中的其他對象查詢每個 object。 如果您需要能夠做到這一點,那么您應該使用嵌套數據類型而不是 object 數據類型。
參考這篇關於Arrays的ES官方文檔得到詳細解釋。
添加具有索引數據、映射、搜索查詢和搜索結果的工作示例
應用嵌套數據類型后,您必須重新索引數據
索引映射:
{
"mappings": {
"properties": {
"attribute_heel-style": {
"type": "nested"
}
}
}
}
指數數據:
{
"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"
}
]
}
搜索查詢:
{
"query": {
"nested": {
"path": "attribute_heel-style",
"query": {
"bool": {
"filter": [
{
"match": {
"attribute_heel-style.value_label": "Wedge"
}
}
]
}
},
"inner_hits":{}
}
}
}
搜索結果:
"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
}
}
]
}
}
}
}
更新 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"
}
}
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.