Lucene 4.7--highlighting

Source: Internet
Author: User

Jar

lucene-analyzers-common-4.7.0. Jarlucene-analyzers-smartcn-4.7.0. Jarlucene-core-4.7.0. Jarlucene-queryparser-4.7.0. Jarlucene-memory-4.7.0. Jarlucene-queries-4.7.0. Jarlucene-highlighter-4.7.0.jar

First section of the query code

New Hashset<string>();  Fields.Add ("Carid");  Fields.Add ("Carname");     = Searcher.doc (scoredoc.doc, fields);   New Carvo ();  Resultvo.setcarid (Document.getvalues ("Carid") [0]);  Resultvo.setcarname (Document.getvalues ("Carname") [0]);   return

Here, if we search for a car through carname, then we want to be able to highlight the matching value in this carname, then highlight the setcarname in this resultvo, as follows:

This is called:

Resultvo.setcarname (Highlightformat (Document.getvalues ("Carname") [0], "carname", Query,scoredoc, Document));

Implementation method:

Privatestring Highlightformat (String value, string field, query query, Scoredoc scoredoc, document document) /c3>throwsException {Queryscorer scorer=NewQueryscorer (Query,field); Simplehtmlformatter Hlightformater=NewSimplehtmlformatter ("<span class=\" hlight\ ">", "</span>"); Highlighter HL=Newhighlighter (hlightformater,scorer); Hl.settextfragmenter (NewSimplespanfragmenter (scorer)); Tokenstream Stream=Tokensources.getanytokenstream (Searcher.getindexreader (), Scoredoc.doc, field, document, analyzer); String Valuewithhighlight=hl.getbestfragment (stream, value); returnValuewithhighlight; } 

Searcher is Indexsearcher,anlyzer is a word breaker, which I usually hold as a static variable for a long time

The rest of this "big pile" of things I do not intend to do too much explanation, one is here and the original version of the change is not small, the second is basically in accordance with these points are enough to meet the basic development application needs, the only need to pay attention to, also need to deal with ourselves, is

New Simplehtmlformatter ("<span class=\" hlight\ ">", "</span>");  

Of course, here's a reminder, if it's your carname. This attribute is for users to customize the input, be sure to pay attention to the interception of injection attacks

Lucene 4.7--highlighting

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.