惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
B
Blog RSS Feed
C
Check Point Blog
D
Docker
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research
量子位
有赞技术团队
有赞技术团队
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
M
MIT News - Artificial intelligence
B
Blog
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
月光博客
月光博客

博客园 - Xsi64

STS或eclipse安装SVN插件 Html解析类的新选择CsQuery Tomcat编码问题 在64位平台上的Lucene,应该使用MMapDirectory[转] Java properties文件用法 apache commons pool deb软件包安装和卸载 maven GroupId 和ArtifactId通常填什么 mongodb first mongodb mongod 启动参数 Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart 在CentOS上安装tomcat CentOS上安装 jdk centOS上安装redis Linux CentOS修改网卡IP/网关设置 vim使用大全 [Redis] redis-cli 命令总结 CentOS装机必备-基本设置以及缺失文件 maven scope含义的说明
Cache Lucene IndexReader with Apache Commons Pool
Xsi64 · 2014-06-06 · via 博客园 - Xsi64

import org.apache.commons.pool.KeyedObjectPool;

import org.apache.commons.pool.impl.GenericKeyedObjectPool;

import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;

import org.apache.lucene.document.Document;

import org.apache.lucene.index.IndexReader;

import org.apache.lucene.index.Term;

import org.apache.lucene.search.IndexSearcher;

import org.apache.lucene.search.NGramPhraseQuery;

import org.apache.lucene.search.ScoreDoc;

import org.apache.lucene.search.TopDocs;

public class LuceneSearcherPool {

    public static void main(String[] args) {

        GenericKeyedObjectPool.Config config = new GenericKeyedObjectPool.Config();

        GenericKeyedObjectPoolFactory<String, IndexReader> poolFactory = new GenericKeyedObjectPoolFactory<String, IndexReader>(new IndexReaderFactory("/var/data/ostree/index"), config);

        KeyedObjectPool<String, IndexReader> pool = poolFactory.createPool();

        try {

            String[] dates = {"2012-01-01", "2011-01-04", "2012-01-05"};

            for (String date : dates) {

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

                    long start = System.currentTimeMillis();

                    IndexReader reader = pool.borrowObject(date);

                    test(reader);

                    pool.returnObject(date, reader);

                    System.out.println(date + ":" + i + ";" + (System.currentTimeMillis() - start));

                }

            }

            pool.close();

        } catch (Exception ex) {

        }

    }

    public static void test(IndexReader reader) throws Exception {

        String input = "java";

        int num = 5;

        IndexSearcher searcher = new IndexSearcher(reader);

        TopDocs hits = searcher.search(query, num);

        for (ScoreDoc scoreDoc : hits.scoreDocs) {

            Document doc = searcher.doc(scoreDoc.doc);

        }

        searcher.close();

    }

}