核心提示:publicclassTermVectorTest{Analyzeranalyzer=newSimpleAnalyzer(); DirectoryramDir=newRAMDirectory();publicvoidcreateRamIndex()throwsCorruptIndexExceptio...
public class TermVectorTest {
Analyzer analyzer = new SimpleAnalyzer();
Directory ramDir = new RAMDirectory();
public void createRamIndex() throws CorruptIndexException, LockObtainFailedException, IOException{
IndexWriter writer = new IndexWriter(ramDir,analyzer,IndexWriter.MaxFieldLength.LIMITED);
Document doc1 = new Document();
doc1.add(new Field("title","java",Store.YES,Index.ANALYZED));
doc1.add(new Field("author","callan",Store.YES,Index.ANALYZED));
doc1.add(new Field("subject","java一门编程语言,用java的人很多,编程语言也不少,但是java最流行",Store.YES,Index.ANALYZED,TermVector.WITH_POSITIONS_OFFSETS));
Document doc2 = new Document();
doc2.add(new Field("title","english",Store.YES,Index.ANALYZED));
doc2.add(new Field("author","wcq",Store.YES,Index.ANALYZED));
doc2.add(new Field("subject","英语用的人很多",Store.YES,Index.ANALYZED,TermVector.WITH_POSITIONS_OFFSETS));
Document doc3 = new Document();
doc3.add(new Field("title","asp",Store.YES,Index.ANALYZED));
doc3.add(new Field("author","ca",Store.YES,Index.ANALYZED));
doc3.add(new Field("subject","英语用的人很多",Store.YES,Index.ANALYZED,TermVector.WITH_POSITIONS_OFFSETS));
writer.addDocument(doc1);
writer.addDocument(doc2);
writer.addDocument(doc3);
writer.optimize();
writer.close();
}
public void search() throws CorruptIndexException, IOException{
IndexReader reader = IndexReader.open(ramDir);
IndexSearcher searcher = new IndexSearcher(reader);
Term term = new Term("title","java");
TermQuery query = new TermQuery(term);
Hits hits = searcher.search(query);
for (int i = 0; i < hits.length(); i++)
{
Document doc = hits.doc(i);
System.out.println(doc.get("title"));
System.out.println(doc.get("subject"));
System.out.println("moreLike search: ");
morelikeSearch(reader,hits.id(i));
}
}
private void morelikeSearch(IndexReader reader,int id) throws IOException
{
TermFreqVector vector = reader.getTermFreqVector(id, "subject");
BooleanQuery query = new BooleanQuery();
for (int i = 0; i < vector.size(); i++)
{
TermQuery tq = new TermQuery(new Term("subject",
vector.getTerms()[i]));
query.add(tq, BooleanClause.Occur.SHOULD);
}
IndexSearcher searcher = new IndexSearcher(ramDir);
Hits hits = searcher.search(query);
}
public void highterLightSearch() throws CorruptIndexException, IOException{
IndexReader reader = IndexReader.open(ramDir);
IndexSearcher searcher = new IndexSearcher(reader);
TermQuery query = new TermQuery(new Term("subject","java"));
Hits hits = searcher.search(query);
SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<font color='red'>","</font>");
Highlighter highlighter =new Highlighter(simpleHTMLFormatter,new QueryScorer(query));
highlighter.setTextFragmenter(new SimpleFragmenter(100));
for(int i = 0; i < hits.length(); i++){
Document doc = hits.doc(i);
TermPositionVector termFreqVector = (TermPositionVector)reader.getTermFreqVector(hits.id(i), "subject");
TermFreqVector vector = reader.getTermFreqVector(hits.id(i), "subject");
TokenStream tokenStream = TokenSources.getTokenStream(termFreqVector);
String result = highlighter.getBestFragment(tokenStream, doc.get("subject"));
System.out.println(doc.get("title"));
System.out.println(result);
}
}
public static void main(String[] args) throws CorruptIndexException, IOException
{
TermVectorTest t = new TermVectorTest();
t.createRamIndex();
t.search();
}
}
文章来源:http://www.xinxilong.com