zoukankan      html  css  js  c++  java
  • 用Lucene实现在检索结果中再检索

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

    上面是简单的测试程序。当然在实际应用中可以做得比较复杂。
  • 相关阅读:
    C++调用dll
    地图匹配技术在车载导航系统中的应用
    卡尔曼滤波器原理说明
    SharePoint 2010 托管元数据批量操作小工具
    SharePoint 常用开发工具
    tagMapping 在SharePoint中的应用
    SharePoint 2010 通配符搜索
    SharePoint 2010 一步步创建发布式站点 创建母版页二
    为SharePoint 文档库图片创建缩略图
    在Visual Studio 2010 快速查找Assembly 信息
  • 原文地址:https://www.cnblogs.com/huazai8204/p/808585.html
Copyright © 2011-2022 走看看