Java full-text query from Oracle database data to elasticsearch full-text Search library

Source: Internet
Author: User



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


Related Article

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.