gpt4 book ai didi

elasticsearch - 在ElasticSearch中进行聚合查询时如何忽略尾随空格

转载 作者:行者123 更新时间:2023-12-03 01:04:15 26 4
gpt4 key购买 nike

我有一个汇总查询来确定哪个国家/地区的城市名称。查询(我的意思是)如下:

GET test/_search
{

"query" : {
"bool" : {
"must" : {
"match" : {
"name.autocomplete" : {
"query" : "new yo",
"type" : "boolean"
}
}
},
"must_not" : {
"term" : {
"source" : "old"
}
}
}
},
"aggregations" : {
"city_name" : {
"terms" : {
"field" : "cityname.raw",
"min_doc_count" : 1
},
"aggregations" : {
"country_name" : {
"terms" : {
"field" : "countryname.raw"
}
}
}
}
}
}

现在, New York在文档中两次出现两次,并带有额外的尾随空格。我得到的汇总结果如下:
{
"key": "New York",
"doc_count": 1,
"city_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "United States of America",
"doc_count": 1
}
]
}
},
{
"key": "New York ",
"doc_count": 1,
"city_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "United States of America",
"doc_count": 1
}
]
}
}

我需要将 New York都一样对待。有什么方法可以查询我是否将它们都归入同一组。我猜想任何能修饰尾随空格的东西都可以。虽然找不到任何东西。谢谢

最佳答案

理想的情况是在索引文档之前清理字段。如果那不是一个选择,您仍然可以在使用(例如)update-by-query plugin之后清理它们。

或者,但这是性能较差的,请使用terms聚合和script而不是field,如下所示:

...
"aggregations" : {
"city_name" : {
"terms" : {
"script" : "doc['cityname.raw'].value.trim()",
"min_doc_count" : 1
},
"aggregations" : {
"country_name" : {
"terms" : {
"script" : "doc['countryname.raw'].value.trim()",
}
}
}
}
}
}

另一个解决方案是将 not_analyzed更改为 analyzed字符串,但是创建一个自定义分析器,该分析器使用 not_analyzed 分析器和 keyword token filter来保留 token (就像 trim一样)。
{
"settings": {
"analysis": {
"analyzer": {
"trimmer": {
"type": "custom",
"filter": [ "trim" ],
"tokenizer": "keyword"
}
}
}
},
"mappings": {
"test": {
"properties": {
"cityname": {
"type": "string",
"analyzer": "trimmer"
},
"countryname": {
"type": "string",
"analyzer": "trimmer"
}
}
}
}
}

如果您索引 cityname: "New York City ",将要存储的 token 将被修剪为 "New York City"

关于elasticsearch - 在ElasticSearch中进行聚合查询时如何忽略尾随空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32880918/

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