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是如何存储需要检索的数据,以及如何完成高效的数据检索。
相关文章
- python多线程多进程脚本
- 3-多线程安全问题,线程同步
- c语言 windows多线程_C语言 多线程
- 开心档之C++ 多线程
- Oracle 12c 多线程数据库操作 的新特性详解
- Linux下的多线程编程详解程序员
- Java 多线程之内置锁与显示锁详解编程语言
- python编写的一个通过多线程扫描端口的代码详解编程语言
- Java之多线程断点下载的实现详解编程语言
- 40个Java多线程问题总结,新手必备详解编程语言
- 多线程Linux:实现更强大的系统服务(线程linux)
- Linux线程间共享内存机制剖析(linux多线程共享内存)
- 多线程高效缩短Redis等待时间(多线程的redis)
- 多线程环境下Redis出现的阻塞问题(多线程下redis阻塞)
- 多线程问题Redis缓存受阻(多线程redis造成阻塞)
- c#.net多线程编程教学——线程同步
- php多线程上下文中安全写文件实现代码
- 解析php多线程下载远程多个文件
- java多线程编程之为什么要进行数据同步
- c#多线程编程基础
- winform基于异步委托实现多线程摇奖器