Typeahead / Rails:不工作

时间:2014-03-05 23:30:20

标签: elasticsearch bootstrap-typeahead typeahead.js typeahead

我正在使用Elasticsearch&在我的Rails应用程序中预先输入以执行自动完成。我从这里得到了这个想法

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

我正确配置了elasticsearch autocomplete,因为当我通过浏览器直接访问它时,它可以正常工作。但是,当我尝试使用typeahead从自动完成查询调用显示数据时,它甚至不会在我的调试器中触发。这是我的表格& javascript其中typeahead被称为

表格

<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
                }
            ]
        }
    ]
}

修改 每当typeahead运行时,我也注意到控制台中出现以下错误

Uncaught Error: missing source 

1 个答案:

答案 0 :(得分:6)

好的,我认为你有两个问题。

问题1:

您认为我正在使用10.0之前的typeahead 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()
          });

我确实必须从我的工作中略微修改上述内容;我在前端使用骨干并将上面的内容拼接到其中(我在typeahead项目中有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"
            }
          }
        }
      }
    }
  }
}