Use of elasticsearch suggest-basic use of completion, elasticsearch

Source: Internet
Author: User

Use of elasticsearch suggest-basic use of completion, elasticsearch

In lucene, the support of suggest is very complete and can be customized as needed; however, it is not so convenient to use it in es. Es classifies suggest into four categories: term; phrase; completion; context; the latest version of es1.2.1 is still being improved; term suggester is a prompt Based on the frequency of a field in which a word element appears. phrase suggester is used to enhance the term. Here we will not describe how to use it;

Link: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-term.html

The rest are completion and context suggester. The usage of these two fields is completely different from the above method. The above two fields are prompted Based on the specified field Content During the query, and the suggester fields need to be customized in mapping. Prompt when a full match is used;

Completion suggester official documentation http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html documentation to explain the very comprehensive, unfortunately there is no code, here the code plus java client;

1: Set suggester in mapping

@Test public void createQucikIndecs() throws IOException{ XContentBuilder mapping = XContentFactory.jsonBuilder() .startObject() .startObject("pingzhuang") .startObject("properties") .startObject("id").field("type", "long").field("store", "yes").field("index", "not_analyzed").endObject() .startObject("name").field("type", "string").field("store", "no").field("indexAnalyzer", "ik").field("searchAnalyzer", "ik").endObject() .startObject("suggest").field("type","completion").field("index_analyzer","simple").field("search_analyzer","simple").field("payloads","true").endObject() .endObject() .endObject() .endObject(); ESHandler.createQuickIndices("chinamedic", "pingzhuang", mapping, "formedic"); }

2: After the index is set, a prompt should be added each time during indexing.

@ Test public void addIndex () {String json = "{\" id \ ": 44, \" name \ ": \" old wolf-age when white clothes floated \", \ "suggest \": {\ "input \": \ "old wolf-age when white was drifting \" }}"; String json1 = "{\" id \ ": 42, \ "name \": \ "old wolf-Lang's heart is like iron \", \ "suggest \": {\ "input \": \ "old wolf-Lang's heart is like iron \"}}"; string json2 = "{\" id \ ": 43, \" name \ ": \" old wolf-wandering Singer ", \" suggest \": {\ "input \": \ "old wolf-ask for a stray singer \"} "; ESHandler. addOneIndex ("chinamedic", "pingzhuang", json); ESHandler. addOneIndex ("chinamedic", "pingzhuang", json1); ESHandler. addOneIndex ("chinamedic", "pingzhuang", json2); System. out. println ();}

3: the query is prompted.

/*** Search suggestion, automatically complete search result * @ param indices index database name * @ param prefix search prefix word * @ return recommended List */public static List <String> getCompletionSuggest (String indices, String prefix) {CompletionSuggestionBuilder suggestionsBuilder = new CompletionSuggestionBuilder ("complete"); suggestionsBuilder. text (prefix); suggestionsBuilder. field ("suggest"); suggestionsBuilder. size (10); SuggestResponse resp = client. prepareSugge St (indices) .addsuggestion(suggestionsbuildercmd.exe cute (). actionGet (); List <? Extends Entry <? Extends Option> list = resp. getSuggest (). getSuggestion ("complete "). getEntries (); List <String> suggests = new ArrayList <String> (); if (list = null) {return null;} else {for (Entry <? Extends Option> e: list) {for (Option option: e) {suggests. add (option. getText (). toString () ;}} return suggests ;}}

OK. Here a simple completion suggester is complete.

More advanced examples will be added later.




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.