2014-05-15 58 views
19

在如此多的網站上,他們教授如何使用範圍查詢從Elasticsearch查詢數據。我想用像這樣的Lucene樣式查詢字符串從Elasticsearch查詢小於或等於某個數字的數據。Lucene查詢字符串Elasticsearch「小於或等於」[URI搜索]

fieldname:[* TO 100] 

fieldname:["*" TO "100"] 

我曾嘗試在其他格式,但這些都不奏效。有人能幫我嗎?

+0

我已經更新了我的答案.. – BlackPOP

+0

如果你不滿意的答案再問。不放棄的問題。接受答案並幫助他人.. – BlackPOP

回答

1

我想你想和小於等於100

curl -XPOST "http://hostname:9200/index/try/_search" -d' 
{ 
"query": { 
    "range": { 
     "FieldName": { 
     "lte" : 100 
     } 
    } 
    } 
}' 

PHP API客戶端

array(
'query' => array(
    'range' => array(
     'FieldName' => array(
      array("lte" => 100) 
     ) 
    ) 
) 
); 

更多查詢.. refer

查詢查詢文件格式你要求的.. ..!

curl -XPOST "http://hostname:9200/index/type/_search?q=FieldName:[* to 100]" 

HOpe它幫助..!

+1

我認爲這不是一個Lucene查詢字符串。我使用Elasticsearch PHP客戶端API,我不想使用JSON或數組格式作爲參數來查詢數據,但想要使用Lucene查詢字符串。 –

+0

你使用哪一個ES版本? – BlackPOP

+0

它是1.0。希望你能幫助我。 –

29

你將要使用的查詢字符串語法(http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html)的範圍與URI搜索(http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-uri-request.html

範圍

結合

範圍可以爲日期,數字或字符串字段中指定。包含 範圍用方括號[最小TO最大值]和排除 範圍和大括號{最小TO最大值}來指定。

All days in 2012: 

    date:[2012/01/01 TO 2012/12/31] 

    Numbers 1..5 

    count:[1 TO 5] 

    Tags between alpha and omega, excluding alpha and omega: 

    tag:{alpha TO omega} 

    Numbers from 10 upwards 

    count:[10 TO *] 

    Dates before 2012 

    date:{* TO 2012/01/01} 

Curly and square brackets can be combined: 

    Numbers from 1 up to but not including 5 

    count:[1..5} 

Ranges with one side unbounded can use the following syntax: 

age:>10 
age:>=10 
age:<10 
age:<=10 

Note 

To combine an upper and lower bound with the simplified syntax, you would need to join two clauses with an AND operator: 

age:(>=10 AND < 20) 
age:(+>=10 +<20) 

The parsing of ranges in query strings can be complex and error prone. It is much more reliable to use an explicit range filter. 

URI搜索

搜索URI搜索請求體搜索碎片API搜索 模板刻面聚集Suggesters語境建議者多搜索 API伯爵API驗證API說明API滲濾器更多類似這樣的API 基準

通過提供 請求參數,可以純粹使用URI執行搜索請求。當執行 使用此模式進行搜索時,並非所有搜索選項都被暴露,但對於快速「捲曲測試」它可能非常方便。 下面是一個例子:

$ curl -XGET 
'http://localhost:9200/twitter/tweet/_search?q=user:kimchy'