I am new to elasticsearch and java and attempting to implement autocomplition with elasticsearch backend in my application.
Here is the problem description:
if I make following query with the context like this it works from the kibana/elastic/dev console:
POST /autocompleteterms_v20/_search
{
"suggest": {
"completeMe": {
"text": "spirit",
"completion": {
"field": "name_suggest",
"contexts": {
"lang": [ "en" ]
}
}
}
}
}
But when I do it from java:
SuggestBuilder sb = new SuggestBuilder()
LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
map.put(prefLang, ContextBuilder.category("lang").field("lang").build())
sb.addSuggestion("completeMe", SuggestBuilders.completionSuggestion("name_suggest" ).text(term).contexts(map))
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
sourceBuilder.query(query)
sourceBuilder.suggest(sb)
SearchRequest searchRequest = new SearchRequest()
searchRequest.source(sourceBuilder)
searchRequest.indices(AUTOCOMPLETE_TERMS_ALIAS "_v4")
SearchRequest request = new SearchRequest()
request.source(sourceBuilder)
SearchResponse suggestResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT)
int count = 5
if (suggestResponse.getHits().size() > 0) {
for (Suggest.Suggestion.Entry.Option next : suggestResponse.getSuggest().getSuggestion("completeMe").iterator().next().getOptions()) {
if (count < 0) break
count--
def text = next.getText().string()
result.getOptions().add(new AutocompleteResult.SuggestOption(text))
if (log.isTraceEnabled()) {
log.trace("Got suggestion: {}", text)
}
}
}
}
Is not successful and I receive the following error:
org.codehaus.groovy.runtime.metaclass.MethodSelectionException: Could not find which method
<init>() to invoke from this list:
public org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder#<init>
(org.elasticsearch.common.io.stream.StreamInput)
private org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder#<init>
(java.lang.String, org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)
groovy.lang.MissingPropertyException: No such property: ContextBuilder for class:
org.proj.myproj.content.index.elasticsearch.ElasticSearchService
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:65) ~
[groovy-3.0.9.jar:3.0.9]
at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:87) ~[groovy-3.0.9.jar:3.0.9]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:341) ~[groovy-3.0.9.jar:3.0.9]
at org.proj.myproj.content.index.elasticsearch.ElasticSearchService.getSuggestions(ElasticSearchService:291) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.codehaus.groovy.runtime.callsite.PlainObjectMetaMethodSite.doInvoke(PlainObjectMetaMethodSite.java:43) ~[groovy-3.0.9.jar:3.0.9]
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:193) ~[groovy-3.0.9.jar:3.0.9]
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:61) ~[groovy-3.0.9.jar:3.0.9]
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51) ~[groovy-3.0.9.jar:3.0.9]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:171) ~[groovy-3.0.9.jar:3.0.9]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:203) ~[groovy-3.0.9.jar:3.0.9]
at org.proj.myproj.content.index.elasticsearch.ElasticSearchService.get(ElasticSearchService:153) ~[na:na]
at org.proj.myproj.controller.SearchController.autocomplete(SearchController.java:85) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
Any idea what I do wrong?
CodePudding user response:
You can try this way:
SuggestBuilder sb = new SuggestBuilder();
List<CategoryQueryContext> ctx = new ArrayList<>();
ctx.add(CategoryQueryContext.builder().setCategory("en").build());
Map<String, List<? extends ToXContent>> map = Collections.singletonMap("lang", ctx);
sb.addSuggestion("completeMe", SuggestBuilders.completionSuggestion("name_suggest").text(term).contexts(map));
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(query);
sourceBuilder.suggest(sb);
SearchRequest searchRequest = new SearchRequest();
searchRequest.source(sourceBuilder);
searchRequest.indices(AUTOCOMPLETE_TERMS_ALIAS "_v4");
SearchRequest request = new SearchRequest();
request.source(sourceBuilder);
SearchResponse suggestResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);