One: Functional background
Recently to do a highlight of the search needs, has been done before, so there is no difficulty, but the original use is Lucene, now to be replaced SOLR, In the lucene4.x, scattered in the previous article also analyzed how to achieve highlighting in the search, there are three ways, the specific content, please refer to the previous 2 articles:
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
Overall 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, slightly higher relative performance
Reliability: Low, in the browser disable JS script case, highlighting failure
Four: Precautions
foreground highlighting, you need to put the sentence after the phrase, back to the foreground JS, easy to replace, about the sentence participle, you can use Lucene can also use SOLR, the way is as follows:
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 ();
- }
[
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 the 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 most likely configured with multiple filter, each step through
- The result of//filter is different, so here, you want to specify a filter to get the word segmentation result ,
- //So the list.size-1 here to write, notice the value here, is not fixed
- for (TokenInfo Token:list.get (List.size ()-1). Gettokens ()) {
- //Get Word segmentation data Results
- Results.add (Token.gettext ());
- }
- }
[ja
In Solr, Mode 2:
Java code
- /***
- * According to the field Word and print the Word result
- * @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");
- //Add 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 may be used for subsequent calls
- list<string> results = new arraylist<string> ();
- //Get results based on field name
- 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 ());
- }
- }
Strategy for highlighting in Lucene or SOLR