用lucene实现在上次检索结果中再检索


感谢您的文章

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 {

               
                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();
                }
}

上面是简单的测试程序。当然在实际应用中可以做得比较复杂。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值