使用 ElasticSearch 进行范围查询
Ranged Query with ElasticSearch
我正在使用 ElasticSearch 进行测试,但我遇到了范围查询问题。
考虑我插入的以下文档:
curl -XPUT 'localhost:9200/test/test/test?pretty' -d '
{
"name": "John Doe",
"duration" : "10",
"state" : "unknown"
}'
现在我正在尝试进行范围查询,以捕获持续时间在 5 到 15 之间的所有文档:
curl -XPOST 'localhost:9200/test/_search?pretty' -d '
{
"query": {
"range": {
"duration": {
"gte": "5",
"lte": "15"
}
}
}
}'
这个 returns 没有命中但是如果我 运行 这样的查询:
curl -XPOST 'localhost:9200/test/_search?pretty' -d '
{
"query": {
"range": {
"duration": {
"gte": "10"
}
}
}
}'
它 returns 我之前插入的文档。如何在 ElasticSearch 中查询持续时间值在 5 到 15 之间的文档。
问题是您将您的值作为字符串编制索引。这会导致范围查询不起作用。尝试按如下方式建立索引和查询:
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
}
}
}
}'
这将产生以下结果:
{
"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"
}
} ]
}
}
我正在使用 ElasticSearch 进行测试,但我遇到了范围查询问题。 考虑我插入的以下文档:
curl -XPUT 'localhost:9200/test/test/test?pretty' -d '
{
"name": "John Doe",
"duration" : "10",
"state" : "unknown"
}'
现在我正在尝试进行范围查询,以捕获持续时间在 5 到 15 之间的所有文档:
curl -XPOST 'localhost:9200/test/_search?pretty' -d '
{
"query": {
"range": {
"duration": {
"gte": "5",
"lte": "15"
}
}
}
}'
这个 returns 没有命中但是如果我 运行 这样的查询:
curl -XPOST 'localhost:9200/test/_search?pretty' -d '
{
"query": {
"range": {
"duration": {
"gte": "10"
}
}
}
}'
它 returns 我之前插入的文档。如何在 ElasticSearch 中查询持续时间值在 5 到 15 之间的文档。
问题是您将您的值作为字符串编制索引。这会导致范围查询不起作用。尝试按如下方式建立索引和查询:
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
}
}
}
}'
这将产生以下结果:
{
"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"
}
} ]
}
}