groovy 如何翻译成java?

qybjjes1  于 2022-11-01  发布在  Java
关注(0)|答案(1)|浏览(292)

我是elasticsearch和java的新手,并试图在我的应用程序中实现与elasticsearch后端的自动完成。
以下是问题描述:
如果我用这样的上下文进行以下查询,它可以从kibana/elastic/dev控制台工作:

POST /autocompleteterms_v20/_search
 {
 "suggest": {
"completeMe": {
  "text": "spirit",
  "completion": {
    "field": "name_suggest",
    "contexts": {
      "lang": [ "en" ]
    }
  }
 }
}
}

但是当我在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)
        }
    }
  }
}

不成功,并收到以下错误:

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]

知道我做错了什么吗?

du7egjpx

du7egjpx1#

您可以尝试以下方法:

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);

相关问题