假設我在 Elasticsearch Index 中有一個檔案,如下所示
{
"data": [
{
"color": "RED",
"qty": 3
},
{
"color": "BLACK",
"qty": 1
}, {
"color": "BLUE",
"qty": 0
}
]
}
我只需要黑色。有什么方法可以像下面這樣獲取足夠的資料。
{
"data": [
{
"color": "BLACK",
"qty": 1
}
]
}
uj5u.com熱心網友回復:
您可以使用腳本欄位從陣列中生成僅具有特定值的新欄位。以下是示例查詢:
{
"_source": {
"excludes": "data"
},
"query": {
"match_all": {}
},
"script_fields": {
"address": {
"script": {
"lang": "painless",
"source": """
List li = new ArrayList();
if(params['_source']['data'] != null)
{
for(p in params['_source']['data'])
{
if( p.color == 'BLACK')
li.add(p);
}
}
return li;
"""
}
}
}
}
回復:
"hits" : [
{
"_index" : "sample1",
"_type" : "_doc",
"_id" : "tUc6338BMCbs63yKTqj_",
"_score" : 1.0,
"_source" : { },
"fields" : {
"address" : [
{
"color" : "BLACK",
"qty" : 1
}
]
}
}
]
uj5u.com熱心網友回復:
如果任何欄位有任何匹配,Elasticsearch 將回傳整個檔案。如果要查找匹配的嵌套檔案,可以將data陣列嵌套在索引映射中,撰寫一個嵌套查詢data.color,按其值過濾,在您的情況下為黑色,然后使用inner_hits查找匹配的嵌套檔案。
您可以使用源過濾來僅檢索您想要的欄位,但它只會按欄位過濾,而不是按欄位值過濾。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/453959.html
標籤:弹性搜索
