【问题标题】:Typeahead/Rails: Not workingTypeahead/Rails:不工作
【发布时间】:2014-03-05 23:30:20
【问题描述】:

我在我的 Rails 应用程序中使用 Elasticsearch 和 Typeahead 来执行自动完成。我从这里得到了这个想法

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

我已正确配置了 elasticsearch 自动完成功能,因为当我直接通过浏览器访问它时它可以工作。但是,当我尝试使用 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 

【问题讨论】:

  • 你使用的是什么版本的预输入
  • 我正在使用来自twitter.github.io/typeahead.js的v0.10.1
  • 请发布您要提前输入的字段的映射
  • 目前我的 JSON 格式如上所示。我希望预先输入以显示标记为text 的结果。但我也为我的 JSON 返回 test = ["maps", "make", "made", "maop", "make me burgers"] 尝试了以下格式
  • @JamesR 当 typeahead 方法运行 Uncaught Error: missing source 时,我也收到以下错误。我试过到处找,但找不到该错误的任何参考

标签: elasticsearch bootstrap-typeahead typeahead.js typeahead


【解决方案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 而言,我不确定你的映射是否正确,通常你的 typeahead 项目的映射看起来像这样:

{
  "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"
            }
          }
        }
      }
    }
  }
}

【讨论】:

  • 我的弹性搜索映射确实是这样的。但是我在其他地方指定了它,但我没有发布它
  • 谢谢!我复制了你的代码,它可以工作。他们真的应该为更新的版本更新他们的文档!
  • 您好 JamesR,我正在使用您的解决方案与 TypeAhead 0.11.1 和 Elastic 2.2.1。 Typeahead 确实会返回结果,但不是建议中的文本,而是显示一些 json,如下所示:dl.dropboxusercontent.com/u/10194489/TypeAhead-Elastic.png我在做什么错?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 2015-12-19
  • 1970-01-01
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多