One: Functional background
In the near future to do a highlight of the search needs, has also been done. So there's no difficulty. Just the original used is Lucene, now to be replaced with SOLR, in the lucene4.x, the scattered fairy in the past in the article also analyzed how to achieve highlighting in the search, there are mainly three ways. For more details, please refer to the previous 2 articles of the Scattered Immortals:
First: How to achieve highlighting in Lucene4.3
http://qindongliang.iteye.com/blog/1953409
Second: How to highlight the service side in Solr4.3
http://qindongliang.iteye.com/blog/2034270
Second: the project inquiry
Generally speaking. There are 2 main ways to achieve, the first is the front desk to display data using JS highlighting, the second is the service side highlighted back to the front desk
The process of the back-end highlighting:
Front-end highlighting process:
Three: Pros and cons analysis
Back-end highlighting:
Performance: In the case of large concurrency, there may be some impact on the performance of the server.
Reliability: High, in the browser disable JS script case, still can display normally
Front-end highlighting:
Performance: rendered by client with slightly higher relative performance
Reliability: Low, in the browser disable JS script case, highlighting failure
Four: Precautions
When the foreground is highlighted, you need to put the phrase after the sentence. Back to the front desk JS, easy to replace, about the sentence participle, can use Lucene can also use SOLR, such as the following:
In Lucene:
Java code
- /***
- *
- * @param Analyzer word breaker
- * @param text sub-phrase
- * @throws Exception
- */
- Public Static void Analyzer (Analyzer analyzer,string text)throws exception{
- Tokenstream ts = analyzer.tokenstream ("name", text);
- Chartermattribute Term=ts.addattribute (chartermattribute. Class);
- Ts.reset ();
- while (Ts.incrementtoken ()) {
- System.out.println (Term.tostring ());
- }
- Ts.end ();
- Ts.close ();
- }
/*** * * @param analyzer Word breaker * @param text sub -phrase * @throws Exception */public static void Analyzer (Analyzer analyze R,string text) throws exception{ tokenstream ts = analyzer.tokenstream ("name", text); Chartermattribute Term=ts.addattribute (chartermattribute.class); Ts.reset (); while (Ts.incrementtoken ()) { System.out.println (term.tostring ()); } Ts.end (); Ts.close ();}
In Solr, Mode 1:
Java code
- /***
- * Word segmentation based on field type and print word segmentation results
- * @param text
- */
- Public Static void showanalysistype (String text)throws exception{
- String fieldtype="ik"; //Division of speech Type
- //Invoke service
- Fieldanalysisrequest request = new fieldanalysisrequest ("/analysis/field");
- //set type
- Request.addfieldtype (FieldType);
- //Set sentences to be participle
- Request.setfieldvalue (text);
- //sc=private static httpsolrclient sc=new httpsolrclient ("Http://localhost:8983/solr/one");
- //Get Results
- Fieldanalysisresponse Response =request.process (SC);
- //Get a corresponding analysis
- Analysis as = Response.getfieldtypeanalysis (FieldType);
- list<string> results = new arraylist<string> ();
- //Use the Guava library to convert the Iteratro object to a list object
- List<analysisphase> list=lists.newarraylist (As.getindexphases (). iterator ());
- //Take one of the Fitler's participle results, because a fieldtype is very likely configured with multiple filter. Every step passes
- the results of//filter are different, so here. To specify a filter that gets the result of the word breaker. With due regard to
- //So the scattered fairy here will write list.size-1. Note that the value here is not a fixed
- for (TokenInfo Token:list.get (List.size ()-1). Gettokens ()) {
- //Get Word segmentation data Results
- Results.add (Token.gettext ());
- }
- }
/*** * Word segmentation based on field type and print word result * @param text */public static void Showanalysistype (String text) throws Exception{string fieldtype= " IK ";//Division of Speech//Call service Fieldanalysisrequest request = new Fieldanalysisrequest ("/analysis/field ");// Set type Request.addfieldtype (FieldType);//Set the sentence to be participle request.setfieldvalue (text);//sc=private Static Httpsolrclient sc= New Httpsolrclient ("Http://localhost:8983/solr/one");//Get Results Fieldanalysisresponse response =request.process (SC); An analysis of the corresponding analysis as = Response.getfieldtypeanalysis (FieldType) is obtained; list<string> results = new arraylist<string> (); Use the Guava library. Convert the Iteratro object to a List object list<analysisphase> list=lists.newarraylist (as.getindexphases (). iterator ()); Take a fitler result, because a fieldtype is very likely to configure multiple filter, each step through//filter results are different, so here. To specify a filter that gets the result of a word breaker, as it relates//So the list.size-1 is written here, note that the value here is not a fixed for (TokenInfo Token:list.get (List.size ()-1). Gettok ENS ()) {//Get Word segmentation data result Results.add (Token.gettext ()); } }
In Solr, Mode 2:
Java code
- /***
- * According to the field of the word and print word results
- * @param text
- */
- Public Static void showanalysis (String text)throws exception{
- //Here is the field name
- String fieldname="Cpyname";
- //fixed wording
- Fieldanalysisrequest request = new fieldanalysisrequest ("/analysis/field");
- //join field
- Request.addfieldname (FieldName);
- //Set sentences that require participle
- Request.setfieldvalue (text);
- //Request the SOLR service to get results
- Fieldanalysisresponse Response =request.process (SC);
- //package result, return, business processing that might be called by it
- list<string> results = new arraylist<string> ();
- //Get results based on field names
- Analysis As=response.getfieldnameanalysis (fieldName);
- //Using Guava Toolkit, turn iterator to list
- List<analysisphase> list=lists.newarraylist (As.getindexphases (). iterator ());
- //Print word breaker results
- for (TokenInfo Token:list.get (List.size ()-1). Gettokens ()) {
- System.out.println (Token.gettext ());
- }
- }
/*** * According to the field name and print the word result * @param text */public static void Showanalysis (string text) throws exception{//Here is the field names String fieldn Ame= "Cpyname"; Fixed notation fieldanalysisrequest request = new Fieldanalysisrequest ("/analysis/field"); Join field Request.addfieldname (FieldName); Set the sentence request.setfieldvalue (text) that requires participle; Request the SOLR service to get results fieldanalysisresponse response =request.process (SC); Encapsulates the result, returned. Business processing that may be called by list<string> results = new arraylist<string> (); Get results based on field name Analysis as=response.getfieldnameanalysis (fieldName); Use the Guava Toolkit. Turn iterator to List list<analysisphase> list=lists.newarraylist (as.getindexphases (). iterator ()); Print the word segmentation result for (TokenInfo token:list.get (List.size ()-1). Gettokens ()) { System.out.println (Token.gettext ()); } }
Finally welcome everybody sweep the code to follow the public number: I am the Siege division (WOSHIGCS). We learn, progress and exchange together! (Woshigcs)
The content of this public number is about search and big data technology and the Internet and other aspects of the sharing of content. is also a warm technical interactive communication of the small home, there are any problems at any time can leave a message, welcome everyone to visit!
Strategy for highlighting in Lucene or SOLR