【发布时间】:2015-11-30 09:05:18
【问题描述】:
我一直在为一个问题苦苦挣扎,所以我想我会通过 stackoverflow 解决这个问题。
我的文档类型有一个标题、一个语言字段(用于过滤)和一个分组 ID 字段(我省略了所有其他字段以保持重点)
当我搜索文档时,我想查找包含标题中文本的所有文档。对于每个唯一的分组 ID,我只需要一个文档。
我一直在研究 tophits 聚合,据我所知,它应该能够解决我的问题。
对我的索引运行此查询时:
{
"query": {
"match": {
"title": "dingo"
}
},
"aggs": {
"top-tags": {
"terms": {
"field": "groupId",
"size": 1000000
},
"aggs": {
"top_tag_hits": {
"top_hits": {
"_source": {
"include": [
"*"
]
},
"size": 1
}
}
}
}
}
}
我收到以下回复(所有结果都使用相同的语言):
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"top-tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [{
"key": "3044BC9E7C29450AAB2E4B6C9B35AAE2",
"doc_count": 2,
"top_tag_hits": {
"hits": {
"total": 2,
"max_score": 1.4983996,
"hits": [{
"_index": "elasticsearch",
"_type": "productdocument",
"_id": "FB15279FB18E4B34AD66ACAF69B96E9E",
"_score": 1.4983996,
"_source": {
"groupId": "3044BC9E7C29450AAB2E4B6C9B35AAE2",
"title": "wombat, dingo and zetapunga actionfigures",
}
}]
}
}
},
{
"key": "F11799ABD0C14B98ADF2554C84FF0DA0",
"doc_count": 1,
"top_tag_hits": {
"hits": {
"total": 1,
"max_score": 1.30684,
"hits": [{
"_index": "elasticsearch",
"_type": "productdocument",
"_id": "42562A25E4434A0091DE0C79A3E7F3F4",
"_score": 1.30684,
"_source": {
"groupId": "F11799ABD0C14B98ADF2554C84FF0DA0",
"title": "awesome dingo raptor"
}
}]
}
}
}]
}
}
}
这正是我所期望的(一个存储桶中有两次命中,但只为该存储桶检索了一个文档)。但是,当我在 NEST 中尝试此操作时,我似乎无法检索所有文档。
我的查询如下所示:
result = _elasticClient.Search<T>(s => s
.From(skip)
.Filter(fd => fd.Term(f => f.Language, language))
.Size(pageSize)
.SearchType(SearchType.Count)
.Query(
q => q.Wildcard(f => f.Title, query, 2.0)
|| q.Wildcard(f => f.Description, query)
)
.Aggregations(agd =>
agd.Terms("groupId", tagd => tagd
.Field("groupId")
.Size(100000) //We sadly need all products
)
.TopHits("top_tag_hits", thagd => thagd
.Size(1)
.Source(ssd => ssd.Include("*")))
));
var topHits = result.Aggs.TopHits("top_tag_hits");
var documents = topHits.Documents<ProductDocument>(); //contains only one document (I would expect it to contain two, one for each bucket)
检查调试器中的聚合显示有一个带有 2 个存储桶的“groupId”聚合(并且与我在“原始”查询中看到的索引相匹配。只是没有任何明显的方式来检索文档)
所以我的问题是。如何检索每个存储桶的热门歌曲?还是我这样做完全错了?还有其他方法可以实现我想要做的吗?
编辑
在我收到帮助后,我能够通过以下方式检索我的结果:
result = _elasticClient.Search<T>(s => s
.From(skip)
.Filter(fd => fd.Term(f => f.Language, language))
.Size(pageSize)
.SearchType(SearchType.Count)
.Query(
q => q.Wildcard(f => f.Title, query, 2.0)
|| q.Wildcard(f => f.Description, query)
)
.Aggregations(agd =>
agd.Terms("groupId", tagd => tagd
.Field("groupId")
.Size(0)
.Aggregations(tagdaggs =>
tagdaggs.TopHits("top_tag_hits", thagd => thagd
.Size(1)))
)
)
);
var groupIdAggregation = result.Aggs.Terms("groupId");
var topHits =
groupIdAggregation.Items.Select(key => key.TopHits("top_tag_hits"))
.SelectMany(topHitMetric => topHitMetric.Documents<ProductDocument>()).ToList();
【问题讨论】:
-
关闭
Terms聚合后,您不必添加.Aggregations吗? -
@EvaldasBuinauskas 我不确定我理解你的意思。你的意思是我的 TopHits 聚合应该在它自己单独的 .Aggregation 中(在第一个之后)我试过了,现在我只得到一个 tophits 聚合。仍然只有一份文件
-
我的意思是每个条款聚合应该有它自己的 TopHits 聚合。您也在 RAW 查询中这样做。
-
@EvaldasBuinauskas 你是对的。我将聚合移动到术语聚合中,现在我能够按预期检索结果。谢啦!如果你做出回答,我会确保你的功劳。 :)
-
顺便说一句,您不必使用
Include("*")来包含所有字段。只需删除此选项。并指定.Size(0)应该为您带来所有可能的条款。我会添加这个来回答。
标签: elasticsearch nest