ElasticSearch 5 adding context to SuggestionBuilders

末鹿安然 提交于 2019-12-23 03:49:06

问题


I am working on ES5 through java, and am trying to add context to a CompletionSuggestionBuilder. I have a map of String objects that need to be added. The code I have so far is -

Map<String, String> context = ...
CompletionSuggestionBuilder csb = SuggestBuilders.completionSuggestion(field).text(value).size(count);

How do I add context objects to csb? I think the method to use is -

csb.contexts(Map<String, List<? extends ToXContent>> queryContexts)

But I don't know how to get from my map to the map to pass as arguments to the contexts method.


回答1:


You can create Map<String, List<? extends ToXContent>> like this;

Collections.singletonMap("cat", Arrays.asList(CategoryQueryContext.builder().setCategory("cat0").setBoost(3).build(), CategoryQueryContext.builder().setCategory("cat1").build()))

I think currently supported types that extend ToXContext are CategoryQueryContext and GeoQueryContext

The strange thing here is that if I create a local variable and pass it to the contexts it does not work. So, I just passed it directly to the contexts it does work.

Full example would be like this:

CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("cat", Arrays.asList(CategoryQueryContext.builder().setCategory("cat0").setBoost(3).build(), CategoryQueryContext.builder().setCategory("cat1").build())));

It is all written in their test cases. You can take a look at it: https://github.com/elastic/elasticsearch/blob/master/core/src/test/java/org/elasticsearch/search/suggest/ContextCompletionSuggestSearchIT.java#L290

Hope it helps.



来源:https://stackoverflow.com/questions/42838168/elasticsearch-5-adding-context-to-suggestionbuilders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!