Trying to use _analyze through JAVA API but exception thrown

Hi!

I'm currently using the Elastic Cloud Service and trying to use the _analyze function of the High-Level Restful Java API.

When I set my analyzer and the to-be analyzed text, my Java code works fine. However, when trying to set the .explain(true) to my AnalyzeRequest matching the Java code to use the .detail() function of the AnalyzeResponse Object, I keep getting errors thrown.

My .printstacktrace():

java.io.IOException: Unable to parse response body for Response{requestLine=GET /rdb/_analyze HTTP/1.1, host=https://92d5f385db294fb4b7ff335201d0a854.asia-northeast1.gcp.cloud.es.io:9243, response=HTTP/1.1 200 OK}
	at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1630)
	at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1596)
	at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1563)
	at org.elasticsearch.client.IndicesClient.analyze(IndicesClient.java:1573)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
Caused by: org.elasticsearch.common.xcontent.XContentParseException: [1:1362] [analyze_response] failed to parse field [detail]
	at org.elasticsearch.common.xcontent.ObjectParser.parseValue(ObjectParser.java:531)
	at org.elasticsearch.common.xcontent.ObjectParser.parseSub(ObjectParser.java:541)
	at org.elasticsearch.common.xcontent.ObjectParser.parse(ObjectParser.java:324)
	at org.elasticsearch.common.xcontent.ConstructingObjectParser.parse(ConstructingObjectParser.java:171)
	at org.elasticsearch.client.indices.AnalyzeResponse.fromXContent(AnalyzeResponse.java:166)
	at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1897)
	at org.elasticsearch.client.RestHighLevelClient.lambda$performRequestAndParseEntity$9(RestHighLevelClient.java:1564)
	at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1628)
	... 47 more
Caused by: org.elasticsearch.common.xcontent.XContentParseException: [1:1362] [detail] failed to parse field [tokenfilters]
	at org.elasticsearch.common.xcontent.ObjectParser.parseValue(ObjectParser.java:531)
	at org.elasticsearch.common.xcontent.ObjectParser.parseArray(ObjectParser.java:523)
	at org.elasticsearch.common.xcontent.ObjectParser.parseSub(ObjectParser.java:555)
	at org.elasticsearch.common.xcontent.ObjectParser.parse(ObjectParser.java:324)
	at org.elasticsearch.common.xcontent.ConstructingObjectParser.parse(ConstructingObjectParser.java:171)
	at org.elasticsearch.common.xcontent.AbstractObjectParser.lambda$declareObject$1(AbstractObjectParser.java:169)
	at org.elasticsearch.common.xcontent.ObjectParser.lambda$declareField$9(ObjectParser.java:386)
	at org.elasticsearch.common.xcontent.ObjectParser.parseValue(ObjectParser.java:529)
	... 54 more
Caused by: org.elasticsearch.common.xcontent.XContentParseException: [1:1362] [token_list] failed to parse field [tokens]
	at org.elasticsearch.common.xcontent.ObjectParser.parseValue(ObjectParser.java:531)
	at org.elasticsearch.common.xcontent.ObjectParser.parseArray(ObjectParser.java:523)
	at org.elasticsearch.common.xcontent.ObjectParser.parseSub(ObjectParser.java:555)
	at org.elasticsearch.common.xcontent.ObjectParser.parse(ObjectParser.java:324)
	at org.elasticsearch.common.xcontent.ConstructingObjectParser.parse(ConstructingObjectParser.java:171)
	at org.elasticsearch.common.xcontent.AbstractObjectParser.lambda$declareObjectArray$11(AbstractObjectParser.java:240)
	at org.elasticsearch.common.xcontent.AbstractObjectParser.lambda$declareFieldArray$20(AbstractObjectParser.java:286)
	at org.elasticsearch.common.xcontent.AbstractObjectParser.parseArray(AbstractObjectParser.java:368)
	at org.elasticsearch.common.xcontent.AbstractObjectParser.lambda$declareFieldArray$21(AbstractObjectParser.java:286)
	at org.elasticsearch.common.xcontent.ObjectParser.lambda$declareField$9(ObjectParser.java:386)
	at org.elasticsearch.common.xcontent.ObjectParser.parseValue(ObjectParser.java:529)
	... 61 more
Caused by: com.fasterxml.jackson.core.JsonParseException: Duplicate field 'positionLength'
 at [Source: (org.apache.http.nio.entity.ContentInputStream); line: 1, column: 1389]
	at com.fasterxml.jackson.core.json.JsonReadContext._checkDup(JsonReadContext.java:204)
	at com.fasterxml.jackson.core.json.JsonReadContext.setCurrentName(JsonReadContext.java:198)
	at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:762)
	at org.elasticsearch.common.xcontent.json.JsonXContentParser.nextToken(JsonXContentParser.java:52)
	at org.elasticsearch.common.xcontent.ObjectParser.parse(ObjectParser.java:287)
	at org.elasticsearch.common.xcontent.ObjectParser.parse(ObjectParser.java:256)
	at org.elasticsearch.client.indices.AnalyzeResponse$AnalyzeToken.fromXContent(AnalyzeResponse.java:136)
	at org.elasticsearch.client.indices.DetailAnalyzeResponse$AnalyzeTokenList.lambda$static$1(DetailAnalyzeResponse.java:152)
	at org.elasticsearch.common.xcontent.AbstractObjectParser.lambda$declareObjectArray$11(AbstractObjectParser.java:240)
	at org.elasticsearch.common.xcontent.AbstractObjectParser.lambda$declareFieldArray$20(AbstractObjectParser.java:286)
	at org.elasticsearch.common.xcontent.AbstractObjectParser.parseArray(AbstractObjectParser.java:368)
	at org.elasticsearch.common.xcontent.AbstractObjectParser.lambda$declareFieldArray$21(AbstractObjectParser.java:286)
	at org.elasticsearch.common.xcontent.ObjectParser.lambda$declareField$9(ObjectParser.java:386)
	at org.elasticsearch.common.xcontent.ObjectParser.parseValue(ObjectParser.java:529)
	... 71 more

