我正在使用这个代码来表示范围。显示价格范围内的住宿(weekly_price)
GET /accommodations/_search
{
"query": {
"filtered": {
"query": {"match_all":{}},
"filter": {"and":[{"range":{"weekly_price":{"gte":0,"lte":500}}}]}
}
}
}结果很奇怪。有时我会得到正确的结果(显示0到500欧元之间的住宿,有时我还会得到weekly_price为2000的住宿。
我做错了什么?
发布于 2015-11-19 12:47:16
唯一可能的方法是,如果您的weekly_price字段是字符串而不是数字字段(integer、long等)。在检索"0"和"500"范围内的字符串时,字符串"2000"在词法上属于该范围,因为"2"比"0"“大”,比"5"“小”。
如果您使用以下命令检索映射
curl -XGET localhost:9200/accommodations/_mapping您应该看到类型为string的weekly_price字段。
您需要将该类型更改为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发布于 2015-11-19 18:46:28
如果要使用范围筛选器,则应具有有效映射,例如;对于日期应具有;
"properties": {
"date": {
"type": "date",
"format": "date"
}另外,对于整型字段,Val先生在上面写了它。然后你可以使用范围过滤器。
https://stackoverflow.com/questions/33790075
复制相似问题