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: E) {suggests. Add (option. gettext (). tostring () ;}} return suggests ;}}
OK. Here a simple completion suggester is complete.
More advanced examples will be added later.