2015-04-20 52 views
7

使用Elasticsearch完成建議程序我在返回與單字查詢匹配的多字輸入建議時遇到問題。Elasticsearch完成建議使用多字輸入進行搜索

實施例結構:

PUT /test_index/ 
{ 
    "mappings": { 
     "item": { 
     "properties": { 
      "test_suggest": { 
       "type": "completion", 
       "index_analyzer": "whitespace", 
       "search_analyzer": "whitespace", 
       "payloads": false 
      } 
     } 
     } 
    } 
} 

PUT /test_index/item/1 
{ 
    "test_suggest": { 
     "input": [ 
     "cat dog", 
     "elephant" 
     ] 
    } 
} 

工作查詢:

POST /test_index/_suggest 
{ 
    "test_suggest":{ 
     "text":"cat", 
     "completion": { 
      "field" : "test_suggest" 
     } 
    } 
} 

與結果

{ 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "test_suggest": [ 
     { 
     "text": "cat", 
     "offset": 0, 
     "length": 3, 
     "options": [ 
      { 
       "text": "cat dog", 
       "score": 1 
      } 
     ] 
     } 
    ] 
} 

失敗查詢:

POST /test_index/_suggest 
{ 
    "test_suggest":{ 
     "text":"dog", 
     "completion": { 
      "field" : "test_suggest" 
     } 
    } 
} 

與結果

{ 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "test_suggest": [ 
     { 
     "text": "dog", 
     "offset": 0, 
     "length": 3, 
     "options": [] 
     } 
    ] 
} 

我希望同樣的結果作爲工作的查詢,匹配「貓狗」。任何建議是什麼問題以及如何使失敗的查詢工作?當使用標準分析儀代替空白分析儀時,我會得到相同的結果。我想在上面的例子中給每個輸入字符串使用多個單詞。

回答

10

完成建議程序是prefix suggester,這意味着它會嘗試將您的查詢與輸入的前幾個字符進行匹配。如果您想要發佈的文檔與文本「dog」匹配,那麼您需要指定「dog」作爲輸入。

PUT /test_index/item/1 
{ 
    "test_suggest": { 
     "input": [ 
     "cat dog", 
     "elephant", 
     "dog" 
     ] 
    } 
} 

以我的經驗,不必指定輸入相匹配的限制使得完成suggesters不太有用其他方法來實現的前綴匹配。爲此,我喜歡edge ngrams。我最近寫了一篇博客文章中使用的n-gram,你可能會發現有用:http://blog.qbox.io/an-introduction-to-ngrams-in-elasticsearch

作爲一個簡單的例子,這裏是一個映射,您可以使用

PUT /test_index 
{ 
    "settings": { 
     "analysis": { 
     "filter": { 
      "edge_ngram_filter": { 
       "type": "edge_ngram", 
       "min_gram": 2, 
       "max_gram": 20 
      } 
     }, 
     "analyzer": { 
      "edge_ngram_analyzer": { 
       "type": "custom", 
       "tokenizer": "standard", 
       "filter": [ 
        "lowercase", 
        "edge_ngram_filter" 
       ] 
      } 
     } 
     } 
    }, 
    "mappings": { 
     "item": { 
     "properties": { 
      "text_field": { 
       "type": "string", 
       "index_analyzer": "edge_ngram_analyzer", 
       "search_analyzer": "standard" 
      } 
     } 
     } 
    } 
} 

那麼指數的文檔是這樣的:

PUT /test_index/item/1 
{ 
    "text_field": [ 
     "cat dog", 
     "elephant" 
    ] 
} 

和這些查詢將返回它:

POST /test_index/_search 
{ 
    "query": { 
     "match": { 
      "text_field": "dog" 
     } 
    } 
} 

POST /test_index/_search 
{ 
    "query": { 
     "match": { 
      "text_field": "ele" 
     } 
    } 
} 

POST /test_index/_search 
{ 
    "query": { 
     "match": { 
      "text_field": "ca" 
     } 
    } 
} 

她E公司代碼一起:

http://sense.qbox.io/gist/4a08fbb6e42c34ff8904badfaaeecc01139f96cf

+2

搜索+查詢不返回文本和我的情況下,文本可以是三個字段中的任何東西,所以我怎麼能顯示自動完成確切的文本。 –

相關問題