gpt4 book ai didi

Elasticsearch 按字段过滤文档组

转载 作者:行者123 更新时间:2023-11-29 02:44:22 26 4
gpt4 key购买 nike

我有一些文件:

{"name": "John", "district": 1},
{"name": "Mary", "district": 2},
{"name": "Nick", "district": 1},
{"name": "Bob", "district": 3},
{"name": "Kenny", "district": 1}

如何按地区过滤/选择不同的文件?

{"name": "John", "district": 1},
{"name": "Mary", "district": 2},
{"name": "Bob", "district": 3}

在 SQL 中,我可以使用 GROUP BY。我尝试了术语聚合,但它只返回不同的计数。

"aggs": {
"distinct": {
"terms": {
"field": "district",
"size": 0
}
}
}

感谢您的帮助! :-)

最佳答案

如果您的 ElasticSearch 版本是 1.3 或更高版本,您可以使用 top_hits 类型的子聚合这将(默认情况下)为您提供根据查询分数排序的前三个匹配文档(此处为 1,因为您使用的是 match_all 查询)。

您可以将size参数设置为大于3。

以下数据集和查询:

POST /test/districts/
{"name": "John", "district": 1}

POST /test/districts/
{"name": "Mary", "district": 2}

POST /test/districts/
{"name": "Nick", "district": 1}

POST /test/districts/
{"name": "Bob", "district": 3}

POST test/districts/_search
{
"size": 0,
"aggs":{
"by_district":{
"terms": {
"field": "district",
"size": 0
},
"aggs": {
"tops": {
"top_hits": {
"size": 10
}
}
}
}
}
}

将按照您想要的方式输出文档:

{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"by_district": {
"buckets": [
{
"key": 1,
"key_as_string": "1",
"doc_count": 2,
"tops": {
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "districts",
"_id": "XYHu4I-JQcOfLm3iWjTiOg",
"_score": 1,
"_source": {
"name": "John",
"district": 1
}
},
{
"_index": "test",
"_type": "districts",
"_id": "5dul2XMTRC2IpV_tKRRltA",
"_score": 1,
"_source": {
"name": "Nick",
"district": 1
}
}
]
}
}
},
{
"key": 2,
"key_as_string": "2",
"doc_count": 1,
"tops": {
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "districts",
"_id": "I-9Gd4OYSRuexhP1dCdQ-g",
"_score": 1,
"_source": {
"name": "Mary",
"district": 2
}
}
]
}
}
},
{
"key": 3,
"key_as_string": "3",
"doc_count": 1,
"tops": {
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "districts",
"_id": "bti2y-OUT3q2mBNhhI3xeA",
"_score": 1,
"_source": {
"name": "Bob",
"district": 3
}
}
]
}
}
}
]
}
}
}

关于Elasticsearch 按字段过滤文档组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25986538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com