Java Simulation Simple Search
Entity class
Package org.dennisit.entity;/*** * *@version: 1.0 * *@author: Sujonin <a href= "mailto:[email protected" "> Send mail </a> * *@since: 1.0 Created: 2013-4-8 pm 04:51:03 * * @function: TODO * */public class Medicine { private Integer ID; private String name; private String function; Public Medicine () { super ();} Public Medicine (Integer ID, string name, String function) { super (); this.id = ID; this.name = name; this.function = function;} //getter and Setter ()}
Analog data
Package org.dennisit.entity;Import java.util.ArrayList;Import java.util.List;Import Org.dennisit.util.JsonUtil;/*** * *@version: 1.0 * *@author: Sujonin <a href= "mailto:[email protected" "> Send mail </a> * *@since: 1.0 creation Time: 2013-4-8 11:38:15 * *@function: TODO * * *PublicClass DataFactory {Publicstatic DataFactory DataFactory =New DataFactory ();Private DataFactory () {}Public DataFactory getinstance () {return DataFactory; }PublicStatic list<string> Getinitjsondata () {list<string> List =New Arraylist<string> (); String data1 = Jsonutil.obj2jsondata (New Medicine (1,"Silver flower cold particles", "function indications: silver flower cold particles, headache, heat, table, swallow." ")); String data2 = jsonutil.obj2jsondata (new Medicine (2, "cold cough syrup", "function indications: Cold cough syrup, table clearing heat, cough and phlegm." ")); String data3 = jsonutil.obj2jsondata (new Medicine (3, "cold Spirit granule", new Medicine (4, "cold Spirit capsule", "function indications: silver flower cold particles, headache, heat, table, swallow. ")); String data5 = jsonutil.obj2jsondata (new Medicine (5, "Renhe cold particles", "function indications: wind and heat, lung cough, relieve the table heat, cough and phlegm. ")); List.add (DATA1); List.add (DATA2); List.add (DATA3); List.add (DATA4); List.add (DATA5); return list;}
Application Tool Classes
Package org.dennisit.util;Import java.io.IOException;Import Org.dennisit.entity.Medicine;Import Org.elasticsearch.common.xcontent.XContentBuilder;Import Org.elasticsearch.common.xcontent.XContentFactory;/*** * *@version: 1.0 * *@author: Sujonin <a href= "mailto:[email protected" "> Send mail </a> * *@since: 1.0 creation Time: 2013-4-8 11:34:56 * *@function: TODO * * *PublicClass jsonutil {/** * Implement convert entity objects to JSON objects * @param Medicine Medicine Object * @return */public static string obj2jsondata (Medicine Medicine) {string jsondata = null; try {// use Xcontentbuilder to create JSON data xcontentbuilder Jsonbuild = Xcontentfactory.jsonbuilder (); Jsonbuild.startobject (). Field ( "id", Medicine.getid ()). Field ( "name", Medicine.getname ()). Field ( "Funciton", Medicine.getfunction ()). EndObject (); jsondata = Jsonbuild.string (); System.out.println (Jsondata); } catch (IOException e) {e.printstacktrace ();} return jsondata;}
ElasticSearch Core Search Simulation class
Package org.dennisit.elastic.process;Import java.util.ArrayList;Import java.util.List;Import Org.dennisit.entity.DataFactory;Import Org.dennisit.entity.Medicine;Import Org.elasticsearch.action.index.IndexRequestBuilder;Import Org.elasticsearch.action.index.IndexResponse;Import Org.elasticsearch.action.search.SearchResponse;Import org.elasticsearch.client.Client;Import org.elasticsearch.client.transport.TransportClient;Import org.elasticsearch.common.transport.InetSocketTransportAddress;Import Org.elasticsearch.index.query.QueryBuilder;Import Org.elasticsearch.index.query.QueryBuilders;Import Org.elasticsearch.search.SearchHit;Import org.elasticsearch.search.SearchHits;/*** * *@version: 1.0 * *@author: Sujonin <a href= "mailto:[email protected" "> Send mail </a> * *@since: 1.0 creation Time: 2013-4-8 11:34:04 * *@function: TODO * * *PublicClass Elasticsearchhandler {Private Client client;Public Elasticsearchhandler () {//Use this machine as a nodeThis"127.0.0.1"); }Public Elasticsearchhandler (String ipAddress) {//Cluster Connection Timeout setting/*Settings Settings = Immutablesettings.settingsbuilder (). Put ("Client.transport.ping_timeout", "10s"). Build (); Client = new Transportclient (settings); */client =New Transportclient (). Addtransportaddress (New Inetsockettransportaddress (IpAddress,9300)); }/*** Index, after index is established, will be created in elasticsearch-0.20.6\data\elasticsearch\nodes\0 so you see *@param indexname is the index library name, there can be multiple index libraries in an ES cluster. The name must be lowercase *@param indextype Type is an index type and is used to differentiate between different types of data under the same index library, there can be multiple index types under an index library. *@param jsondata data collection in JSON format * *@return */Publicvoid Createindexresponse (String indexname, String type, list<string> jsondata) {//Creating an index library It is important to note that the. Setrefresh (true) must be set here, otherwise the first index lookup does not find data indexrequestbuilder Requestbuilder = Client.prepareindex ( IndexName, type). Setrefresh (true);Forint i=0; I<jsondata.size (); i++) {Requestbuilder.setsource (Jsondata.get (i)). Execute (). Actionget ();}/*** Create an index *@param client *@param jsondata *@return */Public Indexresponse Createindexresponse (string indexname, String type,string jsondata) {indexresponse response = Client . Prepareindex (IndexName, type). SetSource (Jsondata). Execute (). Actionget ();return response; }/*** Perform search *@param QueryBuilder *@param indexname *@param type *@return */Public list<medicine> Searcher (QueryBuilder QueryBuilder, String indexname, String type) {list<medicine> List =New Arraylist<medicine> (); SearchResponse SearchResponse = Client.preparesearch (indexname). Settypes (Type). Setquery (QueryBuilder). Execute (). Actionget (); Searchhits hits = Searchresponse.hits (); System.out.println ("Number of queries to record =" + hits.gettotalhits ()); searchhit[] searchhists = Hits.gethits ();if (searchhists.length>0) {for (Searchhit hit:searchhists) {Integer id = (integer) hit.getsource (). Get ("id"); String name = (string) hit.getsource (). Get ("Name"); String function = (string) hit.getsource (). Get ("Funciton"); List.add (New Medicine (ID, name, function)); } }return list; }PublicStaticvoid Main (string[] args) {Elasticsearchhandler Eshandler =New Elasticsearchhandler (); list<string> jsondata = Datafactory.getinitjsondata (); String IndexName = "Indexdemo"; String type = "Typedemo"; Eshandler.createindexresponse (IndexName, type, jsondata); // query condition QueryBuilder QueryBuilder = querybuilders.fieldquery (" Name ", " cold "); /*querybuilder QueryBuilder = Querybuilders.boolquery (). Must ( Querybuilders.termquery ("id", 1)); */list<medicine> result = Eshandler.searcher (QueryBuilder, IndexName, type); for (int i=0; i<result.size (); i++) {Medicine Medicine = Result.get (i); System.out.println ( "(" + medicine.getid () + "\t\t" + medicine.getfunction ()); } }}
Start ElasticSearch (window under Bin/elasticsearch.bat)
Before the program runs, the default nodes directory does not have the content, after running the program will establish the following directory
Console output Information
{"id": 1, "name": "Silver Flower Cold granule", "Funciton": "Function indications: silver flower cold particles, headache, heat, solution table, the pharynx." "} {" id ": 2," name ":" Cold cough Syrup "," funciton ":" Functional indications: Cold cough syrup, clear heat, relieve cough and phlegm. " "} {" id ": 3," name ":" Cold Spirit granule "," Funciton ":" Function indications: antipyretic and analgesic. " Headache, heat-clearing. "} {" id ": 4," "Name": "Cold capsule", "Funciton": "Functional indications: silver flower cold particles, headache, heat, the table, Swallow." "} {" id ": 5," name ":" Renhe cold Particles "," Funciton ":" Functional indications: wind and heat, lung cough, relieve the table heat, cough and phlegm. " "} query to number of records = 5 (4) Drug name: Cold Ling Capsules function Indications: silver flower cold particles, headache, heat, solution table, the pharynx. (1) Drug name: Silver flower Cold particles function indications: silver flower cold particles, headache, heat, table, swallow. (2) Medicine name: Cold Cough Syrup function indications: Cold cough syrup, relieve heat, cough and phlegm. (3) Medicine Name: Cold Spirit granule function indications: antipyretic analgesia. Headache, heat-clearing. (5) Drug name: Renhe Cold particles function indications: wind and heat, lung cough, relieve the table heat, cough and phlegm.
Cluster Management tools Head View information
Appendix:
ElasticSearch Installing plug-ins Elasticsearch-head plugins
After installation, in the Lasticsearch-0.20.6\plugins\ directory, the head plug-in
Open the index.html file directly in the directory to access the management tool
Java Simulation Simple Search