zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

多线程索引学习 查询 以及 目录 之多
2023-09-14 08:59:37 时间

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


import java.util.concurrent.Callable;   import java.util.concurrent.ExecutionException;   import java.util.concurrent.ExecutorService;   import java.util.concurrent.Executors;   import java.util.concurrent.Future;   import org.apache.lucene.document.Document;   import org.apache.lucene.index.DirectoryReader;   import org.apache.lucene.index.IndexReader;   import org.apache.lucene.index.MultiReader;   import org.apache.lucene.index.Term;   import org.apache.lucene.search.IndexSearcher;   import org.apache.lucene.search.Query;   import org.apache.lucene.search.TermQuery;   import org.apache.lucene.store.Directory;   import com.yida.framework.lucene5.util.LuceneUtils;    * 多线程多索引目录查询测试   * @author Lanxiaowei   */   public class MultiThreadSearchTest {       public static void main(String[] args) throws InterruptedException, ExecutionException, IOException {           //每个线程都从5个索引目录中查询,所以最终5个线程的查询结果都一样           //multiThreadAndMultiReaderSearch();                      //多索引目录查询(把多个索引目录当作一个索引目录)           multiReaderSearch();       }              /**       * 多索引目录查询       * @throws InterruptedException       * @throws ExecutionException       * @throws IOException       */       public static void multiReaderSearch()  throws InterruptedException, ExecutionException, IOException {           Directory directory1 = LuceneUtils.openFSDirectory("C:/lucenedir1");           Directory directory2 = LuceneUtils.openFSDirectory("C:/lucenedir2");           Directory directory3 = LuceneUtils.openFSDirectory("C:/lucenedir3");           Directory directory4 = LuceneUtils.openFSDirectory("C:/lucenedir4");           Directory directory5 = LuceneUtils.openFSDirectory("C:/lucenedir5");           IndexReader reader1 = DirectoryReader.open(directory1);           IndexReader reader2 = DirectoryReader.open(directory2);           IndexReader reader3 = DirectoryReader.open(directory3);           IndexReader reader4 = DirectoryReader.open(directory4);           IndexReader reader5 = DirectoryReader.open(directory5);           MultiReader multiReader = new MultiReader(reader1,reader2,reader3,reader4,reader5);                      IndexSearcher indexSearcher = LuceneUtils.getIndexSearcher(multiReader);           Query query = new TermQuery(new Term("contents","volatile"));           List Document  list = LuceneUtils.query(indexSearcher, query);           if(null == list || list.size()  = 0) {               System.out.println("No results.");               return;           }           for(Document doc : list) {               String path = doc.get("path");               //String content = doc.get("contents");               System.out.println("path:" + path);               //System.out.println("contents:" + content);           }       }              /**       * 多索引目录且多线程查询,异步收集查询结果       * @throws InterruptedException       * @throws ExecutionException       * @throws IOException       */       public static void multiThreadAndMultiReaderSearch()  throws InterruptedException, ExecutionException, IOException {           int count = 5;           ExecutorService pool = Executors.newFixedThreadPool(count);                      Directory directory1 = LuceneUtils.openFSDirectory("C:/lucenedir1");           Directory directory2 = LuceneUtils.openFSDirectory("C:/lucenedir2");           Directory directory3 = LuceneUtils.openFSDirectory("C:/lucenedir3");           Directory directory4 = LuceneUtils.openFSDirectory("C:/lucenedir4");           Directory directory5 = LuceneUtils.openFSDirectory("C:/lucenedir5");           IndexReader reader1 = DirectoryReader.open(directory1);           IndexReader reader2 = DirectoryReader.open(directory2);           IndexReader reader3 = DirectoryReader.open(directory3);           IndexReader reader4 = DirectoryReader.open(directory4);           IndexReader reader5 = DirectoryReader.open(directory5);           MultiReader multiReader = new MultiReader(reader1,reader2,reader3,reader4,reader5);                      final IndexSearcher indexSearcher = LuceneUtils.getIndexSearcher(multiReader, pool);           final Query query = new TermQuery(new Term("contents","volatile"));           List Future List Document  futures = new ArrayList Future List Document (count);           for (int i = 0; i   count; i++) {               futures.add(pool.submit(new Callable List Document () {                   public List Document  call() throws Exception {                       return LuceneUtils.query(indexSearcher, query);                   }               }));           }                      int t = 0;           //通过Future异步获取线程执行后返回的结果           for (Future List Document  future : futures) {               List Document  list = future.get();               if(null == list || list.size()  = 0) {                   t++;                   continue;               }               for(Document doc : list) {                   String path = doc.get("path");                   //String content = doc.get("contents");                   System.out.println("path:" + path);                   //System.out.println("contents:" + content);               }               System.out.println("");           }           //释放线程池资源           pool.shutdown();                      if(t == count) {               System.out.println("No results.");           }       }  

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

 

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

或者加裙
一起交流学习!

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


【MySQL从入门到精通】【高级篇】(二十六)建了索引就能用么?我看未必。来看看几种索引失效的情况吧 【MySQL从入门到精通】【高级篇】(二十五)EXPLAIN中ref、rows、filtered、Extra字段的剖析 通过前面几篇文章的学习,相信小伙伴们对EXPLAIN命令有了一个更加深入理解。这篇文章我们将来学习索引失效的11种情况。有时候并不是说加了索引,就一定能用上索引,还是要具体情况具体分析。
性能优化技巧 - 查找 SPL为用户提供了强大的索引机制以及针对不同场景中各对象的查询函数,善加运用,可以显著提高查询性能。 1 键值查找1.1 序表我们先建立一个份“通话记录”的模拟数据,通过这份数据,来比较一下不同查询函数对序表查询性能的影响。
MySQL优化系列(二)--查找优化(1)(非索引设计) MySQL优化系列(二)--查找优化(1)(非索引设计) 接下来这篇是查询优化,用户80%的操作基本都在查询,我们有什么理由不去优化他呢??所以这篇博客将会讲解大量的查询优化(索引以及库表结构优化等高级用法后面文章再讲),先讲单表查优化,再讲多表查优化。
Lucene 查询原理 # 前言 Lucene 是一个基于 Java 的全文信息检索工具包,目前主流的搜索系统Elasticsearch和solr都是基于lucene的索引和搜索能力进行。想要理解搜索系统的实现原理,就需要深入lucene这一层,看看lucene是如何存储需要检索的数据,以及如何完成高效的数据检索。