浏览 4269 次
锁定老帖子 主题:Lucene实现搜索结果命中关键字高亮显示
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-03-24
最后修改:2009-03-24
使用lucene实现搜索结果 命中关键字高亮显示,大致流程与lucene 建立文件索引和针对索引进行搜索(lucene2.2版本)介绍的一致,只需在代码里稍作修改。 package demo.example.searcher; import java.io.*; import java.util.*; import org.apache.lucene.analysis.standard.*; import org.apache.lucene.index.*; import org.apache.lucene.document.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class Indexer { private static Log log = LogFactory.getLog(Indexer.class); public static void main(String[] args) throws Exception { File indexDir = new File("C:\\index"); File dataDir = new File("C:\\lucene\\src"); long start = new Date().getTime(); int numIndexed = index(indexDir, dataDir); long end = new Date().getTime(); System.out.println("use:" + (end - start)); } public static int index(File indexDir, File dataDir) { int ret = 0; try { IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true); writer.setUseCompoundFile(false); indexDirectory(writer, dataDir); ret = writer.docCount(); writer.optimize(); writer.close(); } catch (Exception e) { e.printStackTrace(); } return ret; } public static void indexDirectory(IndexWriter writer, File dir) { try { File[] files = dir.listFiles(); for (File f : files) { if (f.isDirectory()) { indexDirectory(writer, f); } else { indexFile(writer, f); } } } catch (Exception e) { e.printStackTrace(); } } public static void indexFile(IndexWriter writer, File f) { try { System.out.println("Indexing:" + f.getCanonicalPath()); Document doc = new Document(); Reader txtReader = new FileReader(f); doc.add(new Field("contents", txtReader,Field.TermVector.WITH_POSITIONS_OFFSETS)); doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.UN_TOKENIZED)); writer.addDocument(doc); } catch (Exception e) { e.printStackTrace(); } } }
package demo.example.searcher; import java.util.*; import org.apache.lucene.search.highlight.*; import org.apache.lucene.analysis.*; import org.apache.lucene.search.*; import org.apache.lucene.queryParser.*; import org.apache.lucene.analysis.standard.*; import org.apache.lucene.document.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class Searcher { private static Log log = LogFactory.getLog(Searcher.class); public static void main(String[] args) { String indexDir = "C:\\index"; String q = "查询关键字"; search(indexDir, q); } public static void search(String indexDir, String q) { try { IndexSearcher is = new IndexSearcher(indexDir); QueryParser queryParser = new QueryParser("contents", new StandardAnalyzer()); Query query = queryParser.parse(q); long start = new Date().getTime(); Hits hits = is.search(query); long end = new Date().getTime(); System.out.println("use:" + (end - start));
SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<strong><font color='red'>", "</font></strong>"); SimpleFragmenter fragmenter = new SimpleFragmenter(60);
Highlighter highlighter = new Highlighter(formatter, new QueryScorer(q)); highlighter.setTextFragmenter(fragmenter);
int maxNumFragmentsRequired = 10; String fragmentSeparator = ""; TermPositionVector tpv = null; TokenStream tokenstream = null;
for (int i = 0; i < hits.length(); i++) { Document doc = hits.doc(i);
int id = hits.id(i);
System.out.println("The right file:" + doc.get("filename"));
tpv = (TermPositionVector) is.getIndexReader().getTermFreqVector(id, "contents");
tokenstream = TokenSources.getTokenStream(tpv);
String result = highlighter.getBestFragments(tokenstream, doc.get("contents"), maxNumFragmentsRequired, fragmentSeparator)
System.out.println("The right file context is :" + result);
} } catch (Exception e) { e.printStackTrace(); } } }
"<strong><font color='red'>", "</font></strong>"
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |