【问题标题】:Range strange results范围奇怪的结果
【发布时间】:2015-11-19 18:46:27
【问题描述】:

我将此代码用于范围。显示价格范围内的住宿 (weekly_price)

GET /accommodations/_search
{
    "query": {
        "filtered": {
            "query":  {"match_all":{}},
            "filter": {"and":[{"range":{"weekly_price":{"gte":0,"lte":500}}}]}
        }
    }
}

结果很奇怪。有时我会得到正确的结果(显示 0 到 500 欧元之间的住宿,有时我也会得到 2000 年的weekly_price 住宿。

我做错了什么?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    唯一可行的方法是您的weekly_price 字段是字符串而不是数字字段(integerlong 等)。在检索"0""500" 范围内的字符串时,字符串"2000" 在词法上属于该范围,因为"2""0"“大”,比"5"“小”。

    如果您使用

    检索映射
    curl -XGET localhost:9200/accommodations/_mapping
    

    您应该看到weekly_price 字段的类型为string

    您需要将该类型更改为integer 或其他对您的数据有意义的数字类型,擦除您的索引并使用如下所示的新映射重新创建它。这样做后,您的查询将按预期工作。

    // delete your index
    curl -XDELETE localhost:9200/accommodations 
    
    // re-create your index with the proper mapping
    curl -XPUT localhost:9200/accommodations -d '{
        "mappings": {
            "your_type": {
                "properties": {
                    "weekly_price": {
                        "type": "integer"
                    },
                    ... other properties
                }
            }
        }
    }'
    
    // re-populate your index
    curl -XPOST localhost:9200/accommodations/_bulk
    

    【讨论】:

    • 这个运气好吗?
    【解决方案2】:

    如果你想使用范围过滤器,你应该有一个有效的映射,例如;你应该有的日期;

    "properties": {
        "date": {
                    "type": "date",
                    "format": "date"
                  }
    

    Val 先生在上面也写过整数字段。比你可以使用范围过滤器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-28
      • 2015-09-07
      • 2013-03-30
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 1970-01-01
      相关资源
      最近更新 更多