gpt4 book ai didi

ElasticSearch 术语聚合

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

我正在尝试使用以下查询对以下数据使用 Elasticsearch 执行术语聚合,输出将名称分解为标记(请参阅下面的输出)。所以我尝试将 os_name 映射为 multi_field,现在我无法通过它进行查询。是否可以有没有标记的索引?比如“Fedora Core”?

查询:

GET /temp/example/_search
{
"size": 0,
"aggs": {
"OS": {
"terms": {
"field": "os_name"
}
}
}
}

数据:

...
{
"_index": "temp",
"_type": "example",
"_id": "3",
"_score": 1,
"_source": {
"title": "system3",
"os_name": "Fedora Core",
"os_version": 18
}
},
{
"_index": "temp",
"_type": "example",
"_id": "1",
"_score": 1,
"_source": {
"title": "system1",
"os_name": "Fedora Core",
"os_version": 20
}
},
{
"_index": "temp",
"_type": "example",
"_id": "2",
"_score": 1,
"_source": {
"title": "backup",
"os_name": "Yellow Dog",
"os_version": 6
}
}
...

输出:

       ...
{
"key": "core",
"doc_count": 2
},
{
"key": "fedora",
"doc_count": 2
},
{
"key": "dog",
"doc_count": 1
},
{
"key": "yellow",
"doc_count": 1
}
...

映射:

PUT /temp
{
"mappings": {
"example": {
"properties": {
"os_name": {
"type": "string"
},
"os_version": {
"type": "long"
},
"title": {
"type": "string"
}
}
}
}
}

最佳答案

实际上你应该像这样改变你的映射

"os_name": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},

并且您的 aggs 应更改为:

GET /temp/example/_search
{
"size": 0,
"aggs": {
"OS": {
"terms": {
"field": "os_name.raw"
}
}
}
}

关于ElasticSearch 术语聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23769366/

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