And here is m Java code:

String ELASTIC_SEARCH_USER_NAME = "elastic";
        String ELASTIC_SEARCH_PASSWORD = <password>;
        String ELASTIC_SEARCH_ENDPOINT_URL = "92d5f385db294fb4b7ff335201d0a854.asia-northeast1.gcp.cloud.es.io";
        Integer ELASTIC_SEARCH_PORT = 9243;

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(ELASTIC_SEARCH_USER_NAME, ELASTIC_SEARCH_PASSWORD));

        RestClientBuilder builder = RestClient.builder(new HttpHost(ELASTIC_SEARCH_ENDPOINT_URL, ELASTIC_SEARCH_PORT, "https"))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                });

        String INDEX_NAME = "rdb";

        String TYPE_NAME = "_doc";

        RestHighLevelClient highLevelClient = new RestHighLevelClient(builder);

        String CUSTOM_ANALYZER_DIRECT_INPUT = "analyzer";

try {
            AnalyzeRequest request = AnalyzeRequest.withIndexAnalyzer(INDEX_NAME, CUSTOM_ANALYZER_DIRECT_INPUT, text);
            request.explain(true);

            AnalyzeResponse response = highLevelClient.indices().analyze(request, RequestOptions.DEFAULT);          

            List<Map<String, Object>> analyzeList = new ArrayList<>();

            DetailAnalyzeResponse detail = response.detail();

            
            if (detail.tokenizer() != null) {
                DetailAnalyzeResponse.AnalyzeTokenList tokenizer = detail.tokenizer();

                if (tokenizer.getTokens().length != 0) {
                    AnalyzeResponse.AnalyzeToken[] analyzeTokenizerTokens = tokenizer.getTokens();
                    if (analyzeTokenizerTokens.length != 0) {
                        for (int i = 0; i < analyzeTokenizerTokens.length; i++) {
                            Map<String, Object> tokenMap = analyzeTokenizerTokens[i].getAttributes();
                            String token = analyzeTokenizerTokens[i].getTerm();
                            String type = analyzeTokenizerTokens[i].getType();
                            tokenMap.put("name", tokenizer.getName());
                            tokenMap.put("token", token);
                            tokenMap.put("type", type);
                            String posType = (String) tokenMap.get("posType");
                            String leftPOS = (String) tokenMap.get("leftPOS");
                            String rightPOS = (String) tokenMap.get("rightPOS");
                            analyzeList.add(tokenMap);
                        }
                    }
                }
            }

            if (detail.tokenfilters().length != 0) {
                DetailAnalyzeResponse.AnalyzeTokenList[] tokenFilters = detail.tokenfilters();
                if (tokenFilters.length != 0) {
                    for (int i = 0; i < tokenFilters.length; i++) {
                        String tokenFilterName = tokenFilters[i].getName();

                        AnalyzeResponse.AnalyzeToken[] analyzeTokens = tokenFilters[i].getTokens();
                        if (analyzeTokens.length != 0) {
                            for (int j = 0; j < analyzeTokens.length; j++) {
                                Map<String, Object> tokenMap = analyzeTokens[j].getAttributes();
                                String token = analyzeTokens[j].getTerm();
                                String type = analyzeTokens[j].getType();
                                tokenMap.put("name", tokenFilterName);
                                tokenMap.put("token", token);
                                tokenMap.put("type", type);
                                String posType = (String) tokenMap.get("posType");
                                String leftPOS = (String) tokenMap.get("leftPOS");
                                String rightPOS = (String) tokenMap.get("rightPOS");
                                analyzeList.add(tokenMap);
                            }
                    }
                }
            }

            model.addAttribute("analyzeList", analyzeList);
        } catch (Exception e) {
            e.printStackTrace();
        }

highLevelClient.close();

I removed a bit of unrelated stacktrace lines to fit the post's character limit.
The java.io.IOException does point that

AnalyzeResponse response = highLevelClient.indices().analyze(request, RequestOptions.DEFAULT);

this line is where the Exception is thrown in my code.

I modified a bit of the Java code so there might not be some matching syntax (braces).

I tried to look up how to properly use the .detail() function on Elastic's homepage but could not find any related pages and I tried searching everywhere (literally there are at least 50 purple links in my related google searches) but there was almost no meaningful pages on how to properly use the .detail() function.

I've been at this problem for hours any any help is much appreciated.

Thank you!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.