需要在弹性搜索中排序_term [英] Need to Sort the _term in elastic search

查看:167
本文介绍了需要在弹性搜索中排序_term的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Indexer,它包含一个名为billingSequence的字段。映射中字段的数据类型为String,此字段的每个记录的值可以是1到30之间的值。我使用聚合
中的该字段,当我尝试排序_terms时,排序是不正确的字段是String类型。

  {
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 $'

更新



如果更改映射是不是选项,则c在您的术语中使用脚本将您的字符串术语转换为数字,以及$ $的未记录功能c $ c>术语聚合,即 value_type 设置,如下所示:

  {
size:0,
aggs:{
count:{
terms:{
script:doc.billingSequence.value as Integer,< ---将术语转换为整数
order:{
_term:asc
} ,
value_type:integer,< ---在排序
时,将条款视为整数size:10
}
}
}
}


I have an Indexer and which contains a field named 'billingSequence'. The datatype for the field in mapping is String and the value for each record for this field can be one among 1 to 30. I am using this field in terms aggregation and when I tried to sort the _terms, the ordering is improper as the field is of String type.

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

            }

the result for the above aggregation sorting is like -- 1 11 12 13 14 15 16 17 18 19 2 3 4 5 etc.

the expected result is -- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 etc.

It would be a great help if someone can look into this and help.

Thanks..

解决方案

That's because you're sorting strings and the lexical order of string is different than the order of the numbers represented by those strings.

For strings: "11" comes before "2" because "1" is before "2"

For numbers: 11 comes after 2 obviously.

The solution to this is to map your billingSequence field as an integer instead of a string.

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

Note that you need to wipe your index first (1), re-create it and install the above mapping (2) and finally re-index your data (3). Then your aggregation will work as expected.

(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}'

UPDATE

If changing the mapping is not an option, you can use a script in your terms aggregation to transform your string terms to numbers along with an undocumented feature of the terms aggregation, i.e. the value_type setting, like this:

{
  "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
      }
    }
  }
}

这篇关于需要在弹性搜索中排序_term的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