我在 ElasticSearch 5.6.3
上聚合嵌套文档时遇到问题。
我的查询结构如下:
query
aggs
|_filter
|_nested
|_term
|_top-hits
如果我尝试在非嵌套字段上进行聚合(当然还删除了嵌套聚合),一切都会按预期进行。但是按照现在的结构,我收到了来自 Lucene 的异常:
子查询不能与父过滤器匹配相同的文档。将它们组合为 must 子句 (+) 以查找问题文档。 docId=2147483647,类 org.apache.lucene.search.ConstantScoreScorer
此异常不会在 ElasticSearch 2.4.6
上触发。
我尝试以不同的方式构建聚合,但我无法想出一个有效的组合并提供所需的结果。
映射是这样的:
"recording": {
"dynamic": "strict",
"_all" : {
"enabled" : false
},
"properties": {
"id": {
"type": "integer"
},
"soloists": {
"properties": {
"type": "nested",
"person": {
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string",
"index": "not_analyzed"
}
}
}
},
"work": {
"id": {
"type": integer
},
"title": {
"type": "string",
"index": "not_analyzed"
}
}
}
查询本身:
{
"query": {},
"aggs": {
"my_top_results": {
"global": {},
"aggs": {
"my_filter_agg": {
"filter": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"nested": {
"path": "soloists",
"query": {
"bool": {
"must": {
"match": {
"soloists.person.id": 77957
}
}
}
}
}
}
]
}
}
]
}
},
"aggs": {
"my_nested_agg": {
"nested": {
"path": "soloists"
},
"aggs": {
"my_terms_agg": {
"term": {
"field": "soloists.person.id",
"size": 10
}
"aggs": {
"my_top_hits_agg": {
"size": 1,
"_source": {
"include": [
"soloists.person.id",
"soloists.person.name"
]
}
}
}
}
}
}
}
}
}
}
}
}
如有任何帮助,我们将不胜感激。
我在寻找解决方案时偶然发现的一些链接:
最佳答案
您的映射和查询中有一些拼写错误:
这里有一些固定的命令,在 Elasticsearch 5.6.3 的实例上使用时不会触发任何错误。
您可以在 Kibana 或 Linux 终端中复制和粘贴(在这种情况下您应该编辑第一行)并在您的 Elasticsearch 实例上测试它们。
HOST=10.225.0.2:9200
curl -XPUT "http://$HOST/an_index"
curl -XPUT "http://$HOST/an_index/recording/_mapping" -H 'Content-Type: application/json' -d'
{
"dynamic": "strict",
"_all": {
"enabled": false
},
"properties": {
"id": {
"type": "integer"
},
"soloists": {
"type": "nested",
"properties": {
"person": {
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
},
"work": {
"properties": {
"id": {
"type": "integer"
},
"title": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}'
curl -XPOST "http://$HOST/an_index/recording/1" -H 'Content-Type: application/json' -d'
{
"id": 0,
"soloists": [
{
"person": {
"id": 77957,
"name": "John doe"
}
},
{
"person": {
"id": 1,
"name": "Jane smith"
}
}
],
"work": {
"id": 0,
"title": "Test"
}
}'
curl -XGET "http://$HOST/an_index/recording/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"my_top_results": {
"global": {},
"aggs": {
"my_filter_agg": {
"filter": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"nested": {
"path": "soloists",
"query": {
"bool": {
"must": {
"match": {
"soloists.person.id": 77957
}
}
}
}
}
}
]
}
}
]
}
},
"aggs": {
"my_nested_agg": {
"nested": {
"path": "soloists"
},
"aggs": {
"my_terms_agg": {
"terms": {
"field": "soloists.person.id",
"size": 10
},
"aggs": {
"my_top_hits_agg": {
"top_hits": {
"size": 1,
"_source": {
"include": [
"soloists.person.id",
"soloists.person.name"
]
}
}
}
}
}
}
}
}
}
}
}
}
}'
如果这些查询有效但应用于您的索引时无效,请您使用 curl -XGET "http://$HOST/your_index_name"
的输出更新您的问题,以便我们检查您的索引的确切设置和映射?这种错误可能是由同一索引上的类型之间的冲突引起的。我会相应地更新我的答案。
关于elasticsearch - 在 ElasticSearch 5.6.3 中获取嵌套文档的聚合导致 Lucene 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47226236/