lucene-对多个索引的搜索和多线程搜索

1、如果应用程序架构由多个LUCENE索引组成,则可以通过MutltiSearcher把所有索引搜索。也可以通过ParallelMultiSearcher进行多线程搜索。在单核的情况下,MultiSearcher比ParallelMultiSearcher性能更高。

2、MultiSearcher

搜索2个搜索,把动物按首字母在字母表中的位置分成2部分,一部分一个索引

public class MultisearcherTest extends TestCase{

   private Indexsearcher[] searchers;

   public void setUp() throws Exception{ 

        String[] animals={"aardvark","beaver","coati","dog","lemur",

                          "python","vicuna","zebra"};

        Ananlyzer analyzer=ne WhitespaceAnalyzer();

        Directory aTomDirectory =new RAMDirectory();

        Directory nTOzDirectory=new RAMDirectory();

//建立2个索引

        IndexWriter aTomwriter=new IndexWriter(atomDirectory,analyzer,true);

        IndexWriter nTozwriter=new IndexWriter(aTozDirectory,analyzer,true);

 

        for (int i=0;i<anaimals.length;i+){

            Document doc=new Document();

            String animal=animals[i];

            doc.add(Filed.Keyword("animal",animal));

            if (animal.compareToIgnoreCase("n")<0){

               aTomWriter.addDocument(doc);//前半部分索引a-m

            }else{

               nTozWriter.addDocument(doc);//后半部分索引 n-z

            }

        }

 

        aTomwriter.close();

        nTozwriter.close();

        searchers=newIndexsearcher[2];

        searcher[0]=new IndexSearcher(aTOmDirectory);

        searcher[1]=new IndexSearhcer(nTOzDirectory);

 

 

        }

        public void testMulti() throws Exception{

            MultiSearcher searcher=new MultiSearcher(searchers);

            //对2个索引进行搜索

            Query query=new RangeQuery(new Term("animal","h"),new Term("animal","t"),true);

            Hits hits= searcher.search(query);

        }

 }

3、ParallelMultiSearcher多线程搜索

    搜索操作为每个Searchable分配一个线程,直到所有线程都完成其搜索。基本搜索和进行过滤的搜索是并行执行的。

   lucene通过RMI为用户提供搜索远程索引功能。

   RMI服务器绑定了一个RemoteSearchable的实例,它和IndexSearcher、MultiSearch一样,实现Searchable接口

 

1)把文档按26个字母切分为26个索引。服务器端向客户端提供2个RMI调用

public class SearchServer{

    private static final String ALPHABET="abcdefghijklmnopqrstuvwxyz";

    public static void main(String[] args) throws Exceptino{

        if (args.length!=1){

            System.err.printLn("Usage:Searchserver<basedir>");

            System.exit(-1);

        }

        String basedir=args[0];

//为每个索引建立一个IndexSearcher对象

        Searchable[] searchables=new Searchable[ALPHABET.length()];

        for (int i=0;i<ALPHABET.length;i++){

             searchables[]=new IndexSearcher(new File(basedir,""+ALPHABET.charAt(i)).getAbsolutePath());              

        }    

//注册可供客户端调用服务的端口

        LocateRegistry.createRegistry(1099);

//使用     multiSearcher完成所有索引的搜索  

        Searcher multiSearcher=new MultiSearcher(searchables);

        RemoteSearchable multiImpl=new RemoteSearchables(multiSearcher);

        Naming.rebind("//localhost/LIA_Multi",multiImpl);//注册RMI方法

//使用   parallelSearcher   完成搜索 

        Searcher parallelSearcher=new ParallelMultiSearcher(searchables);

        RemoteSearchable parallelImpl=new RemoteSearchables(parallelSearcher);

        Naming.rebind("//localhost/LIA_Parallel",parallelImpl);//注册RMI方法       

        System.out.println("server started");

        }

    }

2)客户端

public class SearchClient{

   private static HashMap searchercache=new HashMap();

 

   public static void main(String[] args) throws Exception{

      if (args.length!=1){

         System.err.println("Usage:SearchClient <query>");

         System.exit(-1);

      }

      String word=args[0];

      for (int i=0;i<5;i++){

          search("LIA_Multi",word);//调用服务器的multi方法搜索

          search("LIA_Multi",word);//调用服务器的multi方法搜索       

      }      
  }

  private static void search(String name,String word) throws Exception{

      TermQuery query=new TermQuery(new Term("word",word));

      MultiSearcher searcher=(MultiSearcher) searcherCache.get(name);//检查缓存中是否有该搜索器,该搜索器是带缓存功能的

      if (searcher==null){//没有该搜索,则生成新的搜索

           searcher=new MultiSearcher(new Searchable[]{lookupRemote(name)});

           searcherCache.put(name,searcher);

      }

//统计时间

      long begin=new Date().getTime();

      Hits hits=searcher.search(query);

      long end=new Date().getTime()

 

      ...........

      ...........

      //不要关闭searcher对象

     }

     private static  Searchable lookupRemote(String name) throws Exception{

          return (Searchable) Naming.lookup("//localhost/"+name);

     }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值