4

我正在使用Elasticsearch & Typeahead在我的Rails應用程序中執行自動完成。我的想法從這裏Typeahead/Rails:無法正常工作

https://shellycloud.com/blog/2013/10/adding-search-and-autocomplete-to-a-rails-app-with-elasticsearch

我elasticsearch自動完成配置正確,因爲它,當我直接通過瀏覽器訪問它的工作原理。但是,當我嘗試使用typeahead從自動完成查詢調用顯示數據時,它甚至不會在我的調試器中觸發。這裏是我的形式&的JavaScript,其中預輸入被稱爲

<script> 
    $('#autcomplete_search').typeahead({ 
    highlight: true 
    }, 
    { 
    name: 'apple_game', 
    remote: "/search/autocomplete?query=%QUERY" 
    }); 
</script> 

<h1>Keyword</h1> 
<form action="/search/keyword"> 
    <div> 
    <%= text_field_tag :query, params[:query], class: "form-control", id: "autcomplete_search" %> 
    <br/> 
    <br/> 
    </div> 
    <div> 
    <input type="submit">/</input> 
    </div> 
</form> 

控制器

def autocomplete 
    es = ESClient.get_client 
    games = es.suggest index: 'games', 
     body: { 
     apple_game: { 
      text: params[:keyword], 
      completion: { 
      field: "title"} 
     } 
     } 
    render json: games 
    end 

樣的瀏覽器從結果控制器方法

{ 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "apple_game": [ 
     { 
      "text": "ma", 
      "offset": 0, 
      "length": 2, 
      "options": [ 
       { 
        "text": "Macabre Mysteries: Curse of the Nightingale Collector's Edition HD", 
        "score": 1 
       }, 
       { 
        "text": "Mad Cop - Police Car Race and Drift (Ads Free)", 
        "score": 1 
       }, 
       { 
        "text": "Mad Freebording (Snowboarding)", 
        "score": 1 
       }, 
       { 
        "text": "Mad Merx: Nemesis", 
        "score": 1 
       }, 
       { 
        "text": "Mad River Whitewater Kayak Rush", 
        "score": 1 
       } 
      ] 
     } 
    ] 
} 

編輯 每當預輸入運行

Uncaught Error: missing source 
+0

什麼版本的預輸入您使用的是 –

+0

我使用v0.10.1從http://twitter.github.io/typeahead。 js/ – user2158382

+0

,請張貼您想要打印的字段的映射 –

回答

6

好吧,我也注意到在控制檯下面的錯誤,我覺得你有兩個問題。

問題1:

你看我像你正在使用前10.0預輸入API。要使用遠程,你必須使用Bloodhound或類似的東西來獲取你的結果。

我最近實施了這一點,這裏是一個工作示例:

var $vartypeahead = $(yourjqueryelement); 
var engine = new Bloodhound({ 
    name: 'typeaheads', 
    remote: {"url":'/search/typeahead?q=%QUERY'}, 
    datumTokenizer: function(d) { return d;}, 
    queryTokenizer: function(d) { return d;} 
}); 
engine.initialize(); 

$vartypeahead.typeahead({ 
      "minLength": 2, 
      "highlight": true 
     }, 
     { 
      "source": engine.ttAdapter() 
      }); 

我確實有修改略高於從我所做的一切;我使用的前端骨幹和拼接以上進去(我在的是,預輸入項目PR)

問題#2

至於ES去,我不知道你有你的映射權,通常是你的預輸入項目映射將會是這個樣子:

{ 
    "settings": { 
    "analysis": { 
     "filter": { 
     "autocomplete_ngram": { 
      "max_gram": 24, 
      "min_gram": 2, 
      "type": "edge_ngram" 
     } 
     }, 
     "analyzer": { 
     "autocomplete_index": { 
      "filter": [ 
      "lowercase", 
      "autocomplete_ngram" 
      ], 
      "tokenizer": "keyword" 
     }, 
     "autocomplete_search": { 
      "filter": [ 
      "lowercase" 
      ], 
      "tokenizer": "keyword" 
     } 
     } 
    }, 
    "index": { 
     "number_of_shards": 20, 
     "number_of_replicas": 1 
    } 
    }, 
    "mappings": { 
    "yourtype": { 
     "properties": { 
     "title": { 
      "type": "multi_field", 
      "fields": { 
      "title_edgengram": { 
       "type": "string", 
       "index": "analyzed", 
       "index_analyzer": "autocomplete_index", 
       "search_analyzer": "autocomplete_search" 
      }, 
      "title": { 
       "type": "string", 
       "index": "not_analyzed" 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

我的彈性搜索映射確實如此。我指定它在其他地方,但我沒有發佈它 – user2158382

+2

謝謝!我複製你的代碼,它的工作原理。他們真的應該更新他們的文件以獲得更新的版本! – user2158382

+0

我一直在想念發動機,謝謝你這個 – lfender6445