typeahead.js在升级到Elasticsearch 6时中断

时间:2018-09-02 06:29:18

标签: elasticsearch typeahead.js elasticsearch-5

我有一个typeahead.js完成建议程序实现,可以在ES5索引上正常运行,并且在升级到ES6后就崩溃了。似乎存在多个问题:_suggest?查询已被放弃,转而使用_search,尽管我可以使用curl和Kibana进行成功的查询,但是我无法在typeahead Bloodhound()函数中表达它们。

这是来自Kibana的有效查询主体,可用于GET或POST:

GET places/_search
{
  "suggest": {
    "place-suggest": {
      "prefix": "den",
      "completion": {
        "field": "suggest"
      }
    }
  }
}

这是新版ES6的工作原理:

curl -XGET "http://localhost:9200/places/_search?pretty" \
  -H 'Content-Type: application/json' -d'{"suggest":{"place-suggest": \ 
  {"prefix":"den","completion" : {"field" : "suggest"}}}}'

这是在ES5中使用的旧Bloodhound():

var names = new Bloodhound({
  datumTokenizer: function(datum) {
    return Bloodhound.tokenizers.whitespace(datum.value);
  },
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: {
    wildcard: encodeURIComponent('%QUERY'),
    url: 'http://localhost:9200/places/_suggest?source=' +
        encodeURIComponent(
          '{"q":{"prefix":"%QUERY",
            "completion":{"field":"suggest"}}}'),

  transform: function(response) {
    return $.map(response.q[0].options, function(place) {
      return {
        id: place._source.id,
        data: place._source.is_conflation_of,
        value: place._source.representative_title,
        names: place._source.suggest
      };
    });
   }
  }

});

以及相应的预输入功能:

$('#bloodhound .typeahead').typeahead({
    hint: true, highlight: true, minLength: 3
  },
  {
    name: 'places', limit: 10, display: 'value', source: toponyms,
    templates: {
      empty: [
        '<div class="empty-message">','no matches','</div>'
        ].join('\n'),
      suggestion: template
    }
});

最后,我尝试使用新的Bloodhound()函数-出现500错误:

var toponyms = new Bloodhound({
  datumTokenizer: function(datum) {
     return Bloodhound.tokenizers.whitespace(datum.value); },
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: {
    wildcard: encodeURIComponent('%QUERY'),
    url: 'http://localhost:9200/linkedplaces/_search?source=' + encodeURIComponent(
     '{"suggest":{"place-suggest":{"prefix":"%QUERY","completion":{"field":"suggest"}}}}'
    ),
    transform: function(response) {
      return $.map(response.q[0].options, function(place) {
        return {
          ...
        };
      });
    }
  }
});

0 个答案:

没有答案