Elasticsearch search instance with highlighting and searching for special character filtering

Source: Internet
Author: User
Tags acer

See Code annotations for application notes.

1. Simple Search Examples show:

    public void Search () throws IOException {//Custom cluster node name String clustername = "elasticsearch_pudongping";        Get Client clients = Esclient.initclient (clustername); Create a query index, the parameter Productindex represents the index library to query for Productindex searchrequestbuilder searchrequestbuilder = client. P        Reparesearch ("Productindex");        Set the query index type, settypes ("ProductType1", "ProductType2", "productType3");        Used to set search searchrequestbuilder.settypes ("Productindex") in multiple types; Set Query Type 1.searchtype.dfs_query_then_fetch = exact query 2.searchtype.scan =//scan query, unordered Searchrequestbuilder.setse        Archtype (Searchtype.dfs_query_then_fetch);        Set the query keyword Searchrequestbuilder. Setquery (Querybuilders.fieldquery ("title", "Acer")); Query filter filter price within 4000-5000 here the range is [4000,5000] closed inclusive, the search results contain a price of 4000 and a price of 5000 data Searchrequestbuilder.setfilter (        Filterbuilders.rangefilter ("Price"). From (4000). to (5000));  Paging application      Searchrequestbuilder.setfrom (0). SetSize (60);        Sets whether to sort by query matching degree searchrequestbuilder.setexplain (true);        Performs a search, returning the search response information SearchResponse response = Searchrequestbuilder.execute (). Actionget ();        Searchhits searchhits = Response.gethits ();        Searchhit[] hits = Searchhits.gethits ();            for (int i = 0; i < hits.length; i++) {Searchhit hit = hits[i];            map<string, object> result = Hit.getsource (); Print Map collection: {id=26, onsale=true, title= Acer 3, price=4009.0,//Description=null, createdate=1380530123140, Ty        pe=2} System.out.println (Result);    } System.out.println ("Search success."); }

Description

Client.preparesearch is used to create a searchrequestbuilder, and the search is performed by Searchrequestbuilder.

The Client.preparesearch method has parameters of one or more indexes, which are represented in the database, that is, 0 or more database names, and you can use both (the following two can be represented in multiple index libraries):

Client.preparesearch (). SetIndices ("Index1", "Index2", "Index3", "index4");

Or:


Searchrequestbuilder Common Method Description:

(1) setindices (String ... indices): As described above, parameters can be one or more strings representing index to be retrieved, (2) settypes (String ... types): parameters can be one or more strings, Represents the type to be retrieved, when the parameter is 0 or does not call this method, the query all Type;setsearchtype (SearchType searchtype): The category that performs the retrieval, An element with a value of Org.elasticsearch.action.search.SearchType, SearchType is a class of enum type whose values are as follows: Query_then_fetch: The query is executed against all blocks, But the return is sufficient information, not the document content. The results are sorted and graded, based on which only the relevant block's document object is returned. Because only these are taken, the hit size returned is exactly equal to the specified sizes. This is handy for index with lots of blocks (return results are not duplicated because the blocks are grouped) Query_and_fetch: the most primitive (and possibly fastest) implementation is simply to perform the retrieval on all relevant shard and return the result. Each shard returns a certain size result.   Since each shard has returned a certain size hit, this type actually returns a certain size result of multiple shard to the caller.   Dfs_query_then_fetch: As with Query_then_fetch, an initial scattering is expected to accompany the term frequency assigned to a more accurate score calculation.   Dfs_query_and_fetch: As with Query_and_fetch, an initial scattering is expected to accompany the term frequency assigned to a more accurate score calculation. Scan: Performs a browse while performing a search that does not have any sort.   This will automatically start scrolling the result set. Count: Only the number of results is calculated, and facets are also executed. (4) Setsearchtype (string searchtype), similar to Setsearchtype (SearchType searchtype), except that its value is a string-type SearchType and the value can be dfs_query_ Then_fetch, Dfsquerythenfetch, Dfs_query_and_fetch, Dfsqueryandfetch, Query_then_fetch, QuErythenfetch, Query_and_fetch or Queryandfetch, (5) Setscroll (Scroll Scroll), Setscroll (TimeValue keepAlive) and Setscroll ( String keepAlive), set the scroll, with the parameter Scroll, construct a Scroll directly with the new Scroll (TimeValue), TimeValue or string is required to convert timevalue and string to scroll, (6) setTimeout (timevalue timeout) and setTimeout (string timeout), Set the time-out period for the search, (7) Setquery, set the query used by queries, (8) SetFilter, set the filter, (9) Setminscore, set the minimum number of score, and Setfrom, from which score to start; (11 SetSize, we need to find out how many results;

After retrieving the results, all the searchhit can be obtained by response.gethits (), and after hit, the corresponding document can be iterated and converted into the required entity.

2. Search highlighting

Spring-boot-starter-data-elasticsearch Highlight a demo of the scene


Org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder
Org.springframework.data.elasticsearch.core.SearchResultMapper
Org.springframework.data.domain.PageImpl
Org.elasticsearch.action.search.SearchResponse
Org.elasticsearch.search.SearchHit
Org.elasticsearch.search.highlight.HighlightField

String Pretag = "<font color= ' #dd4b39 ' >";//Google's color valueString Posttag = "</font>"; SearchQuery SearchQuery=NewNativesearchquerybuilder (). Withquery (QueryBuilder). Withfilter (Querybuilders.termquery ("Status", CommConstants.ItemStatus.Normal)) . Withsort (Sortbuilders.fieldsort ("Modifiedtime"). Order (Sortorder.desc)). Withpageable (pageable). Withhighlightfields (NewHighlightbuilder.field ("name"). Pretags (Pretag). Posttags (Posttag),NewHighlightbuilder.field ("Memo"). Pretags (Pretag). Posttags (Posttag)). Build (); returnElasticsearchtemplate.queryforpage (SearchQuery, UserDocument.class,NewSearchresultmapper () {@Override Public<T> page<t> mapresults (searchresponse response, class<t>Clazz, pageable pageable) {List<UserDocument> chunk =NewArraylist<>();  for(Searchhit searchHit:response.getHits ()) {if(Response.gethits (). Gethits (). length <= 0) {                        return NULL; } userdocument User=Newuserdocument ();                    User.setid (Long.valueof (Searchhit.getid ())); //name or MemoeHighlightfield name = Searchhit.gethighlightfields (). Get ("name"); if(Name! =NULL) {user.setname (Name.fragments () [0].tostring ()); } Highlightfield Memo= Searchhit.Gethighlightfields(). Get ("Memo"); if(Memo! =NULL{User.setmemo (memo).  Fragments() [0].tostring ());                } chunk.add (user); }                if(Chunk.size () > 0) {                    return NewPageimpl<t> ((list<t>) chunk); }                return NULL; }        });

@Test Public voidShouldreturnhighlightedfieldsforgivenqueryandfields () {//givenString DocumentID = randomnumeric (5); String Actualmessage= "Some test message"; String Highlightedmessage= "Some <em>test</em> message"; Sampleentity sampleentity=Sampleentity.builder (). ID (documentid). Message (actualmessage). Version (System.currenttimemillis    ()). build (); Indexquery Indexquery=getindexquery (sampleentity);    Elasticsearchtemplate.index (Indexquery); Elasticsearchtemplate.refresh (sampleentity.class); SearchQuery SearchQuery=NewNativesearchquerybuilder (). Withquery (Termquery ("Message", "Test"). Withhighlightfields (NewHighlightbuilder.field ("Message") . Build (); Page<SampleEntity> sampleentities = Elasticsearchtemplate.queryforpage (SearchQuery, sampleentity.class,NewSearchresultmapper () {@Override Public<T> page<t> mapresults (searchresponse response, class<t>Clazz, pageable pageable) {List<SampleEntity> chunk =NewArraylist<sampleentity>();  for(Searchhit searchHit:response.getHits ()) {if(Response.gethits (). Gethits (). length <= 0) {                    return NULL; } sampleentity User=Newsampleentity ();                User.setid (Searchhit.getid ()); User.setmessage (String) Searchhit.getsource (). Get ("Message")); User.sethighlightedmessage (Searchhit.gethighlightfields (). Get ("Message"). Fragments () [0].tostring ());            Chunk.add (user); }            if(Chunk.size () > 0) {                return NewPageimpl<t> ((list<t>) chunk); }            return NULL;    }    }); Assertthat (Sampleentities.getcontent (). Get (0). Gethighlightedmessage (), is (Highlightedmessage));}

Http://stackoverflow.com/questions/37049764/how-to-provide-highlighting-with-spring-data-elasticsearch



The Addhighlightedfield () method in Searchrequestbuilder can be customized to add highlighting to the keyword that retrieves the result of the field value

public void Search () throws IOException {//Custom cluster node name String clustername = "elasticsearch_pudongping";            Get Client clients = Esclient.initclient (clustername); Create query index, parameter productindex indicates that the index library to query is Productindex searchrequestbuilderSearchrequestbuilder= client. Preparesearch ("Productindex");        Set the query index type, settypes ("ProductType1", "ProductType2", "productType3");        Used to set search searchrequestbuilder.settypes ("Productindex") in multiple types; Set Query Type 1.searchtype.dfs_query_then_fetch = exact query 2.searchtype.scan = Scan query, unordered searchrequestbuilder.setsearchtype (S        Earchtype.dfs_query_then_fetch);        Set the query keyword Searchrequestbuilder. Setquery (Querybuilders.fieldquery ("title", "Acer")); Query filter filter price within 4000-5000 here the range is [4000,5000] closed inclusive, the search results contain a price of 4000 and a price of 5000 data Searchrequestbuilder.setfilter (        Filterbuilders.rangefilter ("Price"). From (4000). to (5000));        Paging application Searchrequestbuilder.setfrom (0). SetSize (60);                Sets whether to sort by query matching degree searchrequestbuilder.setexplain (true); Sets the highlight Searchrequestbuilder.Addhighlightedfield("title"); Searchrequestbuilder.sethighlighterPretags("<span style=\" color:red\ ">");        Searchrequestbuilder.sethighlighterposttags ("</span>");                Performs a search, returning the search response information SearchResponse response = Searchrequestbuilder.execute (). Actionget (); Get document results for searchsearchhitsSearchhits = Response.gethits ();        Searchhit[] hits = Searchhits.gethits ();        Objectmapper mapper = new Objectmapper ();            for (int i = 0; i < hits.length; i++) {Searchhit hit = hits[i];            Converts each object in the document to a JSON string value string json = Hit.getsourceasstring ();                          Converts the JSON string value to the corresponding entity object Product Product = Mapper.readvalue (JSON, product.class); Gets the corresponding highlighted field map<string, highlightfield> result = hit.Highlightfields();              Gets the specified domain Highlightfield Titlefield = result.get ("title") from the set of highlighted fields; Get a defined highlight labelText[] titletexts = Titlefield.Fragments();              Adds a custom highlight label to the title string value string title = "";              for (text text:titletexts) {title + = text;            }//The string value with the highlighted tag is refilled to the corresponding object Product.settitle (title);        SYSTEM.OUT.PRINTLN (product) of the entity object after the completion of the printing of the highlighted label;    } System.out.println ("Search success."); }

Program Run Result:

[id=8,title= Acer <span style= "color:red" >acer</span>,description= Acer Acer Hummingbird series, Price=5000.0,onsale=true, Type=1,createdate=mon Sep 13:46:41 CST 2013][id=21,title= acer <span style= "color:red" &GT;ACER&LT;/SPAN&GT; description= Acer Hummingbird Series, Price=5000.0,onsale=true,type=1,createdate=mon Sep-13:48:17 CST 2013][id=7,title= Acer < Span style= "color:red" >acer</span>,description= Acer Acer Hummingbird series, price=5000.0,onsale=true,type=1,createdate= Mon Sep 11:38:50 CST 2013][id=5,title= acer <span style= "color:red" >Acer</span> le 0,description=<null >,price=4000.0,onsale=true,type=1,createdate=mon Sep 16:35:23 CST 2013][id=12,title= acer <span style= "COLOR: Red ">Acer</span> le 1,description=<null>,price=4003.0,onsale=false,type=2,createdate=mon Sep 30 16:35:23 CST 2013][id=19,title= acer <span style= "color:red" >Acer</span> le 2,description=<null>,price =4006.0,onsale=false,type=1,createdate=mon Sep 16:35:23 CST 2013][id=26,title= acer <span style= "color:red">Acer</span> le 3,description=<null>,price=4009.0,onsale=true,type=2,createdate=mon Sep 30 16:35:23 CST 2013][id=33,title= acer <span style= "color:red" >Acer</span> le 4,description=<null>,price=4012.0, Onsale=false,type=1,createdate=mon Sep 16:35:23 CST 2013]

From the program execution results we can see that the highlighted tags we have defined have been appended to the specified domain.

When searching the index, your search keyword contains special characters, then the program will error

Fieldquery This must be your index field Oh, otherwise you can't find the data, here I only set two field ID, titlestring title = "Title+-&&| |! (){}[]^\"~*?:\ \ "; title = Queryparser.escape (title);//The main sentence is to escape the special characters, so lucene can identify Searchrequestbuilder.setquery ( Querybuilders.fieldquery ("title", title));


Reprint please specify source: [http://www.cnblogs.com/dennisit/p/3363851.html]

Elasticsearch search instance with highlighting and searching for special character filtering

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.