codelibs / elasticsearch-httpclient

Elasticsearch HTTP Rest Client
6 stars 2 forks source link

Not getting response in Elasticsearch suggestion in elasticsearch7.2.1 version #17

Open GeetaLakhwani-1 opened 4 years ago

GeetaLakhwani-1 commented 4 years ago

Hi, Trying to add ElasticSearch Suggestion in my response and implementing it using SearchRequestBuilder as:-

SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(3m);

Query is correct as I tried to run in Kibana

{"from":0,"size":10,"timeout":"10000ms","query":{"bool":{"must":[{"function_score":{"query":{"bool":{"should":[{"match_phrase":{"title":{"query":"elastic","slop":0,"zero_terms_query":"NONE","boost":0.01}}},{"match_phrase":{"content":{"query":"elastic","slop":0,"zero_terms_query":"NONE","boost":0.005}}},{"match_phrase":{"filename":{"query":"elastic","slop":0,"zero_terms_query":"NONE","boost":0.01}}},{"match_phrase":{"all_values":{"query":"elastic","slop":0,"zero_terms_query":"NONE","boost":0.005}}},{"match_phrase":{"title_en":{"query":"elastic","slop":0,"zero_terms_query":"NONE","boost":1.0}}},{"match_phrase":{"content_en":{"query":"elastic","slop":0,"zero_terms_query":"NONE","boost":0.5}}}],"adjust_pure_negative":true,"boost":1.0}},"functions":[{"filter":{"match_all":{"boost":1.0}},"field_value_factor":{"field":"boost","factor":1.0,"modifier":"none"}},{"filter":{"match_all":{"boost":1.0}},"field_value_factor":{"field":"click_count","factor":1.0,"modifier":"none"}}],"score_mode":"sum","max_boost":1.5,"boost":1.0}}],"filter":[{"bool":{"must_not":[{"term":{"role":{"value":"DRguest","boost":1.0}}},{"term":{"role":{"value":"D1admin","boost":1.0}}},{"term":{"role":{"value":"DRadmin","boost":1.0}}}],"should":[{"term":{"role":{"value":"Rguest","boost":1.0}}},{"term":{"role":{"value":"1admin","boost":1.0}}},{"term":{"role":{"value":"Radmin","boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}}],"adjust_pure_negative":true,"boost":1.0}},"_source":{"includes":["score","_id","doc_id","boost","content_length","host","site","last_modified","timestamp","mimetype","filetype","filename","created","title","digest","url","thumbnail","click_count","favorite_count","config_id","lang","has_cache","filename","url","digest","doc_id","title"],"excludes":[]},"track_total_hits":10000,"aggregations":{"field:bGFiZWw=":{"terms":{"field":"label","size":100,"min_doc_count":1,"shard_min_doc_count":0,"show_term_doc_count_error":false,"order":[{"_count":"desc"},{"_key":"asc"}]}},"query:dGltZXN0YW1wOltub3cvZC0xZCBUTyAqXQ==":{"filter":{"range":{"timestamp":{"from":"now/d-1d","to":null,"include_lower":true,"include_upper":true,"boost":1.0}}}},"query:dGltZXN0YW1wOltub3cvZC03ZCBUTyAqXQ==":{"filter":{"range":{"timestamp":{"from":"now/d-7d","to":null,"include_lower":true,"include_upper":true,"boost":1.0}}}},"query:dGltZXN0YW1wOltub3cvZC0xTSBUTyAqXQ==":{"filter":{"range":{"timestamp":{"from":"now/d-1M","to":null,"include_lower":true,"include_upper":true,"boost":1.0}}}},"query:dGltZXN0YW1wOltub3cvZC0xeSBUTyAqXQ==":{"filter":{"range":{"timestamp":{"from":"now/d-1y","to":null,"include_lower":true,"include_upper":true,"boost":1.0}}}}},"highlight":{"fields":{"content":{"fragment_size":60,"number_of_fragments":2,"type":"fvh"}}},
"suggest":{"suggest_user":{"text":"elastic","term":{"field":"title","suggest_mode":"MISSING","accuracy":0.5,"sort":"SCORE","string_distance":"INTERNAL","max_edits":2,"max_inspections":5,"max_term_freq":0.01,"prefix_length":1,"min_word_length":4,"min_doc_freq":0.0}}}}

I tried to implement suggestion feature with the help of below code as in elasticsearch version 7.2:-

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SuggestionBuilder termSuggestionBuilder =SuggestBuilders.termSuggestion("user").text("elast"); 
SuggestBuilder suggestBuilder = new SuggestBuilder();
suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder); 
searchSourceBuilder.suggest(suggestBuilder);

When I tried to retrieve the sugestions with the help of TermSuggestion then not getting proper response and getting below exception:-

ElasticsearchStatusException[{"took":164,"timed_out":false,"_shards":{"total":6,"successful":6,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":0.43710136,"hits":[{"_index":"nero.default.20191204","_type":"_doc","_id":"77db761c336df2368f06ea21404dab6ea14cac38b94bdbdd59da746ca52e6b8f9e715ab57afcecf1fd0a84a7cc480594231cfefa6a00a0809bef27ccde764a26","_score":0.43710136,"_source":{"filetype":"html","thumbnail":"https://www.elastic.co/static-res/images/elastic-logo-200.png","created":"2019-12-05T06:51:41.356Z","click_count":0,"title":"Open Source Search: The Creators of Elasticsearch, ELK Stack & Kibana | Elastic","doc_id":"833795e12339437c9515e6d55b1c290b","url":"https://www.elastic.co/","site":"www.elastic.co/","config_id":"Wcjxw0G4BzlTtvS7CtqfT","has_cache":"true","host":"www.elastic.co","digest":"We're the creators of the Elastic (ELK) Stack -- Elasticsearch, Kibana, Beats, and Logstash. Securely and reliably search, analyze, and visualize your data in the cloud or on-prem.","boost":"1.0","favorite_count":0,"mimetype":"text/html","lang":"en","content_length":"2077723","timestamp":"2019-12-05T06:51:41.356Z"},"highlight":{"content":["the latest and greatest from <em>Elastic</em>. MarketoFEForm"]}}]},"aggregations":{"filter#query:dGltZXN0YW1wOltub3cvZC0xTSBUTyAqXQ==":{"meta":{},"doc_count":1},"filter#query:dGltZXN0YW1wOltub3cvZC03ZCBUTyAqXQ==":{"meta":{},"doc_count":1},"sterms#field:bGFiZWw=":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[]},"filter#query:dGltZXN0YW1wOltub3cvZC0xeSBUTyAqXQ==":{"meta":{},"doc_count":1},"filter#query:dGltZXN0YW1wOltub3cvZC0xZCBUTyAqXQ==":{"meta":{},"doc_count":1}},"suggest":{"term#suggest_user":[{"text":"elast","offset":0,"length":7,"options":[]}]}}]; nested: NamedObjectNotFoundException[unknown named object category [org.elasticsearch.search.suggest.Suggest$Suggestion]];

at org.codelibs.elasticsearch.client.action.HttpAction.toElasticsearchException(HttpAction.java:143)
at org.codelibs.elasticsearch.client.action.HttpSearchAction.lambda$execute$0(HttpSearchAction.java:53)
at org.codelibs.curl.CurlRequest.lambda$execute$4(CurlRequest.java:202)
at org.codelibs.curl.CurlRequest.lambda$connect$3(CurlRequest.java:182)
at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1426)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)
Suppressed: org.elasticsearch.common.xcontent.NamedObjectNotFoundException: unknown named object category [org.elasticsearch.search.suggest.Suggest$Suggestion]
at org.elasticsearch.common.xcontent.NamedXContentRegistry.parseNamedObject(NamedXContentRegistry.java:128)
at org.elasticsearch.common.xcontent.support.AbstractXContentParser.namedObject(AbstractXContentParser.java:415)
at org.elasticsearch.common.xcontent.XContentParserUtils.parseTypedKeysObject(XContentParserUtils.java:153)
at org.elasticsearch.search.suggest.Suggest$Suggestion.fromXContent(Suggest.java:436)
at org.elasticsearch.search.suggest.Suggest.fromXContent(Suggest.java:192)
at org.elasticsearch.action.search.SearchResponse.innerFromXContent(SearchResponse.java:294)
at org.elasticsearch.action.search.SearchResponse.fromXContent(SearchResponse.java:251)
at org.codelibs.elasticsearch.client.action.HttpSearchAction.lambda$execute$0(HttpSearchAction.java:46)
... 8 more
Suppressed: ParsingException[Failed to parse object: expecting field with name [error] but found [took]]
at org.elasticsearch.common.xcontent.XContentParserUtils.ensureFieldName(XContentParserUtils.java:50)
at org.elasticsearch.ElasticsearchException.failureFromXContent(ElasticsearchException.java:587)
at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:169)at org.codelibs.elasticsearch.client.action.HttpAction.toElasticsearchException(HttpAction.java:138)
 [CIRCULAR REFERENCE:org.elasticsearch.common.xcontent.NamedObjectNotFoundException: unknown named object category [org.elasticsearch.search.suggest.Suggest$Suggestion]]

==============================================================

As I debugged this and found that registry content for suggestion from elasticsearch-httpclient-7.2.1.jar -> org.codelibs.elasticsearch.client.HttpClient -> getDefaultNamedXContents() is commented.

 //        entries.add(new NamedXContentRegistry.Entry(Suggest.Suggestion.class, new ParseField(TermSuggestion.NAME),
        //                (parser, context) -> TermSuggestion.fromXContent(parser, (String) context)));
        //        entries.add(new NamedXContentRegistry.Entry(Suggest.Suggestion.class, new ParseField(PhraseSuggestion.NAME),
        //                (parser, context) -> PhraseSuggestion.fromXContent(parser, (String) context)));
        //        entries.add(new NamedXContentRegistry.Entry(Suggest.Suggestion.class, new ParseField(CompletionSuggestion.NAME),
        //  (parser, context) -> CompletionSuggestion.fromXContent(parser, (String) context)));

Could you please advice why these lines are commented ? Is any other jar for elasticsearch suggestion? Could someone please help for the same.

marevol commented 4 years ago

It's not implemented yet. We welcome any PR.

GeetaLakhwani-1 commented 4 years ago

Thank you for your response. Could you please let me know why this code has commented in elasticsearch-httpclient-7.2.1.jar as I asked in above comment // entries.add(new NamedXContentRegistry.Entry(Suggest.Suggestion.class, new ParseField(TermSuggestion.NAME), // (parser, context) -> TermSuggestion.fromXContent(parser, (String) context))); // entries.add(new NamedXContentRegistry.Entry(Suggest.Suggestion.class, new ParseField(PhraseSuggestion.NAME), // (parser, context) -> PhraseSuggestion.fromXContent(parser, (String) context))); // entries.add(new NamedXContentRegistry.Entry(Suggest.Suggestion.class, new ParseField(CompletionSuggestion.NAME), // (parser, context) -> CompletionSuggestion.fromXContent(parser, (String) context)));

marevol commented 4 years ago

Suggest API is not implemented in this library.

GeetaLakhwani-1 commented 4 years ago

Thank you for your response. Please let me know once it will be implemented