Lucene Query Result highlighting

Source: Internet
Author: User

Search results highlighting is very important for the user's experience and friendliness, and can quickly mark the user's search for keywords. The index in this example still uses the index created in the previous blog (lucene query index), which highlights the lucene4.x highlighted fast highlight front.

Implementation results:

Core code
Package Ucas. IR. Lucene;Import Java. IO. File;Import Java(i). IOException;Import Javax. Print. Doc;import org. Apache. Lucene. Analysis. Analyzer;import org. Apache. Lucene. Analysis. Tokenstream;import org. Apache. Lucene. Analysis. Core. Keywordanalyzer;import org. Apache. Lucene. Analysis. Standard. StandardAnalyzer;import org. Apache. Lucene. Document. Document;import org. Apache. Lucene. Index. Directoryreader;import org. Apache. Lucene. Index. IndexWriter;import org. Apache. Lucene. Queryparser. Classic. Queryparser;import org. Apache. Lucene. Search. Indexsearcher;import org. Apache. Lucene. Search. Query;import org. Apache. Lucene. Search. Topdocs;import org. Apache. Lucene. Search. Highlight. Fragmenter;import org. Apache. Lucene. Search. Highlight. Highlighter;import org. Apache. Lucene. Search. Highlight. Queryscorer;import org. Apache. Lucene. Search. Highlight. Simplehtmlformatter;import org. Apache. Lucene. Search. Highlight. Simplespanfragmenter;import org. Apache. Lucene. Search. Highlight. Tokensources;import org. Apache. Lucene. Store. Directory;import org. Apache. Lucene. Store. Fsdirectory;import org. Apache. Lucene. Util. Version;import org. Wltea. Analyzer. Lucene. Ikanalyzer;public class Indexsearch {public static void main (string[] args) {Directory directory = null;try {file Indexpath = new File ("/users/yp/documents/workspace/ucasir/webcontent/index");if (Indexpath. Exists() = True) {Indexpath. Mkdirs();}//Set the index directory to query directories = fsdirectory. Open(Indexpath);Create Indexsearcher directoryreader Dreader = Directoryreader. Open(directory);Indexsearcher searcher = new Indexsearcher (dreader);Set word breaking mode Analyzer analyze2 = new StandardAnalyzer (Version. LUCENE_43);//Standard participleAnalyzer Analyzer = new Ikanalyzer ();Set query domain String field="News_title";Queryparser parser = new Queryparser (Version. LUCENE_43, field, analyzer);Querying string Query query = Parser. Parse("Alfa Dog");Queryscorer scorer=new queryscorer (Query,field);Simplehtmlformatter fors=new Simplehtmlformatter ("<span style=\" color:red;\ "> ","</span>");Highlighter highlighter=new Highlighter (fors, scorer);System. out. println("Query:"+ Query. toString());Before returningTenStrip Topdocs Topdocs = Searcher. Search(Query,Ten);if (Topdocs! = null) {System. out. println("Total documents eligible:"+ Topdocs. Totalhits);for (int i =0; i < topDocs.scoreDocs.length; i++) {Document doc = Searcher. doc(Topdocs. ScoredocsI. doc);Tokenstream tokenstream=tokensources. Getanytokenstream(Searcher. Getindexreader(), Topdocs. ScoredocsI. doc, field, analyzer);Fragmenter Fragment=new Simplespanfragmenter (scorer);Highlighter. Settextfragmenter(fragment); Highlight News_title field String Str=highlighter. Getbestfragment(Tokenstream, Doc. Get("News_title"))///Get the highlighted clip, you can limit its numberSystem. out. println("Highlight title:"+STR);Tokenstream=tokensources. Getanytokenstream(Searcher. Getindexreader(), Topdocs. ScoredocsI. doc,"News_summary", analyzer);Str=highlighter. Getbestfragment(Tokenstream, Doc. Get("News_summary"))///Get the highlighted clip, you can limit its numberSystem. out. println("Highlight Summary:"+STR);}} Directory. Close();Dreader. Close();} catch (Exception e) {E. Printstacktrace();}    }}

Results:

Lucene Query Result 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.