ES (Elasticsearch) suggester A total of four categories (term suggester, phrase suggester, completion suggester, context Suggester), Among them, completion suggester as the auto-completion function in the search box, especially used.
This article will implement a simple example in the Java language to describe how to use completion suggester.
The main function of the example is to create an auto-completion function for the name and number of the stock.
Implementing a complete completion Suggester function requires three steps: Create a schema, index data, and search for data. The following are described separately.
1. Building a schema
Schema for ES, like a database table definition, it is used to pre-define the name and type of each field.
Data that needs to be automatically filled out, a field of type completion is used to store the data to be padded. Specific as follows:
{ "Stock_suggest" : { "Mappings" : { "Stock" : { "_ID" : { "Path": "id" }, "Properties" : { "id" : { ' Type ': ' String ', "Analyzer": "Keyword" }, "Name" : { ' Type ': ' String ', "Index": "Not_analyzed" }, "Namesuggest" : { "Type": "Completion", "Analyzer": "Standard", "Payloads":true, "Preserve_separators":false, "Preserve_position_increments":false, "Max_input_length": 50 } } } } }}
It is necessary to note that the payloads property is set to True to return a payload field for data information that is used to host predefined (defined at index data) when auto-completion is activated.
2. Index data
The pending data is injected into ES below. The code is as follows:
1String eshosts = "";2String clustername = "";3Gsonbuilder Gsonbuilder =NewGsonbuilder (). Setdateformat ("Yyyy-mm-dd ' T ' HH:mm:ss. Sssz ");4Gson Gson =gsonbuilder.create ();5Settings Settings = Immutablesettings.settingsbuilder (). Put ("Client.transport.sniff",true). Put ("Cluster.name", clustername). Put ("Node.client",true). Build ();6Client client =NewTransportclient (Settings). addtransportaddress (NewInetsockettransportaddress (eshosts, 9300));7Bulkrequestbuilder bulkrequest =Client.preparebulk ();8List<stock> stocks =NewArrayList ();9Stocks.add (NewStock ("601390", "China Railway"));TenStocks.add (NewStock ("601186", "China Railway Construction")); OneStocks.add (NewStock ("601766", "China Car")); AStocks.add (NewStock ("600115", "Eastern Airlines")); -Stocks.add (NewStock ("000585", "Tohoku Electric")); -Stocks.add (NewStock ("000527", "Beauty Appliances")); the for(Stock stock:stocks) { -list<string> input =NewArraylist<string>(); - Input.add (Stock.getname ()); - Input.add (Stock.getid ()); +Map<object, object> payload =NewHashmap<object, object>(); -Payload.put ("id", Stock.getid ()); +Payload.put ("name", Stock.getname ()); APayload.put ("type", "Stock"); atNamesuggest namesuggest =Newnamesuggest (input, payload, stock.getid ()); - stock.setnamesuggest (namesuggest); -Jsonobject Jo =(Jsonobject) gson.tojsontree (stock); -String Jsonsource =Gson.tojson (Jo); - Bulkrequest.add (Client.prepareindex (index, type, Stock.getid ()). SetSource (Jsonsource)); - } inBulkresponse bulkresponse = Bulkrequest.execute (). Actionget ();
As shown above, we have inserted six stock data as examples. The first line of Eshosts and the second row of clustername need to be set according to their own ES cluster configuration.
3. Search for Data
Once the data has been indexed, you can use auto-completion.
1String input = "60"2String clustername = "";3String eshosts = "";4Settings Settings = Immutablesettings.settingsbuilder (). Put ("Client.transport.sniff",true). Put ("Cluster.name", clustername). Put ("Node.client",true). Build ();5Client client =NewTransportclient (Settings). addtransportaddress (NewInetsockettransportaddress (eshosts, 9300));6String field = "Namesuggest";7 8Suggestrequestbuilder SRB =client.preparesuggest (index);9Completionsuggestionbuilder CSFB =NewCompletionsuggestionbuilder (field). field. Text (input). Size (10);TenSRB =srb.addsuggestion (CSFB); OneSuggestresponse response =Srb.execute (). Actionget (); ASYSTEM.OUT.PRINTLN (response);
The output from the previous section of the code is as follows:
{ "Namesuggest" : [ { "Text": "60", "Offset": 0, "Length": 2, "Options" : [ { "Text": "600115", "Score": 1.0, "Payload": {"name": "Eastern Airlines", "id": "600115", "type": "Stock"} }, { "Text": "601186", "Score": 1.0, "Payload": {"name": "China Iron Build", "id": "601186", "type": "Stock"} }, { "Text": "601390", "Score": 1.0, "Payload": {"name": "China Iron", "id": "601390", "type": "Stock"} }, { "Text": "601766", "Score": 1.0, "Payload": {"name": "Chinese Car", "id": "601766", "type": "Stock"} } ] } ]}
As can be seen, the numbers with the "60" beginning of the stock have been returned.
Well, a simple example is complete, and some of the details and related principles are detailed in the next article.
Elasticsearch in Java example: auto-complement function (completion suggester)