2017-04-24 42 views
4

我使用ElasticSearch 5.1.2作爲Heroku的Searchly插件,使用nodeJS包(https://github.com/elastic/elasticsearch-js)。 我試圖用菜單提示符匹配菜名。 我相信我遵循信函以下教程https://www.elastic.co/guide/en/elasticsearch/reference/5.1/search-suggesters-phrase.htmlElasticSearch短語提示器沒有返回結果

我可以在Searchly儀表板中看到我的商店包含數據。 但是,我的查詢從不返回任何結果,即使在搜索確切的短語時也是如此。

這裏是我的設置(CoffeeScript的):

elasticSearchClient.indices.exists index: 'dishes' 
.then (indexExists) -> 
    if indexExists 
    elasticSearchClient.indices.delete index: 'dishes' 
.then() -> 
    elasticSearchClient.indices.create index: 'dishes' 
.then() -> 
    elasticSearchClient.indices.close index: 'dishes' 
.then() -> 
    elasticSearchClient.indices.putSettings { 
    index: 'dishes' 
    body: 
     settings: 
     number_of_shards: 1 
     analysis: 
      filter: 
      shingle: 
       type: 'shingle', 
       min_shingle_size: 2 
       max_shingle_size: 3 
      analyzer: 
      trigram: 
       type: 'custom', 
       tokenizer: 'standard', 
       filter: ['standard', 'shingle'] 
      reverse: 
       type: 'custom', 
       tokenizer: 'standard', 
       filter: ['standard', 'reverse'] 
    } 
.then() -> 
    elasticSearchClient.indices.open index: 'dishes' 
.then() -> 
    elasticSearchClient.indices.putMapping { 
    index: 'dishes' 
    type: 'dishes' 
    body: 
     properties: 
     dishNameMatching: 
      type: 'text' 
      fields: 
      trigram: 
       type: 'text' 
       analyzer: 'trigram' 
      reverse: 
       type: 'text' 
       analyzer: 'reverse' 
     name: 
      type: 'string' 
      index: 'no' 
     flavorId: 
      type: 'string' 
      index: 'no' 
    } 

這裏是我的查詢:

elasticSearchClient.suggest { 
    index: 'dishes' 
    body: 
    text: myText 
    dishSuggester: 
     phrase: 
     field: 'dishNameMatching.trigram' 
     size: options.limit or 5 
     gram_size: 3 
     direct_generator: [ 
      field: 'dishNameMatching.trigram' 
      suggest_mode: 'always' 
     ] 
     highlight: { 
      pre_tag: '[' 
      post_tag: ']' 
     } 
} 

這裏是我所得到的,當我搜索 「與草莓醬薯片」:

results: { 
    "_shards": { 
    "total": 1, 
    "successful": 1, 
    "failed": 0 
    }, 
    "dishSuggester": [ 
    { 
     "text": "potato chips with strawberry jam", 
     "offset": 0, 
     "length": 49, 
     "options": [] # Should be some results here :-(
    } 
    ] 
} 

謝謝您的閱讀,我將不勝感激任何提示!

回答

0

我們遇到了同樣的問題。原來我們創建的映射和我們索引的數據之間的類型名稱不匹配。例如,我們正在定義類型爲agent的映射,但我們的數據正在以類型agents發送到索引。

因此,正在創建的新類型不包含trigram字段和reverse字段,因此在字段中沒有任何分析器可以提供建議。

希望這會有所幫助!