栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

需要在elasticsearch中对_term进行排序

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

需要在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      }    }  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/401433.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号