2012-10-12 45 views
3

這是一個由兩部分組成的問題。如何讓elasticsearch執行完全匹配查詢?

我的文件是這樣的:

{"url": "https://someurl.com", 
"content": "searchable content here", 
"hash": "c54cc9cdd4a79ca10a891b8d1b7783c295455040", 
"headings": "more searchable content", 
"title": "Page Title"} 

我的第一個問題是如何獲取的所有文件,其中「標題」正是「無題」。我不希望出現標題爲「此文檔無標題」的文檔。

我的第二個問題是如何檢索URL中出現的所有文檔正好在一長串的URL中。

我使用pyelasticsearch,但捲曲的通用答案也可以。

回答

3

如果您存儲源(這是默認的),你可以使用一個script filter

它應該是這樣的:

$ curl -XPUT localhost:9200/index/type/1 -d '{"foo": "bar"}' 
$ curl -XPUT localhost:9200/index/type/2 -d '{"foo": "bar baz"}' 
$ curl -XPOST localhost:9200/index/type/_search?pretty=true -d '{ 
"filter": { 
    "script": { 
     "script": "_source.foo == \"bar\"" 
    } 
} 
}' 
{ 
    "took" : 2, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 5, 
    "successful" : 5, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 1, 
    "max_score" : 1.0, 
    "hits" : [ { 
     "_index" : "index", 
     "_type" : "type", 
     "_id" : "1", 
     "_score" : 1.0, "_source" : {"foo": "bar"} 
    } ] 
    } 
} 

編輯:我認爲值得一提的是,「not_analyzed」映射應該是更快的方法。但是如果你想要這個字段的精確和部分匹配,我看到兩個選擇:使用腳本或索引數據兩次(一旦分析,一次不分析)。

+1

這個解決方案爲索引中的每個*文檔執行腳本非常重要。您應該先對結果進行過濾(例如,對僞代碼「'foo:bar'AND _source.foo ='bar'」)對同一字段使用字符串查詢。我用620k的文件測試了這個索引,速度從「花7245」變爲「花19」 – sfussenegger

9

您必須定義字段映射。

如果您正在查找確切值(區分大小寫),您可以將索引屬性設置爲not_analyzed

喜歡的東西:

"url" : {"type" : "string", "index" : "not_analyzed"} 
+0

如果你的領域沒有被分析,這並不意味着elasticsearch基本上會做一個全表類型的掃描來查找文檔?這似乎很糟糕... –

+4

沒有。Lucene不是數據庫。這是一個索引。它不像一個數據庫。 – dadoonet