使用Java API的ElasticSearch完成建议器 [英] ElasticSearch completion suggester with Java API

查看:486
本文介绍了使用Java API的ElasticSearch完成建议器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上尝试了一些关于ElasticSearch的建议功能的示例代码,但我无法解决我对自动完成解决方案的问题

I had tried a few example codes on suggester feature of ElasticSearch on the net but I couldn't solve my problem against the autocomplete solution

我的索引:

client.prepareIndex("kodcucom", "article", "1")
      .setSource(putJsonDocument("ElasticSearch: Java",
        "ElasticSeach provides Java API, thus it executes all operations " +
        "asynchronously by using client object..",
         new Date(),
         new String[]{"elasticsearch"},
         "Hüseyin Akdoğan")).execute().actionGet();

我使用了suggestbuilder获取关键字然后浏览内容字段,这里是由于没有结果而发生空指针异常

and I used suggestbuilder to obtain the keyword then scan through the content "field", and here is where the null pointer exception occurs due to no result

CompletionSuggestionBuilder skillNameSuggest = new CompletionSuggestionBuilder("skillNameSuggest");

skillNameSuggest.text("lien");
skillNameSuggest.field("content");

SuggestRequestBuilder suggestRequestBuilder = client.prepareSuggest("kodcucom").addSuggestion(skillNameSuggest);

SuggestResponse suggestResponse = suggestRequestBuilder.execute().actionGet();

Iterator<? extends Suggest.Suggestion.Entry.Option> iterator =
          suggestResponse.getSuggest().getSuggestion("skillNameSuggest").iterator().next().getOptions().iterator();

我是否缺少一些过滤器或输入条件才能获得结果?任何结果都可以,例如自动填充或找到记录。

Am I missing some filters or input criteria in order to get result? Any result should ok such as autocomplete or record found.

编辑1:

这是我获得NPE的地方,我可以看到任何结果都没有从调试模式返回 suggestResponse

This is where I got the NPE and I could see that none of any result return at suggestResponse from debug mode

Iterator<? extends Suggest.Suggestion.Entry.Option> iterator =
              suggestResponse.getSuggest().getSuggestion("skillNameSuggest").iterator().next().getOptions().iterator();

编辑2:
我使用的是2.1.1版本ElasticSearch Java API

EDIT 2: I am using 2.1.1 version of ElasticSearch Java API

编辑3:
我试图将迭代器行拆分成几个代码块,NPE发生在将一组数据转换为迭代器时的最后一行,但没有多少帮助

EDIT 3: I tried in splitting up the iterator line into several code blocks, the NPE occur at the last line when converting a set of data into iterator, but there is not much helping

Suggest tempSuggest = suggestResponse.getSuggest();

Suggestion tempSuggestion = tempSuggest.getSuggestion("skillNameSuggest");

Iterator tempIterator = tempSuggestion.iterator();

我看到代码:

SuggestRequestBuilder suggestRequestBuilder = client.prepareSuggest("kodcucom").addSuggestion(skillNameSuggest);

    SuggestResponse suggestResponse = suggestRequestBuilder.execute().actionGet();

已包含空数组/数据集,我是否错误地使用了建议请求构建器?

has already consists a empty array/dataset, am I using the suggest request builder incorrectly?

推荐答案

为了使用完成功能,您需要专用一个字段,这将被称为完成,您必须指定一个特殊的映射它。

In order to use completion feature, you need to dedicate one field, which will be called completion and you have to specify a special mapping for it.

例如:

"mappings": {
   "article": {
     "properties": {
      "content": {
        "type": "string"
      },
     "completion_suggest": {
      "type": "completion"}
     }
   }
}

completion_suggest字段是我们将用于上述代码示例中的自动完成功能的字段。在映射定义之后,数据必须索引如下:

The completion_suggest field is the field we will use for the autocomplete function in the above code sample. After this mapping defination, the data must be indexing as follow:

curl -XPOST localhost:9200/kodcucom/article/1 -d '{
   "content": "elasticsearch",
   "completion_suggest": {
     "input": [ "es", "elastic", "elasticsearch" ],
     "output": "ElasticSearch"
   }
}'

然后是Java API可以按如下方式用于获取建议:

Then Java API can be used as follows for get suggestions:

        CompletionSuggestionBuilder skillNameSuggest  = new CompletionSuggestionBuilder("complete");
        skillNameSuggest.text("es");
        skillNameSuggest.field("completion_suggest");

        SearchResponse searchResponse = client.prepareSearch("kodcucom")
                .setTypes("article")
                .setQuery(QueryBuilders.matchAllQuery())
                .addSuggestion(skillNameSuggest)
                .execute().actionGet();

        CompletionSuggestion compSuggestion = searchResponse.getSuggest().getSuggestion("complete");

        List<CompletionSuggestion.Entry> entryList = compSuggestion.getEntries();
        if(entryList != null) {
            CompletionSuggestion.Entry entry = entryList.get(0);
            List<CompletionSuggestion.Entry.Option> options =entry.getOptions();
            if(options != null)  {
                CompletionSuggestion.Entry.Option option = options.get(0);
                System.out.println(option.getText().string());
            }
        }

这篇关于使用Java API的ElasticSearch完成建议器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