弹性搜索中的嵌套聚合
Nested aggregations in elasticsearch
我正在尝试使用 elasticsearch 实现自动完成功能。我正在处理的索引结构是
"title": "blog post 1",
"body": "body of the blog post",
"category": "programming",
"locations": [
{"name": "united states"},
{"name": "new york"},
{"name": "venice"}
]
我正在尝试使用嵌套聚合。我使用的映射是这个
{
blog : {
mappings : {
tag : {
properties : {
body : {
type : string
},
category : {
type : string
},
locations : {
properties : {
name : {
type : string
}
}
},
title : {
type : string
}
}
}
}
}
}
应该根据locations.name
聚合结果的查询是
GET /blog/tag/_search
{
"size": 0,
"query": {
"match": {
"locations.name": "montreal"
}
},
"aggs": {
"aggregated_locations": {
"nested": {
"path": "locations"
},
"aggs": {
"filtered_locations": {
"terms": {
"fields": "locations.name"
}
}
}
}
}
}
目前我正在使用 elasticsearch 的 chrome 插件执行上述请求。上面的查询给出了一个错误,它很长但是如果有人提示我也可以 post 它。我想我在解释嵌套聚合的含义时可能是错误的,这意味着我的映射是错误的。但是我无法弄清楚问题出在哪里。
您的映射中缺少一些部分,您需要将位置字段设置为 nested
类型并且不分析内部字段以便能够根据您的期望进行聚合:
PUT blog
{
"mappings": {
"tag": {
"properties": {
"body": {
"type": "string"
},
"category": {
"type": "string"
},
"locations": {
"type": "nested",
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
}
}
},
"title": {
"type": "string"
}
}
}
}
}
然后 运行 以下查询过滤嵌套字段,并聚合嵌套字段:
GET blog/_search
{
"size": 0,
"query": {
"nested": {
"path": "locations",
"query": {
"match": {
"locations.name": "montreal"
}
}
}
},
"aggs": {
"aggregated_locations": {
"nested": {
"path": "locations"
},
"aggs": {
"filtered": {
"terms": {
"field": "locations.name"
}
}
}
}
}
}
可以在某种意义上复制/粘贴这些查询(https://www.elastic.co/guide/en/sense/current/installing.html)
我正在尝试使用 elasticsearch 实现自动完成功能。我正在处理的索引结构是
"title": "blog post 1",
"body": "body of the blog post",
"category": "programming",
"locations": [
{"name": "united states"},
{"name": "new york"},
{"name": "venice"}
]
我正在尝试使用嵌套聚合。我使用的映射是这个
{
blog : {
mappings : {
tag : {
properties : {
body : {
type : string
},
category : {
type : string
},
locations : {
properties : {
name : {
type : string
}
}
},
title : {
type : string
}
}
}
}
}
}
应该根据locations.name
聚合结果的查询是
GET /blog/tag/_search
{
"size": 0,
"query": {
"match": {
"locations.name": "montreal"
}
},
"aggs": {
"aggregated_locations": {
"nested": {
"path": "locations"
},
"aggs": {
"filtered_locations": {
"terms": {
"fields": "locations.name"
}
}
}
}
}
}
目前我正在使用 elasticsearch 的 chrome 插件执行上述请求。上面的查询给出了一个错误,它很长但是如果有人提示我也可以 post 它。我想我在解释嵌套聚合的含义时可能是错误的,这意味着我的映射是错误的。但是我无法弄清楚问题出在哪里。
您的映射中缺少一些部分,您需要将位置字段设置为 nested
类型并且不分析内部字段以便能够根据您的期望进行聚合:
PUT blog
{
"mappings": {
"tag": {
"properties": {
"body": {
"type": "string"
},
"category": {
"type": "string"
},
"locations": {
"type": "nested",
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
}
}
},
"title": {
"type": "string"
}
}
}
}
}
然后 运行 以下查询过滤嵌套字段,并聚合嵌套字段:
GET blog/_search
{
"size": 0,
"query": {
"nested": {
"path": "locations",
"query": {
"match": {
"locations.name": "montreal"
}
}
}
},
"aggs": {
"aggregated_locations": {
"nested": {
"path": "locations"
},
"aggs": {
"filtered": {
"terms": {
"field": "locations.name"
}
}
}
}
}
}
可以在某种意义上复制/粘贴这些查询(https://www.elastic.co/guide/en/sense/current/installing.html)