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

    上面是简单的测试程序。当然在实际应用中可以做得比较复杂。
  • 相关阅读:
    基于Lumisoft.NET实现的邮件发送功能
    jqueryautocomplete 使用手册
    asp.net访问网络路径方法模拟用户登录
    JavaScript判断浏览器类型及版本
    解决jquery.autocomplete在IE6下被下拉框遮住的问题
    How to resovle aspnet_merge.exe error issue in web deployment project
    敏捷开发之Scrum扫盲篇
    JS 异常: Uncaught RangeError: Maximum call stack size exceeded
    HTTP请求的 转发 重定向 代理
    JS跨域访问 控制
  • 原文地址:https://www.cnblogs.com/huazai8204/p/808585.html
Copyright © 2011-2022 走看看