2015-03-31 86 views
0

我使用ElasticSearch進行測試,並遇到遠程查詢問題。 考慮,我已經插入下列文件:使用ElasticSearch進行遠程查詢

curl -XPUT 'localhost:9200/test/test/test?pretty' -d ' 
{ 
    "name": "John Doe", 
    "duration" : "10", 
    "state" : "unknown" 
}' 

現在I'me嘗試這樣做,抓住其持續時間爲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之間的文檔。

回答

1

問題是您正在將值作爲字符串編入索引。這會導致範圍查詢不起作用。嘗試索引和查詢,如下所示:

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" 
} 
    } ] 
    } 
} 
+0

謝謝你,事實是,我已經嘗試過多次,但我從來沒有試圖刪除文件和再次插入它,我不斷更新相同的對象,將持續時間字段更改爲整數,但它永遠不會工作,但是,刪除我的舊文檔並重新編制索引使得範圍查詢按預期工作。 – 2015-03-31 11:01:16

+0

很好用!您必須刪除文檔的原因是,如果未指定映射,則彈性會在首次編制索引時自動爲您的文檔創建映射。然後它猜測用於文檔字段的類型。在這種情況下,它決定使用一個字符串來存儲持續時間。 只有當您刪除索引時,此自動創建的映射也會被刪除。 – mdewit 2015-03-31 11:05:58