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

推荐订阅源

A
About on SuperTechFans
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
宝玉的分享
宝玉的分享
美团技术团队
量子位
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
IT之家
IT之家
爱范儿
爱范儿
J
Java Code Geeks
博客园 - Franky
Last Week in AI
Last Week in AI
B
Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
GbyAI
GbyAI
Recent Announcements
Recent Announcements
小众软件
小众软件
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
MyScale Blog
MyScale Blog

博客园 - 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();

    }

}