gpt4 book ai didi

java - @Singleton @ApplicationScoped bean 中的 Lucene IndexWriters 关闭 IndexWriter

转载 作者:太空宇宙 更新时间:2023-11-04 11:29:39 37 4
gpt4 key购买 nike

当文档根据 Java Web 应用程序中的内容上传到不同的索引时,我需要对文档进行索引,其中多个用户可以同时上传多个文档

我使用 Lucene 6.2.1 进行索引

为此我创建了一个无状态 EJB。它在上传文档时对文档进行索引,称为 IndexingSessionBean

但是,由于我无法在一个索引上打开多个 IndexWriter,因此我创建了一个名为 CatagoryIndexWriters 的 @Singleton 和 @ApplicationScoped bean,它应该具有每个文档类别的 Index writer 的映射,并将其传递给 IndexingSessionBean。

我的代码如下所示

IndexingSessionBean.java

@Stateless
public class IndexingSessionBean {
@EJB
CatagoryIndexWriters catagoryIndexWriters;

public void indexFile(String documentId, String catId, byte[] fileBytes, boolean isUpdate) {

String content = // get contents of the fileBytes in String

try {
IndexWriter writer = catagoryIndexWriters.getTargetIndexWriter(catId)
Document doc = new Document();
Field documentIdField = new StringField("documentId", documentId, Field.Store.YES);
doc.add(documentIdField);
doc.add(new TextField("contents", content, Field.Store.YES));
if (!isUpdate) {
LOG.log(Level.INFO, "Indexing file with documentId {0}", documentId);
writer.addDocument(doc);
} else {
LOG.log(Level.INFO, "Updating Index for file with documentId {0}", documentId);
writer.updateDocument(new Term("documentId", documentId), doc);
}
}
catch (IOException ex) {
LOG.log(Level.SEVERE, "Unable to index document!", ex);
}

}
}

类别索引编写器

@Singleton
@ApplicationScoped
@ConcurrencyManagement(BEAN)
public class CatagoryIndexWriters {

@EJB
SystemConfigBean systemConfigBean;


Map<String, IndexWriter> indexWritersMap =new HashMap<String, IndexWriter>();
private double RAMBufferSize = 256.00;

public IndexWriter getCatagoryIndexWriter(String catId){
IndexWriter writer;
writer = indexWritersMap.get(catId);
if (writer != null){
return writer;
}else{
addCatagoryIndexWriterToMap(catId);
return indexWritersMap.get(catId);
}

}

private void createCatagoryIndexPath(String catId){
String indexPath = systemConfigBean.getSearchindexPath();
String catIndexPathString = indexPath+systemConfigBean.SEPARATORCHAR+catId;
Path catIndexPath = new File(catIndexPathString).toPath();

//Check the Catagory Index Folder if there is no index folder create it.

}

private void addCatagoryIndexWriterToMap(String catId){
createCatagoryIndexPath(catId);
String indexPath = systemConfigBean.getSearchindexPath();
String catIndexPathString = indexPath+systemConfigBean.SEPARATORCHAR+catId;
Path catIndexPath = new File(catIndexPathString).toPath();

try {
Directory dir = FSDirectory.open(catIndexPath);
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
iwc.setRAMBufferSizeMB(this.RAMBufferSize);
try (IndexWriter writer = new IndexWriter(dir, iwc)) {
indexWritersMap.put(catId, writer);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

但是在添加文档时我遇到以下异常..

Mai 12, 2017 12:54:59 PM org.apache.openejb.core.transaction.EjbTransactionUtil handleSystemException
SCHWERWIEGEND: EjbTransactionUtil.handleSystemException: this IndexWriter is closed
org.apache.lucene.store.AlreadyClosedException: this IndexWriter is closed
at org.apache.lucene.index.IndexWriter.ensureOpen(IndexWriter.java:740)
at org.apache.lucene.index.IndexWriter.ensureOpen(IndexWriter.java:754)
at org.apache.lucene.index.IndexWriter.updateDocument(IndexWriter.java:1558)
at org.apache.lucene.index.IndexWriter.addDocument(IndexWriter.java:1307)
at de.zaffar.docloaddoc.beans.IndexingSessionBean.indexFile(IndexingSessionBean.java:257)

我不知道IndexWriter bieng中的close方法是从哪里调用的

最佳答案

您的问题似乎是行,try (IndexWriter writer = new IndexWriter(dir, iwc)),因此该资源将在 try 语句后自动关闭,即一旦您将其放入 map 中。

try-with-resource有一个非常具体的用例,即在 try block 中使用该资源,否则它将被关闭。

IndexWriter确实实现了AutoCloseable,因此它会关闭。

将其从 try-with-resource 中删除并使其成为正常语句,然后重试。

关于java - @Singleton @ApplicationScoped bean 中的 Lucene IndexWriters 关闭 IndexWriter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43936860/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com