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