What is elasticsearch.
Elasticsearch is an open source search engine based on the Apache Lucene (TM). Whether in open source or proprietary domain, Lucene can be considered to be the most advanced, best performance, the most full-featured search engine library.
However, Lucene is just a library. To use it, you have to use Java as the development language and integrate it directly into your application, and, worse, Lucene is very complex, and you need to delve into the relevant knowledge of the search to understand how it works.
Elasticsearch also uses Java to develop and use Lucene as its core to implement all indexing and search capabilities, but its goal is to hide Lucene's complexity through a simple restful API, making Full-text search simple.
However, Elasticsearch is not only Lucene and Full-text search, we can also describe it: Distributed real-time file storage, each field is indexed and can be searched distributed real-time Analysis search engine can be extended to hundreds of servers, processing PB-structured or unstructured data
Furthermore, all of these functions are integrated into a service, and your application can interact with it through a simple restful API, client of various languages, or even a command line.
It's very easy to get started with Elasticsearch. It provides many reasonable defaults and hides complex search engine theory for beginners. It is out-of-the-box (installed for use) and requires little learning to be used in a production environment.
Elasticsearch is licensed under Apache 2 License and can be downloaded, used and modified free of charge.
As you deepen your understanding of elasticsearch, you can tailor the advanced features of Elasticsearch to different problem areas, all of which are configurable and very flexible to configure. Config elasticsearch.properties
#配置elasticsearch index
csg_bill_index_name=hsc_test_index
#配置elasticsearch type
csg_bill_type_name= Billdetails
#配置elasticsearch es address
csg_bill_es_address=l-test.h.beta.cn0:9300
#配置elasticsearch Cluster
Csg_bill_es_cluster=elasticsearch_f
Create elasticsearch transportsessionfactory
/** * Created by xueping.you on 15-7-30. * * @Service public class Estransportsessionfactory {private final static Logger Logger = Loggerfactory.getlogger (ES
Transportsessionfactory.class);
@Value ("${csg_bill_es_address}") Private String elasticaddress;
@Value ("${csg_bill_es_cluster}") Private String elasticcluster;
Private Transportclient transportclient; @PostConstruct public void init () {checkargument (!
Strings.isempty (Elasticcluster), "Elasticsearch Cluster Name Null or emptystring");
transportaddress[] transportaddresses = null; List<string> AddressList = lists.newarraylist (Splitter.on (","). Trimresults (). Omitempty
Strings (). Split (elasticaddress)); Checkargument (!
Collectionutils.isempty (AddressList), "Elasticsearch Cluster address Can ' t be Empty");
transportaddresses = new Transportaddress[addresslist.size ()]; Splitter Splitter = SpLitter.on (":"). Omitemptystrings (). Trimresults (); for (int i=0; i<addresslist.size (); i++) {list<string> Singleaddresspair = lists.new
ArrayList (Splitter.split (Addresslist.get (i))); Checkargument (Singleaddresspair.size () ==2, "Elasticsearch address format address:port error" + addres
Slist.get (i));
Transportaddresses[i] = new Inetsockettransportaddress (singleaddresspair.get (0),
Integer.parseint (Singleaddresspair.get (1))); Settings settings = Immutablesettings.builder (). Put ("Cluster.name", Elasticcluster). b
Uild ();
Transportclient = new transportclient (settings);
Transportclient.addtransportaddresses (transportaddresses); Logger.info ("Elasticsearch Init done elasticaddress={}, elasticclustername={}", Elasticaddress, ELASTICCLU
Ster); } @PreDestrOy public void Destroy () {if (transportclient!= null) {transportclient.close ();
} public transportclient Gettransportclient () {return transportclient; }
}
Service Method Interface
/** * Created by xueping.you on 15-7-30. * * @Service public class Esbilldetailservicebase {private final static Logger Logger = Loggerfactory.getlogger (Esbill
Detailservicebase.class);
@Resource private Estransportsessionfactory estransportsessionfactory;
@Value ("${csg_bill_index_name}") Private String Es_index;
@Value ("${csg_bill_type_name}") Private String Es_type;
private static Objectmapper Objectmapper = new Objectmapper ();
static {Objectmapper.setdateformat (new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss")); /** * Method1 will order number, billing number, Product ID, billing number, settlement supplier ID, settlement Object id,create_ts Index * @param esbilldetails/P ublic void Insertallonduplicateidcover (list<esbilldetail> esbilldetails) {Stopwatch Stopwatch = Stopwatch.cr
Eatestarted ();
if (Collectionutils.isempty (esbilldetails)) {return;
} transportclient client = Estransportsessionfactory.gettransportclient (); Bulkrequestbuilder Bulkrequestbuilder = Client.preparebulk ();
for (Esbilldetail esbilldetail:esbilldetails) {String source;
try {Source = objectmapper.writevalueasstring (Esbilldetail);
catch (Exception E1) {logger.error ("Create Billdetail index Error Source:" + Esbilldetail, E1);
Qmonitor.recordone (Qmonitorconstants.es_bill_detail_indexing_error);
Continue
} Indexrequestbuilder Indexrequestbuilder = Client.prepareindex (Es_index, Es_type,
Esbilldetail.getid ()). SetSource (source);
Bulkrequestbuilder.add (Indexrequestbuilder);
} bulkresponse Bulkresponse = Bulkrequestbuilder.execute (). Actionget (); if (Bulkresponse.hasfailures ()) {iterator<bulkitemresponse> Itemresponseiterator = BulkResponse.iterator (
);
while (Itemresponseiterator.hasnext ()) { Bulkitemresponse itemresponse = Itemresponseiterator.next (); if (Itemresponse!=null && itemresponse.isfailed ()) {Logger.info ("Indexing Billdetail error Inde xmessage={}, errormassage={} ", Joiner.on (', '). Join (Itemresponse.getindex (), ITE
Mresponse.gettype (), Itemresponse.getid ()), jsonutils.tojsonstring (Itemresponse));
} qmonitor.recordone (Qmonitorconstants.es_bill_detail_indexing_error);
} qmonitor.recordone (Qmonitorconstants.es_bill_detail_indexing_done, stopwatch.elapsed (TimeUnit.MILLISECONDS)); /** * METHOD2 The order number, the billing number, product ID, billing number of any conditions to query, support pagination/public list<esbilldetail> query (ESB Illdetailquery esbilldetailquery, pagination pagination) {try {stopwatch stopwatch = Stopwatch.creat
Estarted (); list<esbilldetail> esbilldetails = Lists.newarrAylist ();
Boolquerybuilder Boolquerybuilder = Querybuilders.boolquery (); For (Map.entry Entry:esBillDetailQuery.entrySet ()) {Esbilldetailquery.param Param = (ESBILLDETAILQUERY.PA
RAM) Entry.getkey ();
Param.decorateboolquerybuilder (Entry.getvalue (), boolquerybuilder); } Searchrequestbuilder Requestbuilder = Estransportsessionfactory.gettransportclient (). pr Eparesearch (Es_index). Settypes (Es_type). Setsearchtype (Searchtype.dfs_query_then_
FETCH). Setquery (Boolquerybuilder); if (pagination!=null) {Requestbuilder.setfrom (Pagination.getstartindex ()).
SetSize (Pagination.getpagesize ());
} SearchResponse SearchResponse = Requestbuilder.execute (). Actionget ();
Searchhits searchhits = Searchresponse.gethits (); for (Searchhit searchhit:sEarchhits) {map<string, object> Map = Searchhit.getsource ();
Esbilldetail Esbilldetail = Objectmapper.readvalue (jsonutils.tojsonstring (map),
New Typereference<esbilldetail> () {});
Esbilldetails.add (Esbilldetail); } logger.info ("Query esdetail done query={}, time={}", Esbilldetailquery, stopwatch.elapsed (timeunit.millis
Econds));
Qmonitor.recordone (Qmonitorconstants.es_bill_query, stopwatch.elapsed (timeunit.milliseconds));
return esbilldetails; }catch (Exception e) {logger.error ("Elastic Search query Error query={}, page={}", Esbilldetailquery, Pagin
ation, E);
Qmonitor.recordone (Qmonitorconstants.es_bill_detail_query_error);
throw new RuntimeException ("ES query Exception");
}/** * This method is careful to use .......... * Method3 Delete records in ES by matching criteria * * Public boolean DELETe (esbilldetailquery esbilldetailquery) {try {stopwatch stopwatch = stopwatch.createstarted ();
Boolquerybuilder Boolquerybuilder = Querybuilders.boolquery (); For (Map.entry Entry:esBillDetailQuery.entrySet ()) {Esbilldetailquery.param Param = (ESBILLDETAILQUERY.PA
RAM) Entry.getkey ();
Param.decorateboolquerybuilder (Entry.getvalue (), boolquerybuilder);
} deletebyqueryresponse Deletebyqueryresponse = Estransportsessionfactory.gettransportclient () . Preparedeletebyquery (Es_index). Settypes (Es_type). Setquery (Boolquerybuilde
R). Execute (). Actionget ();
Logger.info ("Delete esdetail done query={}, time={}", Esbilldetailquery, stopwatch.elapsed (timeunit.milliseconds));
Qmonitor.recordone (Qmonitorconstants.es_bill_delete, stopwatch.elapsed (timeunit.milliseconds)); Return Deletebyqueryresponse.status ().Equals (Reststatus.ok)?
True:false;
}catch (Exception e) {logger.error ("Elastic Search Delete Error query={}, page={}", Esbilldetailquery, E);
Qmonitor.recordone (Qmonitorconstants.es_bill_detail_query_error);
throw new RuntimeException ("ES Delete Record exception"); }
}
Esbilldetailquery
/** * Created by xueping.you on 15-7-30. */public class Esbilldetailquery extends genericquery<esbilldetailquery.param> {//All str type public enum Par am{IDs {@Override public <t, Q extends querybuilder> void Decorateboolquerybuilder (T param, Q QueryBuilder)
{list<string> Strsparam = (list<string>) param;
Boolquerybuilder Boolquerybuilder = (boolquerybuilder) QueryBuilder;
if (Collectionutils.isempty (Strsparam)) {return;
} Boolquerybuilder Subcodequery = Querybuilders.boolquery (); for (String Strparam:strsparam) {subcodequery.should (Querybuilders.matchquery ("Billno", Strparam). op
Erator (MatchQueryBuilder.Operator.AND));
} boolquerybuilder.must (Subcodequery); }, Billnos {@Override public <t, Q extends querybuilder> void Decorateboolquerybuilder (T param, Q querybuilder) {list<string> Strsparam =
(list<string>) param;
Boolquerybuilder Boolquerybuilder = (boolquerybuilder) QueryBuilder;
if (Collectionutils.isempty (Strsparam)) {return;
} Boolquerybuilder Subcodequery = Querybuilders.boolquery (); for (String Strparam:strsparam) {subcodequery.should (Querybuilders.matchquery ("Billno", Strparam). op
Erator (MatchQueryBuilder.Operator.AND));
} boolquerybuilder.must (Subcodequery); }, Ordernos {@Override public <t, Q extends querybuilder> void Decoratebo
Olquerybuilder (T param, Q querybuilder) {list<string> Strsparam = (list<string>) param;
Boolquerybuilder Boolquerybuilder = (boolquerybuilder) QueryBuilder; if (Collectionutils.isempty (Strsparam)) {return;
} Boolquerybuilder Subcodequery = Querybuilders.boolquery (); for (String Strparam:strsparam) {subcodequery.should (Querybuilders.matchquery ("OrderNo", strparam). O
Perator (MatchQueryBuilder.Operator.AND));
} boolquerybuilder.must (Subcodequery); }, Productids {@Override public <t, Q extends querybuilder> void decorate
Boolquerybuilder (T param, Q querybuilder) {list<string> Strsparam = (list<string>) param;
Boolquerybuilder Boolquerybuilder = (boolquerybuilder) QueryBuilder;
if (Collectionutils.isempty (Strsparam)) {return;
} Boolquerybuilder Subcodequery = Querybuilders.boolquery ();
for (String Strparam:strsparam) { Subcodequery.should (Querybuilders.matchquery ("ProductId", Strparam). Operator (MatchQueryBuilder.Operator.AND));
} boolquerybuilder.must (Subcodequery); }, Reconcileids {@Override public <t, Q extends querybuilder> void decora
Teboolquerybuilder (T param, Q querybuilder) {list<string> Strsparam = (list<string>) param;
Boolquerybuilder Boolquerybuilder = (boolquerybuilder) QueryBuilder;
if (Collectionutils.isempty (Strsparam)) {return;
} Boolquerybuilder Subcodequery = Querybuilders.boolquery (); for (String Strparam:strsparam) {subcodequery.should (Querybuilders.matchquery ("Reconcileid", Strpara
m). Operator (MatchQueryBuilder.Operator.AND));
} boolquerybuilder.must (Subcodequery);
}
}; Public ABstract <t, Q extends querybuilder> void Decorateboolquerybuilder (T param, q QueryBuilder); Public esbilldetailquery () {} public esbilldetailquery (integer startIndex, integer maxcount) {s
Uper (StartIndex, Maxcount);
Public Esbilldetailquery (class<param> paramtype) {super (Paramtype); }
}