elasticsearch的新手,正在研究术语建议和短语建议功能。我创建了以下脚本,并在kibana中执行了该脚本,它工作得非常好。我想使用Java API来实现相同的功能。
请在下面找到我的脚本。
get product/_search
{
"suggest" : {
"autocomplete" : {
"text" : "pressure",
"term" : {
"field" : "product_attr_value",
"suggest_mode" : "popular"
}
}
},
"size" : 0
}
是将ES 6.2.3版本与RestHighLevel客户端一起使用。
更新:1
已经形成了uggestinationBuilder查询,并且还能够从查询中获取响应。但是我如何在列表中设置这些响应。从下面的代码中,尝试访问来自SearchHit
的响应,并尝试在List中进行设置,但是在打印列表大小时,其结果为0
。
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SuggestionBuilder termSuggestionBuilder =
SuggestBuilders.termSuggestion("product_attr_value").text("valv").suggestMode(SuggestMode.ALWAYS);
SuggestBuilder suggestBuilder = new SuggestBuilder();
suggestBuilder.addSuggestion("suggest_product", termSuggestionBuilder);
searchSourceBuilder.suggest(suggestBuilder);
searchSourceBuilder.size(10);
suggestionRequest.source(searchSourceBuilder);
try
{
searchResponse = SearchEngineClient.getInstance().search(suggestionRequest);
}
catch (IOException e)
{
e.getLocalizedMessage();
}
SearchHit[] searchHits1 = searchResponse.getHits().getHits();
long totalHits = searchResponse.getHits().totalHits;
System.out.println("Keyword Search Hits....---->"+totalHits);
List<Suggestion> suggestionTermList=new ArrayList<Suggestion>();
Map<String, Object> sourceAsMap = null;
for (SearchHit hit : searchHits1)
{
sourceAsMap = hit.getSourceAsMap();
suggestions = new Suggestion();
suggestions.setSuggestionString(String.valueOf(sourceAsMap.get("text")));
suggestionTermList.add(suggestions);
}
System.out.println("suggestion response --->"+searchResponse.toString());
System.out.println("Suggestion List Size --->"+suggestionTermList.size());
请在下面找到我的回复:
{"took":6,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":0.0,"hits":[]},"suggest":{"suggest_product":[{"text":"valv","offset":0,"length":4,"options":[{"text":"valve","score":0.75,"freq":15218},{"text":"val,","score":0.75,"freq":169},{"text":"valvw","score":0.75,"freq":2},{"text":"val","score":0.6666666,"freq":1532},{"text":"vlv","score":0.6666666,"freq":1145}]}]}}