gpt4 book ai didi

elasticsearch - 需要对 Elasticsearch 中的术语进行排序

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

我有一个索引器,其中包含一个名为“billingSequence”的字段。映射中字段的数据类型是字符串,该字段的每条记录的值可以是 1 到 30 之间的一个。我在聚合方面使用这个字段
当我尝试对_terms 进行排序时,排序不正确,因为该字段是字符串类型。

{
"aggs": {
"count": {
"terms": {
"field": "billingSequence"
, "order" : { "_term" : "asc" }
}
}
}

}

上述聚合排序的结果是——
1 11 12 13 14 15 16 17 18 19 2 3 4 5 等

预期的结果是——
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 等

如果有人可以对此进行调查并提供帮助,那将是一个很大的帮助。

谢谢..

最佳答案

那是因为您正在对字符串进行排序,并且字符串的词法顺序与这些字符串表示的数字的顺序不同。

对于字符串:“11”来自 之前 “2”,因为“1”在“2”之前

对于数字:11 来 之后 2显然。

解决方案是映射您的 billingSequence字段作为整数而不是字符串。

{
"billingSequence": {
"type": "integer"
}
}

请注意,您需要先删除索引 (1),重新创建它并安装上述映射 (2),最后重新索引您的数据 (3)。然后您的聚合将按预期工作。

(1)
curl -XDELETE localhost:9200/your_index

(2)
curl -XPUT localhost:9200/your_index -d '{
"mappings": {
"your_type": {
"properties": {
"billingSequence": {
"type": "integer"
}
}
}
}
}

(3)
curl -XPOST localhost:9200/your_index/your_type/1 -d '{"billingSequence": 1}'
curl -XPOST localhost:9200/your_index/your_type/2 -d '{"billingSequence": 2}'
curl -XPOST localhost:9200/your_index/your_type/3 -d '{"billingSequence": 3}'

更新

如果更改映射是 不是选项 ,您可以使用 script在您的 terms聚合将您的字符串术语转换为数字以及 terms 的未记录功能聚合,即 value_type设置,像这样:
{
"size": 0,
"aggs": {
"count": {
"terms": {
"script": "doc.billingSequence.value as Integer", <--- transform the terms to integers
"order": {
"_term": "asc"
},
"value_type": "integer", <--- consider the terms as integer when sorting
"size": 10
}
}
}
}

关于elasticsearch - 需要对 Elasticsearch 中的术语进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32969369/

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