2012-05-06 62 views
7

一定非空鍵I'de想要過濾的elasticsearch查詢的結果和檢索只有那些具有非空場elasticsearch:只能得到有從URL

例如元素。給出以下數據

{ 
    total: 4912, 
    max_score: 1, 
    hits: [ 
{ 
    { 
    _index: "gcba", 
    _type: "bafici", 
    _id: "5a93472b-5db4-4ff9-8c8a-d13158e72d5f-62", 
    _score: 1, 
    _source: { 
     id_film: "23", 
     title: "great film", 
    } 
    }, 
    { 
    _index: "gcba", 
    _type: "bafici", 
    _id: "2732fbf4-4e55-4794-8e98-e5d5fa6a0419-40", 
    _score: 1, 
    _source: { 
     name: "conference", 
     [...] 
    } 
    } 
} 

我想發出類似

.../_search?from=1&size=100&q=id_film:'*' 

只得到與id_film值的那些元素

回答

5

ES將只返回由具有特定領域的文檔進行通配符查詢時默認:

% curl -XPUT http://localhost:9200/foo/bar/1 -d '{"id":1,"id_film":23}' 
{"ok":true,"_index":"foo","_type":"bar","_id":"1","_version":1}% 

% curl -XPUT http://localhost:9200/foo/bar/2 -d '{"id":2,"foo":23}' 
{"ok":true,"_index":"foo","_type":"bar","_id":"2","_version":1}% 

% curl "http://localhost:9200/foo/_search?q=id_film:*&pretty=true" 
{ 
    "took" : 2, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 1, 
    "max_score" : 1.0, 
    "hits" : [ { 
     "_index" : "foo", 
     "_type" : "bar", 
     "_id" : "1", 
     "_score" : 1.0, "_source" : {"id":1,"id_film":23} 
    } ] 
    } 
}% 
+0

非常感謝!這正是我所想的,但我搞砸了''的東西,沒有引用它效果很好! – opensas

4

您也可以使用exists(或丟失)過濾器。在這裏看到:

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-exists-filter.html

唯一的一點是,它是一個過濾器,而不是一個查詢。要使用搜索方法,您需要查詢match_all並將其作爲過濾器存在。 (或者,使用在其內指定的過濾器的恆定分數查詢)

+0

是的,過濾器可能不在查詢結構中,但可以被緩存... imho更適合生產使用 – gatoatigrado