2016-01-22 44 views
2

我正在嘗試使用Elasticsearch提供一個建議功能。 關注此文章https://qbox.io/blog/multi-field-partial-word-autocomplete-in-elasticsearch-using-ngrams獲取有關字段Elasticsearch的建議

我現在的工作,但不是在同一句話中的兩個單詞。

我現在在ES的數據是。

{ 
    "_index": "books", 
    "_type": "book", 
    "_id": "AVJp8p4ZTfM-Ee45GnF5", 
    "_score": 1, 
    "_source": { 
     "title": "Making a dish", 
     "author": "Jim haunter" 
    } 
}, 
{ 
    "_index": "books", 
    "_type": "book", 
    "_id": "AVJp8jaZTfM-Ee45GnF4", 
    "_score": 1, 
    "_source": { 
     "title": "The big fish", 
     "author": "Jane Stewart" 
    } 
}, 
{ 
    "_index": "books", 
    "_type": "book", 
    "_id": "AVJp8clRTfM-Ee45GnF3", 
    "_score": 1, 
    "_source": { 
     "title": "The Hunter", 
     "author": "Jame Franco" 
    } 
} 

這裏是映射和設置。

{"settings": { 
    "analysis": { 
    "filter": { 
     "nGram_filter": { 
      "type": "nGram", 
      "min_gram": 2, 
      "max_gram": 20, 
      "token_chars": [ 
       "letter", 
       "digit" 
      ] 
     } 
    }, 
    "analyzer": { 
     "nGram_analyzer": { 
      "type": "custom", 
      "tokenizer": "whitespace", 
      "filter": [ 
       "lowercase", 
       "nGram_filter" 
      ] 
     }, 
     "whitespace_analyzer": { 
      "type": "custom", 
      "tokenizer": "whitespace", 
      "filter": [ 
       "lowercase" 
      ] 
     } 
    } 
    } 
}, 
"mappings": { 
    "books": { 
    "_all": { 
     "index_analyzer": "nGram_analyzer", 
     "search_analyzer": "whitespace_analyzer" 
    }, 
    "properties": { 
     "title": { 
      "type": "string", 
      "index": "no" 
     }, 
     "author": { 
      "type": "string", 
      "index": "no" 
     } 
     } 
    } 
    } 
} 

這裏是搜索

{ 
    "size": 10, 
    "query": { 
    "match": { 
    "_all": { 
     "query": "Hunter", 
     "operator": "and", 
     "fuzziness": 1 
    } 
    } 
} 
} 

,當我搜索「的」我得到 「大魚」和 「獵人」。 但是,當我輸入「The Hunt」時,我一無所獲。 爲了再次獲得這本書,我需要輸入「The Hunte」。 有什麼建議嗎? 任何幫助表示讚賞。

回答

1

從工作區域中刪除"index": "no"。另外,由於我使用的是ES 2.x,我不得不用"analyzer"替換「index_analyzer」。因此,這裏的映射:

PUT /test_index 
{ 
    "settings": { 
     "analysis": { 
     "filter": { 
      "nGram_filter": { 
       "type": "nGram", 
       "min_gram": 2, 
       "max_gram": 20, 
       "token_chars": [ 
        "letter", 
        "digit" 
       ] 
      } 
     }, 
     "analyzer": { 
      "nGram_analyzer": { 
       "type": "custom", 
       "tokenizer": "whitespace", 
       "filter": [ 
        "lowercase", 
        "nGram_filter" 
       ] 
      }, 
      "whitespace_analyzer": { 
       "type": "custom", 
       "tokenizer": "whitespace", 
       "filter": [ 
        "lowercase" 
       ] 
      } 
     } 
     } 
    }, 
    "mappings": { 
     "books": { 
     "_all": { 
      "analyzer": "nGram_analyzer", 
      "search_analyzer": "whitespace_analyzer" 
     }, 
     "properties": { 
      "title": { 
       "type": "string" 
      }, 
      "author": { 
       "type": "string" 
      } 
     } 
     } 
    } 
} 

下面是一些代碼我用來測試它:

http://sense.qbox.io/gist/0140ee0f5043f66e76cc3109a18d573c1d09280b

+0

我不爲我工作,我想我使用ES 1.7.2。也許這是問題? – dontberude

+0

Oppps對不起,它似乎工作。非常感謝。 – dontberude