lucene的锁机制,首先讲对于indexWriter的只能创建一次,一但建立了indexWriter对象系统会分配锁(write.lock)给他.
由此我的思路是新建一个luceneUtil 使用同步方法 来实现。
org.spache.lucene.store.LockoBtainFailedException:Lock obtain time .out 异常就是应为锁机制而应发的。
或者是由于你indexWriter.close()忘记写(finally{indexWriter.close()})
代码:
public synchronized static void getIndexWriter(String indexPosition,Document doc) throws IOException{
IndexWriter indexWriter = null;
try{
Directory director = FSDirectory.open(new File(indexPosition));
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_47, new IKAnalyzer(false));
indexWriter = new IndexWriter(director, indexWriterConfig);
indexWriter.addDocument(doc);
indexWriter.forceMerge(1);// 优化
} catch (IOException e) {
e.printStackTrace();
}finally{
indexWriter.close();
}
}
既然说了indexWriter也说一下IndexReader,为了提高和充分利用资源,可以共享一个indexRreader 。代码如下。
注意:如果修改和新建时请重新创建IndexReader.
代码:
private static Hashtable<String, IndexReader> shapeMap = new Hashtable<String, IndexReader>();
public static IndexSearcher loadCache( String id,String path) {
IndexSearcher searcher = null;
try {
MultiReader mreader=null;
if(shapeMap.get(id) == null){
FSDirectory open = FSDirectory.open(new File(path+File.separator+id ));
shapeMap.put(id,DirectoryReader.open(open));
}
if(shapeMap.get("00") == null){
FSDirectory open00 = FSDirectory.open(new File(path+File.separator+"00" ));
shapeMap.put("00",DirectoryReader.open(open00));
}
if("00".equals(id)){
searcher=new IndexSearcher(shapeMap.get(id));
}else {
mreader=new MultiReader(shapeMap.get(id),shapeMap.get("00"));//合并索引文件
searcher=new IndexSearcher(mreader);
}
return searcher;
} catch (Exception e) {
e.printStackTrace();
}
return searcher;
}
以上从属个人思路,如有不同观点和想法请评论一下,指正。希望大家支持一下。
如果想了解lucene可以看 Lucene实战(第2版)_中文版.pdf 地址:
http://pan.baidu.com/s/1i587t8x
由此我的思路是新建一个luceneUtil 使用同步方法 来实现。
org.spache.lucene.store.LockoBtainFailedException:Lock obtain time .out 异常就是应为锁机制而应发的。
或者是由于你indexWriter.close()忘记写(finally{indexWriter.close()})
代码:
public synchronized static void getIndexWriter(String indexPosition,Document doc) throws IOException{
IndexWriter indexWriter = null;
try{
Directory director = FSDirectory.open(new File(indexPosition));
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_47, new IKAnalyzer(false));
indexWriter = new IndexWriter(director, indexWriterConfig);
indexWriter.addDocument(doc);
indexWriter.forceMerge(1);// 优化
} catch (IOException e) {
e.printStackTrace();
}finally{
indexWriter.close();
}
}
既然说了indexWriter也说一下IndexReader,为了提高和充分利用资源,可以共享一个indexRreader 。代码如下。
注意:如果修改和新建时请重新创建IndexReader.
代码:
private static Hashtable<String, IndexReader> shapeMap = new Hashtable<String, IndexReader>();
public static IndexSearcher loadCache( String id,String path) {
IndexSearcher searcher = null;
try {
MultiReader mreader=null;
if(shapeMap.get(id) == null){
FSDirectory open = FSDirectory.open(new File(path+File.separator+id ));
shapeMap.put(id,DirectoryReader.open(open));
}
if(shapeMap.get("00") == null){
FSDirectory open00 = FSDirectory.open(new File(path+File.separator+"00" ));
shapeMap.put("00",DirectoryReader.open(open00));
}
if("00".equals(id)){
searcher=new IndexSearcher(shapeMap.get(id));
}else {
mreader=new MultiReader(shapeMap.get(id),shapeMap.get("00"));//合并索引文件
searcher=new IndexSearcher(mreader);
}
return searcher;
} catch (Exception e) {
e.printStackTrace();
}
return searcher;
}
以上从属个人思路,如有不同观点和想法请评论一下,指正。希望大家支持一下。
如果想了解lucene可以看 Lucene实战(第2版)_中文版.pdf 地址:
http://pan.baidu.com/s/1i587t8x