使用JAVA API从Elastic Search建议搜索响应中提取源数据 [英] Extract the Source data from Elastic Search Suggest Search response using JAVA API

查看:250
本文介绍了使用JAVA API从Elastic Search建议搜索响应中提取源数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java的自动完成应用程序的Completition提示器,我能够使用JAVA api从Search响应中提取建议文本。在检查原始响应时,我看到建议响应包含_source数据(完整的文档,而不仅仅是建议字符串)。如何从建议搜索响应中提取源数据?

I am using the Completition suggester for an auto complete App in Java, I was able to extract the suggest text from the Search response using the JAVA api. While checking the raw response I saw that suggest response contain the _source data (complete document instead of just the Suggest string). How to extract the source data from the Suggest Search response ?

请在下面我用来获取建议文本的代码下查找-

Please find below the Code I have used to get the suggested text -

SearchRequest searchRequest = new SearchRequest("my_entitiy");
CompletionSuggestionBuilder suggestionBuilder = new CompletionSuggestionBuilder("nameSuggest");
suggestionBuilder.size(10).prefix(input).skipDuplicates(true);

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.suggest(
        new SuggestBuilder().addSuggestion(SUGGESTION_NAME, suggestionBuilder));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = elasticClient.search(searchRequest, RequestOptions.DEFAULT);

Suggest suggest = searchResponse.getSuggest();
Suggest.Suggestion<Suggest.Suggestion.Entry<Suggest.Suggestion.Entry.Option>> suggesition =
        suggest.getSuggestion(SUGGESTION_NAME);
List<String> suggestionList =  new ArrayList<>();
for (Suggest.Suggestion.Entry<Suggest.Suggestion.Entry.Option> entry : suggesition.getEntries()) {
  for(Suggest.Suggestion.Entry.Option option:entry.getOptions()){
    suggestionList.add(option.getText().toString());
  }
}

在选项中,很少有方法可以提取分数,文字和突出显示。是否可以从该选项获取_source数据?我看到了 toXContent 函数是否可以使用它来获取源数据?

In the Option few methods are available to extract the score, text and highlighted. Is it possible to get the _source data from the option ? I saw a toXContent function is it possible to use that to get the source data ?

以上代码段将建议字符串保存到列表中,我想知道是否有可能获得完整的Doc JSON。

Above snippet is saving the Suggested string to a list I was wondering whether it's possible to get the complete Doc JSON.

推荐答案

您始终可以使用源过滤来过滤要在搜索结果中返回的字段。在弹性中,您可以在 _source includes excludes 或两者$ c>上下文。例如您只想获得 field1 field2 ,则可以设置 _source 以及查询:

You can always make use of source filtering to filter the fields to be returned in search results. In elastic you can do so adding includes, excludes or both in _source context. For e.g. you want only to get field1 and field2, the you can set the _source as below along with query:

{
  "query":{
    // your query goes here
  },
  "_source":{
    "includes":["field1", "field2"]
  }
}

使用高级休息客户可以达到以下效果:

Using high level rest client the same can be achieved as below:

String[] includes = {"field1", "field2"};
searchSourceBuilder.fetchSource(new FetchSourceContext(true, includes, null));

这篇关于使用JAVA API从Elastic Search建议搜索响应中提取源数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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