需要在elasticsearch中对_term进行排序

需要在elasticsearch中对_term进行排序,第1张

需要在elasticsearch中对_term进行排序

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

对于字符串:“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
聚合中使用a
将字符串术语转换为数字,以及
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      }    }  }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5010235.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存