2013-12-22 65 views
2

我們在elasticsearch中有一個域名索引(我們使用ruby連接和維護這個輪胎寶石),但是我們在精確搜索時遇到了麻煩。elasticsearch與破折號完全匹配

如果我在域中搜索術語google.com,它會帶回google.com,但它還會帶回任何帶有破折號( - )的域,例如in-google.com,research會讓我相信 - 是ES中的通配符,我需要做的就是不分析,但不起作用。

:domain  => { :type => 'string' , :analyzer => 'whitespace'       }, 
    :domain_2  => { :type => 'string' , :analyzer => 'pattern'       }, 
    :domain_3  => { :type => 'string', :index => 'not_analyzed'       }, 
    :domain_4  => { :type => 'string', :analyzer => 'snowball'       } 

我已經嘗試了不同的分析儀,你可以在上面看到,但使用「頭」插件搜索時,他們都具有相同的問題。

https://gist.github.com/anonymous/8080839是我用來生成數據集來測試的代碼,我在尋找的是能夠搜索JUST谷歌,如果我想*谷歌我可以實現我自己的通配符?

我辭職的事實,我將不得不刪除並重新生成我的索引,但無論我選擇什麼樣的分析或類型,我仍然不能得到一個確切的匹配

回答

2

你不顯示您正在使用的示例查詢。你確定你的查詢和索引使用相同的文本處理嗎?

此外,您可能希望將multi_field -approach檢查出分析事物的多種方式。

我做了一堆那說明這個不同的查詢可運行的例子。請注意,域名已經在兩個方面被索引,並注意查詢打哪場:https://www.found.no/play/gist/ecc52fad687e83ddcf73

#!/bin/bash 

export ELASTICSEARCH_ENDPOINT="http://localhost:9200" 

# Create indexes 

curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{ 
    "mappings": { 
     "type": { 
      "properties": { 
       "domain": { 
        "type": "multi_field", 
        "fields": { 
         "domain": { 
          "type": "string", 
          "analyzer": "standard" 
         }, 
         "whitespace": { 
          "type": "string", 
          "analyzer": "whitespace" 
         } 
        } 
       } 
      } 
     } 
    } 
}' 


# Index documents 
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d ' 
{"index":{"_index":"play","_type":"type"}} 
{"domain":"google.com"} 
{"index":{"_index":"play","_type":"type"}} 
{"domain":"in-google.com"} 
' 

# Do searches 

# Matches both 
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d ' 
{ 
    "query": { 
     "match": { 
      "_all": "google.com" 
     } 
    } 
} 
' 

# Also matches "google.com". in-google.com gets tokenized to ["in", "google.com"] 
# and the default match operator is `or`. 
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d ' 
{ 
    "query": { 
     "match": { 
      "domain": { 
       "query": "in-google.com" 
      } 
     } 
    } 
} 
' 

# What terms are generated? (Answer: `google.com` and `in`) 
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d ' 
{ 
    "size": 0, 
    "facets": { 
     "domain": { 
      "terms": { 
       "field": "domain" 
      } 
     } 
    } 
} 
' 

# This should just match the second document. 
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d ' 
{ 
    "query": { 
     "match": { 
      "domain.whitespace": { 
       "query": "in-google.com" 
      } 
     } 
    } 
} 
' 
+0

亞歷您好,感謝回答,我有點不確定我理解你的榜樣,我設置瞭如你所建議的multi_field方法(謝謝),但我仍然有問題搜索確切的域,你給的兩個例子查詢仍然顯示in-google.com,即使搜索查詢只是谷歌。 –

+0

對不起,我忘記了在輸出中丟失的註釋。如果你看這個劇本,應該對他們爲什麼被收錄進行評論。最後一個查詢只匹配in-google.com。我已經更新了答案,以包含更多澄清的評論。希望這有助於:) –

+0

我開始瞭解這一點(和播放)多一點,https://www.found.no/play/gist/dd354aad8703837877cf這是我目前的工作正在進行中多一點的數據,作爲你可以看到我有精確的匹配運行良好,但現在是通配符搜索,如果我想搜索谷歌*,在谷歌中仍然會像megoogle一樣出現。 –