gpt4 book ai didi

elasticsearch - 在Elasticsearch中汇总值列表

转载 作者:行者123 更新时间:2023-12-02 23:37:06 24 4
gpt4 key购买 nike

{
...
"shirt_sizes":["XL","L","M","S"]
...
}

如何在elasticsearch中创建shirt_size字段的聚合?

最佳答案

假设我正确理解了您的问题,则需要使用terms aggregation。您可能还希望"shirt_sizes"字段为 not_analyzed

作为一个简单的示例,我使用一个未分析的字段设置了索引,并添加了一些文档:

PUT /test_index
{
"mappings": {
"doc": {
"properties": {
"shirt_sizes": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}

POST /test_index/doc/_bulk
{"index":{"_id":1}}
{"shirt_sizes":["XL","L","M","S"]}
{"index":{"_id":2}}
{"shirt_sizes":["L","M","S","XS"]}

然后,我可以通过简单的聚合来获取所有大小:
POST /test_index/_search?search_type=count
{
"aggs": {
"shirt_sizes_terms": {
"terms": {
"field": "shirt_sizes"
}
}
}
}
...
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"shirt_sizes_terms": {
"buckets": [
{
"key": "L",
"doc_count": 2
},
{
"key": "M",
"doc_count": 2
},
{
"key": "S",
"doc_count": 2
},
{
"key": "XL",
"doc_count": 1
},
{
"key": "XS",
"doc_count": 1
}
]
}
}
}

这是我使用的代码:

http://sense.qbox.io/gist/9d759c95c8794be7786b0248f58bfdec6da7510f

关于elasticsearch - 在Elasticsearch中汇总值列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29651605/

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