Lucene5学习之多索引目录查询以及多线程查询

简介:

   上一篇中我们使用多线程创建了索引,下面我们来试着采用不把多个索引目录里的数据合并到一个新的索引目录的方式去查询索引数据,当然你也可以合并(合并到一个索引目录查询就很简单了),其实很多情况我们都是不合并到一个索引目录的,那多索引目录该如何查询呢,在Lucene5中使用的MultiReader类,在Lucene4时代,使用的是MultiSearcher类。至于Lucene多线程查询,只需要在构建IndexSearcher对象时传入一个ExecutorService线程池管理对象即可,具体请看下面贴出的示例代码:

Java代码   收藏代码
  1. package com.yida.framework.lucene5.index;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6. import java.util.concurrent.Callable;  
  7. import java.util.concurrent.ExecutionException;  
  8. import java.util.concurrent.ExecutorService;  
  9. import java.util.concurrent.Executors;  
  10. import java.util.concurrent.Future;  
  11.   
  12. import org.apache.lucene.document.Document;  
  13. import org.apache.lucene.index.DirectoryReader;  
  14. import org.apache.lucene.index.IndexReader;  
  15. import org.apache.lucene.index.MultiReader;  
  16. import org.apache.lucene.index.Term;  
  17. import org.apache.lucene.search.IndexSearcher;  
  18. import org.apache.lucene.search.Query;  
  19. import org.apache.lucene.search.TermQuery;  
  20. import org.apache.lucene.store.Directory;  
  21.   
  22. import com.yida.framework.lucene5.util.LuceneUtils;  
  23.   
  24. /** 
  25.  * 多线程多索引目录查询测试 
  26.  * @author Lanxiaowei 
  27.  * 
  28.  */  
  29. public class MultiThreadSearchTest {  
  30.     public static void main(String[] args) throws InterruptedException, ExecutionException, IOException {  
  31.         //每个线程都从5个索引目录中查询,所以最终5个线程的查询结果都一样  
  32.         //multiThreadAndMultiReaderSearch();  
  33.           
  34.         //多索引目录查询(把多个索引目录当作一个索引目录)  
  35.         multiReaderSearch();  
  36.     }  
  37.       
  38.     /** 
  39.      * 多索引目录查询 
  40.      * @throws InterruptedException 
  41.      * @throws ExecutionException 
  42.      * @throws IOException 
  43.      */  
  44.     public static void multiReaderSearch()  throws InterruptedException, ExecutionException, IOException {  
  45.         Directory directory1 = LuceneUtils.openFSDirectory("C:/lucenedir1");  
  46.         Directory directory2 = LuceneUtils.openFSDirectory("C:/lucenedir2");  
  47.         Directory directory3 = LuceneUtils.openFSDirectory("C:/lucenedir3");  
  48.         Directory directory4 = LuceneUtils.openFSDirectory("C:/lucenedir4");  
  49.         Directory directory5 = LuceneUtils.openFSDirectory("C:/lucenedir5");  
  50.         IndexReader reader1 = DirectoryReader.open(directory1);  
  51.         IndexReader reader2 = DirectoryReader.open(directory2);  
  52.         IndexReader reader3 = DirectoryReader.open(directory3);  
  53.         IndexReader reader4 = DirectoryReader.open(directory4);  
  54.         IndexReader reader5 = DirectoryReader.open(directory5);  
  55.         MultiReader multiReader = new MultiReader(reader1,reader2,reader3,reader4,reader5);  
  56.           
  57.         IndexSearcher indexSearcher = LuceneUtils.getIndexSearcher(multiReader);  
  58.         Query query = new TermQuery(new Term("contents","volatile"));  
  59.         List<Document> list = LuceneUtils.query(indexSearcher, query);  
  60.         if(null == list || list.size() <= 0) {  
  61.             System.out.println("No results.");  
  62.             return;  
  63.         }  
  64.         for(Document doc : list) {  
  65.             String path = doc.get("path");  
  66.             //String content = doc.get("contents");  
  67.             System.out.println("path:" + path);  
  68.             //System.out.println("contents:" + content);  
  69.         }  
  70.     }  
  71.       
  72.     /** 
  73.      * 多索引目录且多线程查询,异步收集查询结果 
  74.      * @throws InterruptedException 
  75.      * @throws ExecutionException 
  76.      * @throws IOException 
  77.      */  
  78.     public static void multiThreadAndMultiReaderSearch()  throws InterruptedException, ExecutionException, IOException {  
  79.         int count = 5;  
  80.         ExecutorService pool = Executors.newFixedThreadPool(count);  
  81.           
  82.         Directory directory1 = LuceneUtils.openFSDirectory("C:/lucenedir1");  
  83.         Directory directory2 = LuceneUtils.openFSDirectory("C:/lucenedir2");  
  84.         Directory directory3 = LuceneUtils.openFSDirectory("C:/lucenedir3");  
  85.         Directory directory4 = LuceneUtils.openFSDirectory("C:/lucenedir4");  
  86.         Directory directory5 = LuceneUtils.openFSDirectory("C:/lucenedir5");  
  87.         IndexReader reader1 = DirectoryReader.open(directory1);  
  88.         IndexReader reader2 = DirectoryReader.open(directory2);  
  89.         IndexReader reader3 = DirectoryReader.open(directory3);  
  90.         IndexReader reader4 = DirectoryReader.open(directory4);  
  91.         IndexReader reader5 = DirectoryReader.open(directory5);  
  92.         MultiReader multiReader = new MultiReader(reader1,reader2,reader3,reader4,reader5);  
  93.           
  94.         final IndexSearcher indexSearcher = LuceneUtils.getIndexSearcher(multiReader, pool);  
  95.         final Query query = new TermQuery(new Term("contents","volatile"));  
  96.         List<Future<List<Document>>> futures = new ArrayList<Future<List<Document>>>(count);  
  97.         for (int i = 0; i < count; i++) {  
  98.             futures.add(pool.submit(new Callable<List<Document>>() {  
  99.                 public List<Document> call() throws Exception {  
  100.                     return LuceneUtils.query(indexSearcher, query);  
  101.                 }  
  102.             }));  
  103.         }  
  104.           
  105.         int t = 0;  
  106.         //通过Future异步获取线程执行后返回的结果  
  107.         for (Future<List<Document>> future : futures) {  
  108.             List<Document> list = future.get();  
  109.             if(null == list || list.size() <= 0) {  
  110.                 t++;  
  111.                 continue;  
  112.             }  
  113.             for(Document doc : list) {  
  114.                 String path = doc.get("path");  
  115.                 //String content = doc.get("contents");  
  116.                 System.out.println("path:" + path);  
  117.                 //System.out.println("contents:" + content);  
  118.             }  
  119.             System.out.println("");  
  120.         }  
  121.         //释放线程池资源  
  122.         pool.shutdown();  
  123.           
  124.         if(t == count) {  
  125.             System.out.println("No results.");  
  126.         }  
  127.     }  
  128. }  

