假設我在給定索引上有這 3 個檔案:
{
"product_title":"dog shampoo",
"properties":[
{
"type":"dog",
"category":"hygiene"
},
{
"type":"dog",
"category":"foo"
}
]
}
第二
{
"product_title":"cat food",
"properties":[
{
"type":"cat",
"category":"food"
},
{
"type":"cat",
"category":"foo"
}
]
}
第三
{
"product_title":"dog toy",
"properties":[
{
"type":"dog",
"category":"toys"
},
{
"type":"dog",
"category":"foo"
}
]
}
我想計算/匯總每個屬性 ['type'] 有多少產品。
我已經嘗試過使用aggregations:
"aggregations" : {
"dog" : {
"value" : 4
},
"cat" : {
"value" : 2
},
}
但是 properties['type'] 一直在變化,所以這必須動態完成。
uj5u.com熱心網友回復:
對于您的示例,您將需要使用nested聚合。假設您將有以下映射:
{
"mappings": {
"properties": {
"product_title": {
"type": "text"
},
"properties": {
"type": "nested",
"properties": {
"type": {
"type": "keyword"
},
"category": {
"type": "keyword"
}
}
}
}
}
}
因此,您首先需要使用nested聚合,然后使用 aterms來獲取您的存盤桶properties.type:
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"properties": {
"nested": {
"path": "properties"
},
"aggs": {
"by_type": {
"terms": {
"field": "properties.type"
}
}
}
}
}
}
這是結果:
"aggregations": {
"properties": {
"doc_count": 6,
"by_type": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "dog",
"doc_count": 4
},
{
"key": "cat",
"doc_count": 2
}
]
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/348846.html
標籤:弹性搜索
上一篇:需要一些關于Elasticsearch引擎和reactjs的幫助
下一篇:僅搜索父母(帶連接)
