gpt4 book ai didi

elasticsearch - Elasticsearch 多值字段聚合

转载 作者:行者123 更新时间:2023-12-03 02:02:29 25 4
gpt4 key购买 nike

我的索引文档具有以下架构:

{
...
'authors': [{'first name': 'John', 'last name': 'Smith'},
{'first name': 'Mark', 'last name': 'Spencer'}]
...
}

我想搜索它们并按单个作者进行汇总,因此获得了我的命中出现的主要作者的列表。 Terms aggregation似乎可以满足我的需求,但是我无法让它用于包含值列表的字段。有什么帮助吗?

最佳答案

您可能需要使用nested type,然后可以在作者姓名上使用nested aggregation

例如,我设置了一个简单的索引,如下所示:

PUT /test_index
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"doc": {
"properties": {
"title": {
"type": "string"
},
"authors": {
"type": "nested",
"properties": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
}
}
}
}
}
}
}

然后添加了一些文档:
PUT /test_index/doc/1
{
"title": "Book 1",
"authors": [
{
"first_name": "John",
"last_name": "Smith"
},
{
"first_name": "Mark",
"last_name": "Spencer"
}
]
}

PUT /test_index/doc/2
{
"title": "Book 2",
"authors": [
{
"first_name": "Ben",
"last_name": "Jones"
},
{
"first_name": "Tom",
"last_name": "Lawrence"
}
]
}

然后,我可以获得具有(分析的)作者姓氏的列表:
POST /test_index/_search?search_type=count
{
"aggs": {
"nested_authors": {
"nested": {
"path": "authors"
},
"aggs": {
"author_last_names": {
"terms": {
"field": "authors.last_name"
}
}
}
}
}
}
...
{
"took": 71,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"nested_authors": {
"doc_count": 4,
"author_last_names": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "jones",
"doc_count": 1
},
{
"key": "lawrence",
"doc_count": 1
},
{
"key": "smith",
"doc_count": 1
},
{
"key": "spencer",
"doc_count": 1
}
]
}
}
}
}

这是我使用的代码:

http://sense.qbox.io/gist/ca94cc11a12f8e4fed5c62c52966128b9a6f58de

关于elasticsearch - Elasticsearch 多值字段聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30710988/

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