如何在弹性搜索中聚合动态字段? [英] How to aggregate over dynamic fields in elasticsearch?
问题描述
我正在尝试通过弹性搜索来聚合动态字段(不同的文档)。文件如下:
I am trying to aggregate over dynamic fields (different for different documents) via elasticsearch. Documents are like following:
[{
"name": "galaxy note",
"price": 123,
"attributes": {
"type": "phone",
"weight": "140gm"
}
},{
"name": "shirt",
"price": 123,
"attributes": {
"type": "clothing",
"size": "m"
}
}]
正如您可以看到文档中的属性更改。我想要实现的是汇总这些属性的字段,如下所示:
As you can see attributes change across documents. What Im trying to achieve is to aggregate fields of these attributes, like so:
{
aggregations: {
types: {
buckets: [{key: 'phone', count: 123}, {key: 'clothing', count: 12}]
}
}
}
我正在尝试聚合弹性搜索的功能来实现这一点,但无法找到正确的方法。是否有可能通过聚合实现?或者我应该开始查看 facets ,
I am trying aggregation feature of elasticsearch to achieve this, but not able to find correct way. Is it possible to achieve via aggregation ? Or should I start looking in to facets, thought it seem to be depricated.
推荐答案
您必须将属性定义为嵌套在映射中并更改单个属性的布局值到固定布局 {key:DynamicKey,value:DynamicValue}
You have to define attributes as nested in your mapping and change the layout of the single attribute values to the fixed layout { 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"
}
}
}
}
}
}
}
您可能不像这样索引您的文章
You may than index your articles like this
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"}
]
}
毕竟,你可以通过嵌套的属性聚合
After all you are then able to aggregate over the nested attributes
GET /catalog/_search
{
"query":{
"match_all":{}
},
"aggs": {
"attributes": {
"nested": {
"path": "attributes"
},
"aggs": {
"key": {
"terms": {
"field": "attributes.key"
},
"aggs": {
"value": {
"terms": {
"field": "attributes.value"
}
}
}
}
}
}
}
}
然后以稍微不同的形式向您提供您所要求的信息
Which then gives you the information you requested in a slightly different form
[...]
"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
}
]
}
},
[...]
这篇关于如何在弹性搜索中聚合动态字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!