public class SearcherUtil {
public void searcher01() {
try {
IndexSearcher searcher = new IndexSearcher(IndexReader.open(FileIndexUtils.getDirectory()));
TermQuery query = new TermQuery(new Term("content","沈"));
TopDocs tds = searcher.search(query,20);
for(ScoreDoc sd:tds.scoreDocs) {
Document doc = searcher.doc(sd.doc);
System.out.println(doc.get("title"));
}
searcher.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void searcherByHighlighter(String name) {
try {
Analyzer a = new MMSegAnalyzer();
IndexSearcher searcher = new IndexSearcher(IndexReader.open(FileIndexUtils.getDirectory()));
//QueryParser parser = new QueryParser(Version.LUCENE_35,"title",a);
MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, new String[]{"title","content"}, a);
Query query = parser.parse(name);
TopDocs tds = searcher.search(query, 20);
MoreLikeThis mlt = new MoreLikeThis(searcher.getIndexReader());
mlt.setFieldNames(new String[]{"title","content"});
mlt.setMinDocFreq(1);
mlt.setMinTermFreq(1);
for(ScoreDoc sd:tds.scoreDocs) {
Document doc = searcher.doc(sd.doc);
String title = doc.get("title");
title = lighterStr(a, query, title, "title");
System.out.println(title);
System.out.println("**************************************************************************************************");
Query moreLike = mlt.like(sd.doc);
TopDocs stds = searcher.search(moreLike, 10);
for(ScoreDoc ssd:stds.scoreDocs) {
Document d = searcher.doc(ssd.doc);
System.out.println(d.get("title"));
}
}
searcher.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (InvalidTokenOffsetsException e) {
e.printStackTrace();
}
}
public void searcherByFastHighlighter(String name) {
try {
FastVectorHighlighter fvh = new FastVectorHighlighter(false,false);
Analyzer a = new MMSegAnalyzer();
IndexSearcher searcher = new IndexSearcher(IndexReader.open(FileIndexUtils.getDirectory()));
//QueryParser parser = new QueryParser(Version.LUCENE_35,"title",a);
MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_35, new String[]{"title","content"}, a);
Query query = parser.parse(name);
FieldQuery fq = fvh.getFieldQuery(query);
TopDocs tds = searcher.search(query, 20);
for(ScoreDoc sd:tds.scoreDocs) {
String highTitle = fvh.getBestFragment(fq, searcher.getIndexReader(), sd.doc, "title", 100);
System.out.println(highTitle);
System.out.println("-------------------------");
String highContent = fvh.getBestFragment(fq, searcher.getIndexReader(), sd.doc, "content",100);
System.out.println(highContent);
System.out.println("**********************************************************************");
}
searcher.close();
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String lighterStr(Analyzer a,Query query,String txt,String fieldname) throws IOException, InvalidTokenOffsetsException {
String str = null;
QueryScorer scorer = new QueryScorer(query);
Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);
Formatter fmt = new SimpleHTMLFormatter("<b>", "</b>");
Highlighter lighter = new Highlighter(fmt, scorer);
lighter.setTextFragmenter(fragmenter);
str = lighter.getBestFragments(a.tokenStream(fieldname,new StringReader(txt)),txt, 3, "......\n");
if(str==null)return txt;
return str;
}
public void lighter01() {
try {
String txt = "我愛北京天安門,天安門上彩旗飛,偉大領袖毛主席,指引咱們向前進,向前進!!!想起身離開東京法律思考的機會那個上的講話那偉大的個聖誕sadfsadnfl.sajdfl;aksjdf;lsadfsadfm.asd那是確定激發了深入的機會拉薩寬帶計費了那個傻大姐華納公司的機會節賀卡就是對話框那是國天安門際 北京電話卡開始覺啊 北京得人們大會堂 北京!!!!";
Query query = new QueryParser(Version.LUCENE_35, "f", new MMSegAnalyzer()).parse("北京 偉大");
QueryScorer scorer = new QueryScorer(query);
Fragmenter fragment = new SimpleSpanFragmenter(scorer);
Formatter formatter = new SimpleHTMLFormatter("<span style='color:red'>", "</span>");
Highlighter highlighter = new Highlighter(formatter,scorer);
highlighter.setTextFragmenter(fragment);
String str = highlighter.getBestFragment(new MMSegAnalyzer(), "f", txt);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidTokenOffsetsException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}