标签:des Lucene class code java tar
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 |
package
org.ostree.module.lucene;import
org.apache.commons.pool.KeyedPoolableObjectFactory;import
org.apache.lucene.index.IndexReader;import
org.apache.lucene.store.FSDirectory;import
org.slf4j.Logger;import
org.slf4j.LoggerFactory;import
java.io.File;import
java.util.NoSuchElementException;public
class IndexReaderFactory implements
KeyedPoolableObjectFactory<String, IndexReader> { private
String baseIndexDir="/var/data/ostree/index"; private
static final Logger logger = LoggerFactory .getLogger(IndexReaderFactory.class); public
IndexReaderFactory(String baseIndexDir) { this.baseIndexDir = baseIndexDir; } @Override public
IndexReader makeObject(String key) throws
Exception { logger.info("open index: "
+ key); File file=new
File(baseIndexDir,key); if(!file.exists()){ throw
new NoSuchElementException(key +" index doesn‘t exist!"); } FSDirectory dir =FSDirectory.open(file); return
IndexReader.open(dir, true); } @Override public
void destroyObject(String key, IndexReader reader) throws
Exception { logger.info("destroy index: "
+ key); reader.close(); } @Override public
boolean validateObject(String key, IndexReader reader) { logger.info("validate index: "
+ key); if(reader!=null){ return
true; } return
false; } @Override public
void activateObject(String key, IndexReader reader) throws
Exception { logger.debug("activate index: "
+ key); } @Override public
void passivateObject(String key, IndexReader reader) throws
Exception { logger.debug("passivate index: "
+ key); }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 |
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); //build your query here //Query query = TopDocs hits = searcher.search(query, num); for
(ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = searcher.doc(scoreDoc.doc); // handle the Document } searcher.close(); }} |
Cache Lucene IndexReader with Apache Commons Pool,布布扣,bubuko.com
Cache Lucene IndexReader with Apache Commons Pool
标签:des Lucene class code java tar
原文地址:http://www.cnblogs.com/xsi640/p/3772301.html