First write the code before, we must first set up the Elasticsearch environment (this is very simple, online Baidu a lot). The Elasticsearch jar package is then imported into the project.
Elasticsearch database
After that, the coding work begins:
Elasticsearch create database
First step: Write code that connects to the local Elasticsearch environment
Elasticsearch vs database
Public StaticClient client=NULL; Public StaticClient getclient () {if(Client! =NULL){ returnclient; } Settings Settings= Settings.settingsbuilder (). Put ("Cluster.name", "My-application"). build (); Try{Client=Transportclient.builder (). settings (Settings). Build (). Addtransportaddress (NewInetsockettransportaddress (Inetaddress.getbyname (NULL), 9300));//if NULL, the default connection is local IP}Catch(unknownhostexception e) {e.printstacktrace (); } returnclient; }
Second step, build the index library and create the index
Logstash database to elasticsearch
Public voidCreateindexresponse (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 cannot find the dataIndexrequestbuilder Requestbuilder = Getclient (). Prepareindex (IndexName, type). Setrefresh (true); for(inti=0; I<jsondata.size (); i++) {Requestbuilder.setsource (Jsondata.get (i)). Execute (). Actionget (); } } /*** Create an index *@paramClient *@paramJsondata *@return */ Publicindexresponse createindexresponse (String indexname, String type,string jsondata) {indexresponse response=getclient (). Prepareindex (IndexName, type). SetSource (Jsondata). Execute (). actionget (); returnresponse; } /*** Perform search *@paramQueryBuilder *@paramIndexName *@paramtype *@return */ PublicList<esbean>Searcher (QueryBuilder QueryBuilder, String indexname, String type) {List<EsBean> list =NewArraylist<esbean> ();//Esbean is the field of the databaseSearchResponse SearchResponse =getclient (). Preparesearch (IndexName). Settypes (Type). Setquery (QueryBuilder). Execute (). Actionge T (); Searchhits hits=searchresponse.gethits (); System.out.println ("Query to Record count =" +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 (NewEsbean (ID, name, function)); } } returnlist; }
The third step is to write the entity class Esbean
Why elasticsearch is not a database
Public class esbean{private String reg_id; Private String organization; Omitting setter and getter methods; Public Esbean (String reg_id,string organization) {Super(); this. reg_id=reg_id; this. organization=organization;}}
Fourth step, query data from Oracle database into Esalticsearch Library
1 Public classjdbc{2 Public StaticJDBC DataFactory =Newjdbc ();3 Private StaticConnection Conn;4 Static FinalString username= "* * * *";5 Static FinalString password= "* * * * *";6 Static FinalString url= "***********";7 Public Static synchronizedConnection getinstance () {8 if(conn=NULL||conn.isclosed ()) {9 class. forname ("Oracle.jdbc.driver.OracleDriver");Tenconn=drivermanager.getconnection (Url,username,password); One } A returnConn; - } -}
Java full-text query from Oracle database data to elasticsearch full-text Search library