Usually the search engine, according to the user's input, real-time to give matching hints. So how does this function in Elasticsearch?
Elasticsearch has designed 4 types of suggester, namely:
- Term suggester
- Phrase Suggester
- Completion suggester
- Context Suggester
I use completion suggester to achieve this.
There are many articles on the Internet, not much to say here. We directly on the code:
1 PackageCom.cs99lzzs.elasticsearch.service.imp;2 3 ImportJava.text.DecimalFormat;4 Importjava.util.ArrayList;5 ImportJava.util.Iterator;6 Importjava.util.List;7 ImportJava.util.Map;8 9 ImportJavax.annotation.Resource;Ten One Importorg.apache.commons.lang.StringUtils; A ImportOrg.apache.log4j.Logger; - ImportOrg.apache.lucene.queryparser.classic.QueryParser; - ImportOrg.elasticsearch.action.search.SearchRequestBuilder; the ImportOrg.elasticsearch.action.search.SearchResponse; - ImportOrg.elasticsearch.action.search.SearchType; - Importorg.elasticsearch.client.Client; - ImportOrg.elasticsearch.index.query.BoolQueryBuilder; + ImportOrg.elasticsearch.index.query.QueryBuilder; - Importorg.elasticsearch.index.query.QueryBuilders; + ImportOrg.elasticsearch.search.SearchHit; A Importorg.elasticsearch.search.aggregations.Aggregation; at Importorg.elasticsearch.search.aggregations.AggregationBuilders; - Importorg.elasticsearch.search.aggregations.bucket.terms.StringTerms; - ImportOrg.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; - ImportOrg.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; - ImportOrg.elasticsearch.search.sort.SortBuilder; - Importorg.elasticsearch.search.sort.SortBuilders; in ImportOrg.elasticsearch.search.sort.SortOrder; - Importorg.elasticsearch.search.suggest.Suggest; to ImportOrg.elasticsearch.search.suggest.SuggestBuilder; + ImportOrg.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder; - ImportOrg.springframework.beans.factory.annotation.Value; the ImportOrg.springframework.stereotype.Service; * $ Panax Notoginseng@Service ("Productsearchservice") - Public classProductsearchserviceimpImplementsProductsearchservice { the + Private StaticLogger Logger = Logger.getlogger (productsearchservice.class); A the Private StaticString aggrationbrandname = "Brand_count"; + - Private StaticString suggestzhname = "Suggestname"; $ $@Resource (name= "Esclient") - Client esclient; - the@Value ("${elasticsearch.index}") - PrivateString Cluster_index;Wuyi the@Value ("${elasticsearch.type}") - PrivateString Cluster_type; Wu - About /** $ * @return - */ - @Override - Publiclist<string> getsuggestlist (String keyword,intNumber ) { AList<string> suggestlist =NewArraylist<string>(); + theCompletionsuggestionbuilder Suggestzhnamebuilder =NewCompletionsuggestionbuilder (suggestzhname). prefix (keyword). Size (100); -Suggestbuilder Suggestbuilder =NewSuggestbuilder () $ . Addsuggestion (Suggestzhname, suggestzhnamebuilder); theSearchResponse Suggestresponse =Esclient.preparesearch (Cluster_index) the . Settypes (Cluster_type) the //. Setquery (Querybuilders.matchallquery ()) the . Suggest (Suggestbuilder) - . Execute () in . Actionget (); the thelist<?extendssuggest.suggestion.entry<?extendssuggest.suggestion.entry.option>> RESULT1 =Suggestresponse About . Getsuggest (). Getsuggestion (Suggestzhname). GetEntries (); the for(SUGGEST.SUGGESTION.ENTRY<?extendsSuggest.suggestion.entry.option>OP:RESULT1) { thelist<?extendssuggest.suggestion.entry.option> options =op.getoptions (); the if(Suggestlist.size () <Number ) { + for(Suggest.Suggestion.Entry.Option pp:options) { - if(Suggestlist.size () < number &&!Suggestlist.contains (Pp.gettext (). toString ())) { the Suggestlist.add (Pp.gettext (). toString ());Bayi } the } the}Else{ - Break; - } the } the the returnsuggestlist; the } - the}
Elasticsearch 5.4.3 Combat--java API call: Search Suggestions