当然你也可以把上面的代码改造成每个线程查询一个索引目录,我上面是每个线程都从5个索引目录中查询,所以结果会打印5次,看到运行结果请不要感到奇怪。

 

如果你还有什么问题请加我Q-Q:7-3-6-0-3-1-3-0-5,

或者加裙
一起交流学习!

转载:http://iamyida.iteye.com/blog/2196932

目录
相关文章
|
1月前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
|
1月前
|
安全 编译器 C#
C#学习相关系列之多线程---lock线程锁的用法
C#学习相关系列之多线程---lock线程锁的用法
|
1月前
|
C#
C#学习相关系列之多线程---ConfigureAwait的用法
C#学习相关系列之多线程---ConfigureAwait的用法
|
1月前
|
C#
C#学习相关系列之多线程---TaskCompletionSource用法(八)
C#学习相关系列之多线程---TaskCompletionSource用法(八)
|
1月前
|
Java C#
C#学习系列相关之多线程(五)----线程池ThreadPool用法
C#学习系列相关之多线程(五)----线程池ThreadPool用法
|
3月前
|
存储 Java 调度
从零开始学习 Java:简单易懂的入门指南之线程池(三十六)
从零开始学习 Java:简单易懂的入门指南之线程池(三十六)
|
3月前
|
Java 调度
从零开始学习 Java:简单易懂的入门指南之多线程(三十四)
从零开始学习 Java:简单易懂的入门指南之多线程(三十四)
|
3月前
|
数据处理
多线程与并发编程【线程对象锁、死锁及解决方案、线程并发协作、生产者与消费者模式】(四)-全面详解(学习总结---从入门到深化)
多线程与并发编程【线程对象锁、死锁及解决方案、线程并发协作、生产者与消费者模式】(四)-全面详解(学习总结---从入门到深化)
43 1
|
3月前
|
设计模式 监控 安全
多线程设计模式【多线程上下文设计模式、Guarded Suspension 设计模式、 Latch 设计模式】(二)-全面详解(学习总结---从入门到深化)
多线程设计模式【多线程上下文设计模式、Guarded Suspension 设计模式、 Latch 设计模式】(二)-全面详解(学习总结---从入门到深化)
62 0
|
1月前
|
C#
C#学习系列相关之多线程(二)----Thread类介绍
C#学习系列相关之多线程(二)----Thread类介绍

热门文章

最新文章