[英]Ranged Query with ElasticSearch
I'm testing with ElasticSearch and I'm having problems with ranged queries. 我正在使用ElasticSearch进行测试,但是远程查询存在问题。 Consider the following document that I've inserted:
考虑一下我插入的以下文档:
curl -XPUT 'localhost:9200/test/test/test?pretty' -d '
{
"name": "John Doe",
"duration" : "10",
"state" : "unknown"
}'
And now I'me trying to do a ranged query that catches all documents whose duration is between 5 and 15: 现在,我尝试进行一个范围内的查询,以捕获持续时间在5到15之间的所有文档:
curl -XPOST 'localhost:9200/test/_search?pretty' -d '
{
"query": {
"range": {
"duration": {
"gte": "5",
"lte": "15"
}
}
}
}'
This returns no hits however if I run the Query like this: 但是,如果我像这样运行查询,则不会返回任何匹配:
curl -XPOST 'localhost:9200/test/_search?pretty' -d '
{
"query": {
"range": {
"duration": {
"gte": "10"
}
}
}
}'
It returns the Document I've inserted earlier. 它返回我之前插入的文档。 How can I query ElasticSearch for documents with the duration value between 5 and 15.
如何查询ElasticSearch的持续时间值在5到15之间的文档。
The problem is that you are indexing your values as strings. 问题是您正在将值索引为字符串。 This causes the range query not to work.
这将导致范围查询不起作用。 Try indexing and querying as follows:
尝试按以下方式建立索引和查询:
curl -XPUT 'localhost:9200/test/test/test?pretty' -d '
{
"name": "John Doe",
"duration" : 10,
"state" : "unknown"
}'
curl -XPOST 'localhost:9200/test/_search?pretty' -d '
{
"query": {
"range": {
"duration": {
"gte": 5,
"lte": 15
}
}
}
}'
This wil yield the following result: 这将产生以下结果:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [ {
"_index" : "test",
"_type" : "test",
"_id" : "test",
"_score" : 1.0,
"_source":
{
"name": "John Doe",
"duration" : 10,
"state" : "unknown"
}
} ]
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.