Elasticsearch 5.4.3 Combat--java API call: Search Suggestions

Source: Internet
Author: User
Tags gettext

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&LT;?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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.