2015-11-04 63 views
0

我創建了兩個索引時尚和手機與字段「名稱」。如何在彈性搜索中匹配不匹配的單詞,如「iphone手機」中的文檔如「iphone」

client.indices.create(index='fashion',body={"mappings": {"doc": {"properties": {"name": {"type": "string"} } } } }) 
client.indices.create(index='mobiles',body={"mappings": {"doc": {"properties": {"name": {"type": "string"} } } } }) 

對於時尚,下面的文件被添加。

client.index(index='mobiles',doc_type='blog',body={"query":{ "name": "i shirts" }}) 
client.index(index='mobiles',doc_type='blog',body={"query":{ "name": "i celekon" }}) 
client.index(index='mobiles',doc_type='blog',body={"query":{ "name": "satsung" }}) 

對於手機:

client.index(index='mobiles',doc_type='blog',body={"query":{ "name": "apple iphone 6s" }}) 
client.index(index='mobiles',doc_type='blog',body={"query":{ "name": "samsung galaxy s2" }}) 
client.index(index='mobiles',doc_type='blog',body={"query":{ "name": "apple iphone 5s" }}) 

當我用匹配查詢搜索術語像

search="i phone" 
test=client.search(index='mobiles,fashion',doc_type='blog',size=10,body={"query": {"bool" : {"should" : [{"match": {"name": {"query":search,"slop": 10,"max_expansions": 2 }}},{"match_phrase_prefix": {"name": {"query":search,"slop": 10,"max_expansions": 2}}},{"match": {"name": {"query":search, "fuzziness":1}}}]}}}) 

我得到以下順序結果..

我的襯衫,我celekon,蘋果iphone 6s,蘋果iphone 5s

我該如何跟蹤結果?

蘋果iPhone 6S,蘋果iPhone 5S,....

如何 「亞馬遜」, 「flipkart」 實現這些類型的搜索?

注意:我用elasticsearch-py api來進行搜索。

回答

0

你必須創造條件,使用Word Delimiter Token Filter自定義分析:

命名word_delimiter,它分裂成字子詞,並對子字組 可選的轉換。字被分成 子詞的規則如下:

  1. 分裂上字內的分隔符(默認情況下,所有的非字母數字
    字符)。 「Wi-Fi」→「Wi」,「Fi」
  2. 拆分大小寫轉換:「PowerShot」→「Power」,「Shot」
  3. 拆分字母數字轉換:「SD500」→「SD」, 「500」
  4. 領導和各子詞尾字內的分隔符 忽略:「//你好---那裏,花花公子」→「你好」,「有」,「花花公子」
  5. 後「的「每個子詞被刪除:‘奧尼爾的’→‘O’,‘尼爾’

我認爲你正在尋找第二個例子。如果您編制索引iPhone,它會創建令牌"i""Phone",這正是您要查找的內容。

有一點要記住,你應該照顧"preserve_original"參數那裏,並將其設置爲true,所以它確實保留原始單詞。這很重要,因爲用戶可以同時尋找我的手機和iPhone,它仍然會得分。

+0

如果「iphone」較低情況下,我們會怎麼做呢@Evaldas Buinauskas – Println

+0

你以後這個權利適用'lowercase'令牌過濾器,那麼iPhone iphone iphone和我的手機應該被視爲相同的關鍵字。 –

相關問題