簡體   English   中英

用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"
      }
    }
  }
}'

但是,如果我像這樣運行查詢,則不會返回任何匹配:

curl -XPOST 'localhost:9200/test/_search?pretty' -d '
{
  "query": {
    "range": {
      "duration": {
        "gte": "10"
      }
    }
  }
}'

它返回我之前插入的文檔。 如何查詢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"
}
    } ]
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM