![](/img/trans.png)
[英]How to aggregate by dynamic or unknown fields in Elasticsearch 6.x
[英]How to aggregate over dynamic fields in elasticsearch?
我試圖通過elasticsearch聚合動態字段(不同文檔不同)。 文件如下:
[{
"name": "galaxy note",
"price": 123,
"attributes": {
"type": "phone",
"weight": "140gm"
}
},{
"name": "shirt",
"price": 123,
"attributes": {
"type": "clothing",
"size": "m"
}
}]
如您所見,文檔中的屬性發生了變化。 我想要實現的是聚合這些屬性的字段,如下所示:
{
aggregations: {
types: {
buckets: [{key: 'phone', count: 123}, {key: 'clothing', count: 12}]
}
}
}
我正在嘗試使用elasticsearch的聚合功能來實現這一目標,但卻無法找到正確的方法。 是否有可能通過聚合實現? 或者我應該開始尋找方面 ,認為它似乎被剝奪了。
您必須將屬性定義為嵌套在映射中,並將單個屬性值的布局更改為固定布局{ key: DynamicKey, value: DynamicValue }
PUT /catalog
{
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"article": {
"properties": {
"name": {
"type" : "string",
"index" : "not_analyzed"
},
"price": {
"type" : "integer"
},
"attributes": {
"type": "nested",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
}
}
}
}
}
}
您可以像這樣索引您的文章
POST /catalog/article
{
"name": "shirt",
"price": 123,
"attributes": [
{ "key": "type", "value": "clothing"},
{ "key": "size", "value": "m"}
]
}
POST /catalog/article
{
"name": "galaxy note",
"price": 123,
"attributes": [
{ "key": "type", "value": "phone"},
{ "key": "weight", "value": "140gm"}
]
}
畢竟,您可以聚合嵌套屬性
GET /catalog/_search
{
"query":{
"match_all":{}
},
"aggs": {
"attributes": {
"nested": {
"path": "attributes"
},
"aggs": {
"key": {
"terms": {
"field": "attributes.key"
},
"aggs": {
"value": {
"terms": {
"field": "attributes.value"
}
}
}
}
}
}
}
}
然后,它會以稍微不同的形式為您提供所需的信息
[...]
"buckets": [
{
"key": "type",
"doc_count": 2,
"value": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "clothing",
"doc_count": 1
}, {
"key": "phone",
"doc_count": 1
}
]
}
},
[...]
不確定這是不是你的意思,但這對於基本的聚合功能來說相當簡單。 請注意,我沒有包含映射,所以使用多個單詞的類型會得到雙重結果。
POST /product/atype
{
"name": "galaxy note",
"price": 123,
"attributes": {
"type": "phone",
"weight": "140gm"
}
}
POST /product/atype
{
"name": "shirt",
"price": 123,
"attributes": {
"type": "clothing",
"size": "m"
}
}
GET /product/_search?search_type=count
{
"aggs": {
"byType": {
"terms": {
"field": "attributes.type",
"size": 10
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.