
这是因为您正在对字符串进行排序,并且字符串的词法顺序与这些字符串所表示的数字顺序不同。
对于字符串:“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 } } }}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)