2015-11-18 184 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" 
       } 

也用於整數字段先生瓦爾在上面寫道。比你可以使用範圍過濾器。