Highlighter高亮


package com.bjsxt.indexsearch;

import jeasy.analysis.MMAnalyzer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Formatter;
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.Scorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;

import com.bjsxt.utils.File2DocumentUtil;

public class IndexAndSearch {

/**
* @param args
*/
String filePath = "D:\\flexWorkespace\\LuceneDemo\\luceneDataSource\\小笑话_总统的房间 Room .txt";
String indexPath = "D:\\flexWorkespace\\LuceneDemo\\indexPath";

// Analyzer analyzer = new StandardAnalyzer();
Analyzer analyzer = new MMAnalyzer();

public static void main(String[] args) throws Exception {
// new IndexAndSearch().createIndexByDir();
new IndexAndSearch().search("房间");
}

public void createIndex() throws Exception {
// File file = new File(filePath);
Document doc = File2DocumentUtil.file2Document(filePath);

// IndexWriter 是用来操作(增删改)索引库的
IndexWriter iw = new IndexWriter(indexPath, analyzer, true,
MaxFieldLength.LIMITED);
iw.addDocument(doc);
iw.close();
}

public void createIndexByDir()throws Exception {

//1.创建时候载入文件系统里的索引
Directory fsDir = FSDirectory.getDirectory(indexPath);
Directory ramDir = new RAMDirectory(fsDir);
//new ramIndexWriter时不需要重新创建
IndexWriter ramIndexWriter = new IndexWriter(ramDir, analyzer, MaxFieldLength.LIMITED);
//添加document
Document doc = File2DocumentUtil.file2Document(filePath);
ramIndexWriter.addDocument(doc);
ramIndexWriter.close();

//2.退出时保存内存里的索引
//new fsIndexWriter时需要重新创建,即删除原来的索引文件
IndexWriter fsIndexWriter = new IndexWriter(fsDir, analyzer, true, MaxFieldLength.LIMITED);
// Directory[] dir = {ramDir};
//保存内存里的索引
fsIndexWriter.addIndexesNoOptimize(new Directory[]{ramDir});
//把内存里的东西提交后才优化
fsIndexWriter.commit();
// 优化索引文件,把多个cfs文件合并成一个
fsIndexWriter.optimize();
fsIndexWriter.close();
}

public void search(String queryStr) throws Exception {
String[] fields = { "content","name" };
QueryParser queryParser = new MultiFieldQueryParser(fields, analyzer);
Query query = null;
Filter filter = null;
query = queryParser.parse(queryStr);
IndexSearcher indexSearcher = null;
indexSearcher = new IndexSearcher(indexPath);
TopDocs topDocs = indexSearcher.search(query, filter, 1000);
System.out.println("总共有" + topDocs.totalHits + "条记录:");
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
int docNum = scoreDoc.doc;
Document doc = indexSearcher.doc(docNum);
//---------------------------准备高亮器
Formatter formatter = new SimpleHTMLFormatter("<font color='red'>","</font>");
Scorer scorer = new QueryScorer(query);
Highlighter highlighter = new Highlighter(formatter,scorer);
Fragmenter fragmenter = new SimpleFragmenter(50);
highlighter.setTextFragmenter(fragmenter);
//---------------------------

//--------------------------高亮
//将高亮后的结果返回,如果当前属性值中没有出现关键字,则返回空
String hc = highlighter.getBestFragment(analyzer, "content", doc.get("content"));
if(hc == null){
String content = doc.get("content");
int endIndex = Math.min(50,content.length());System.out.println(content.length());
hc = content.substring(0,endIndex);
}
doc.getField("content").setValue(hc);
//--------------------------


File2DocumentUtil.printDocumentInfo(doc);
}
}

public void searchByDir(String queryStr) throws Exception {
String[] fields = { "name", "content" };
QueryParser queryParser = new MultiFieldQueryParser(fields, analyzer);
Query query = null;
Filter filter = null;
query = queryParser.parse(queryStr);
IndexSearcher indexSearcher = null;
Directory fsDir = FSDirectory.getDirectory(indexPath);
indexSearcher = new IndexSearcher(fsDir);
TopDocs topDocs = indexSearcher.search(query, filter, 1000);
System.out.println("总共有" + topDocs.totalHits + "条记录:");
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
int docNum = scoreDoc.doc;
Document doc = indexSearcher.doc(docNum);
File2DocumentUtil.printDocumentInfo(doc);
}
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值