2015-11-18 300 views
0

我正在使用此代码的范围。要显示的价格范围(weekly_price范围奇怪的结果

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

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

一个weekly_price的住宿我在做什么错?

回答

0

这是唯一可行的办法是,如果你的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 
0

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

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

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