Elasticsearch Java Client Operation---Search filtering, grouping highlighting, Elasticsearchjava
Elasticsearch provides a lot of API, look at the name of almost can understand what meaning
SOURCE Address: Source Download click
public void Search01 () {QueryBuilder query = querybuilders.querystring ("9");//Set the content of the search and the actual size searchresponse res = Client.preparesearch ("Test"). Setquery (query). Setfrom (0). SetSize (). Execute (). Actionget (); Searchhits SHS = Res.gethits (); SYSTEM.OUT.PRINTLN ("Total data:" + shs.gethits (). length); for (Searchhit It:shs) {System.out.println (It.getsource ());}} Grouping fact is similar to SQL Grouppublic void Search02 () {//defines a grouping and names it termsfacetbuilder Facetbuilder = Facetbuilders.termsfacet (" Groupbysex ");//by which field to group Facetbuilder.field (" Sex "). Size (1);//filter matches All Files Facetbuilder.facetfilter ( Filterbuilders.matchallfilter ()); SearchResponse res = Client.preparesearch ("Test2"). Settypes ("test02"). Addfacet (Facetbuilder). Execute (). Actionget ( );//Gets the grouping in search results facets facet = res.getfacets ();//multiple groupings are stored termsfacet FAC = (termsfacet) facet.getfacets () in map form. Get (" Groupbysex "); for (Termsfacet.entry tf:fac.entries ()) {System.out.println (Tf.getcount () +" ====== "+ tf.getterm ());}} querypublic void search03 () {//indexed by field, as long as the content contains not allConform to QueryBuilder QueryBuilder = querybuilders.fieldquery ("name", "Lee 41 is also available after");//The entire data content can querybuilder QB as long as it has this word Querybuilders.querystring ("four"); The combination query name is Zhang San, the record of sex for boy must must have, mustnot means not included, should means can exist querybuilder QueryBuilder2 = Querybuilders.fieldquery ( "Name", "Zhang San"); QueryBuilder QueryBuilder3 = querybuilders.fieldquery ("Sex", "boy"); QueryBuilder qbmust = Querybuilders.boolquery (). Must (QueryBuilder2). should (queryBuilder3);//filter query QueryBuilder Qb3 = Querybuilders.filteredquery (QueryBuilder2, Filterbuilders.prefixfilter ("Sex", "bo")); SearchResponse res = client.preparesearch ("Test"). Settypes ("test01"). Setsearchtype (Searchtype.default). Setquery ( QB3). Execute (). Actionget (); Searchhits SHS = Res.gethits (); SYSTEM.OUT.PRINTLN ("Data found:" + shs.gethits (). length); for (Searchhit It:shs) {System.out.println (It.getsource ());}} Highlight public void search04 () {//prefix filter Chinese characters are not good with filterbuilder fb = Filterbuilders.prefixfilter ("Sex", "bo");// Range filter Filterbuilder FB2 = FiLterbuilders.rangefilter ("id"). from (1). to (10); Combined filtering Filterbuilder FB3 = Filterbuilders.andfilter (Filterbuilders.rangefilter ("id"). from (1) to (10), Filterbuilders.prefixfilter ("Sex", "bo")). Cache (true);//Default false SearchResponse res = Client.prepa ("Test"). Settypes ("test01"). Setsearchtype (Searchtype.dfs_query_then_fetch). SetFilter (FB3). Execute (). Actionget (); Searchhits SHS = Res.hits (); SYSTEM.OUT.PRINTLN ("Total data:" + shs.gethits (). length); for (Searchhit It:shs) {System.out.println (It.getsource ());}} Highlight public void search05 () {QueryBuilder QB1 = querybuilders.termquery ("name", "Zhang San _4");//Filterbuilder filter =filter Builders.prefixfilter ("name", "Zhang San"); SearchResponse res = Client.preparesearch ("Test4"). Settypes ("test04"). Setsearchtype (Searchtype.dfs_query_then_ FETCH). Setquery (Querybuilders.fieldquery ("id", "1")). Addhighlightedfield ("id"). Sethighlighterpretags ("<spand > "). Sethighlighterposttags (" </bod> "). Execute (). Actionget (); Searchhits SHS = Res.hits (); SYSTEM.OUT.PRINTLN ("Total data:" + shs.gethits (). length); for (Searchhit It:shs) {System.out.println (It.getsource ());// Gets the corresponding highlighted field map<string, highlightfield> result = It.highlightfields ();//obtains the specified domain from the set of highlighted fields Highlightfield Titlefield = Result.get ("id");//Get definition of the highlighted label string[] titletexts = titlefield.fragments ();//Add a custom highlight label to the title string value string title = ""; for ( String text:titletexts) {title + = text;} Re-populate the string value of the highlighted tag to the corresponding Object//Product.settitle (title);//The Entity object System.out.println (title) after the highlight label is appended;}}
Elasticsearch Java Client Operations-filtering of searches, grouping highlighting