http://www.matrix.org.cn/thread.shtml?topicId=753ba0a5-125e-11dc-b33a-df989147150e&forumId=32
Lucene是可以做到的,利用lucene的Filter,具体可以查看lucene的api中的org.apache.lucene.search.CachingWrapperFilter,它可以缓存上次的搜索结果,从而实现在结果中的搜索。
测试实例:
package com.wsjava;
import java.io.IOException;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.CachingWrapperFilter;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryFilter;
public class IndexTest {
/**
* @param args
* @throws ParseException
* @throws IOException
*/
public static void main(String[] args) throws IOException, ParseException {
index();
search("day"); //简单搜索
searchInResult("day", "you"); //在结果集中搜索
}
public static void index() throws IOException {
IndexWriter writer = new IndexWriter("d:/tesindex",new SimpleAnalyzer(), true);
writer.setMaxMergeDocs(1000);
writer.setMergeFactor(100);
for (int i = 0; i < 10; i++) {
Document doc = new Document();
String content = "How do you do?";
if (i >= 5) {
content = "What's a good day. ";
}
if (i >= 7) {
content = "Nice day. Thanks you!";
}
doc.add(new Field("content", content, Field.Store.YES,Field.Index.TOKENIZED));
writer.addDocument(doc);
}
}
//简单实现对qw的搜索.
public static void search(String qw) throws IOException, ParseException {
QueryParser queryParser = new QueryParser("content",new SimpleAnalyzer());
Query query = queryParser.parse(qw.trim());
QueryFilter filter = new QueryFilter(query);
search(query, filter);
}
//在搜索oldqw的结果集中搜索qw.
public static void searchInResult(String qw, String oldqw) throws ParseException, IOException {
QueryParser queryParser = new QueryParser("content",new SimpleAnalyzer());
Query query = queryParser.parse(qw.trim());
Query oldQuery = queryParser.parse(oldqw.trim());
QueryFilter oldFilter = new QueryFilter(oldQuery);
CachingWrapperFilter filter = new CachingWrapperFilter(oldFilter);
search(query, filter);
}
private static void search(Query query, Filter filter) throws IOException, ParseException {
IndexSearcher ins = new IndexSearcher("d:/tesindex");
Hits hits = ins.search(query, filter);
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
System.out.println(doc.get("content"));
}
System.out.println();
}
}
上面是简单的测试程序。当然在实际应用中可以做得比较复杂。
转载于:https://www.cnblogs.com/huazai8204/archive/2007/07/06/808585.html