Apache lucene 4.3.1 - 索引阅读器无法访问最后一个索引文档

时间:2013-07-09 08:55:06

标签: lucene

在我的应用程序中,我有文档代表我的每个类别的数据,我的应用程序执行新的和修改的文档的自动索引。

如果我为一个类别中的所有文档执行了索引,它的工作正常并检索到正确的结果,但问题是,如果我修改或创建新文档,如果它与我的搜索查询匹配则不会检索它。

通常会保留返回除最后修改过的文档之外的所有文档。

有什么帮助吗?

我有这个IndexWriter配置:

private IndexWriter getIndexWriter() throws IOException {

     Directory directory = FSDirectory.open(new File(filepath));
     IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_43, IndexFactory.ANALYZER);
     config.setRAMBufferSizeMB(350);

     TieredMergePolicy tmp = new TieredMergePolicy();
     tmp.setUseCompoundFile(false);
     config.setMergePolicy(tmp);

     ConcurrentMergeScheduler scheduler = (ConcurrentMergeScheduler) config.getMergeScheduler();
     scheduler.setMaxThreadCount(2);
     scheduler.setMaxMergeCount(20);

     IndexWriter writer = new IndexWriter(directory, config);
     writer.forceMerge(1);

     return writer;

我的收藏家:

public void collect(int docNum) throws IOException {
    try {
        if ((getCount() == getMaxSearchLimit() + 1) && getMaxSearchResults() != null) {
            setCounterExceededLimit(true);
            return;
        }

        addDocKey();// method to add and render the matching docs by customize way
    } catch(IOException exp) {
        if (!getErrors().toArrayList(getApplication().getLocale()).contains(exp.getMessage())) {
            getErrors().addError(exp.getMessage());
        }
    } catch (BusinessException bEx) {
        if (!getErrors().containsError(bEx.getErrorNumber())) {
            getErrors().addError(bEx);
        }
    } catch (CounterExceededLimitException counterEx) {
        return;
    }
}

@Override
public boolean acceptsDocsOutOfOrder() {
    // TODO Auto-generated method stub
    return true;
}

@Override
public void setNextReader(AtomicReaderContext context) throws IOException {
    // TODO Auto-generated method stub
}

@Override
public void setScorer(Scorer scorer) throws IOException {
    // TODO Auto-generated method stub
}

我真的有这个busniess逻辑来保存我的文档,然后我问是否成功保存文档以将其添加到索引过程。

public boolean saveDocument(CategoryDocument doc) {
boolean saved = false;

// code to save my doc 

if(saved) {
    //add this document to the index process
    IndexManager.getInstance().addToIndex(this);
}

}

然后我的索引管理器创建一个新线程来处理索引此doc。 这是我的索引数据文档的过程:

private void processDocument(IndexDocument indexDoc, DocKey docKey, boolean addToIndex) throws SearchException, BusinessException {
    CategorySetting catSetting = docKey.getCategorySetting();
    Integer catID = catSetting.getID();
    IndexManager manager = IndexManager.getInstance();
    IndexWriter writer = null;

    try {

        //Delete the lock file in case previous index operation failed to delete it
        File lockFile = new File(filepath, IndexWriter.WRITE_LOCK_NAME);
        if (lockFile != null && lockFile.exists()) {
            lockFile.delete();
        }

        if(!manager.isGlobalIndexingProcess(catID)) {
            writer = getIndexWriter();
        } else {
            writer = manager.getGlobalIndexWriter(catID);
        }
        writer.forceMerge(1);

        removeDocument(docKey, writer);

        if (addToIndex) {
            writer.addDocument(indexDoc.getLuceneIndexDoc());
        }
    } catch(IOException exp) {
        throw new SearchException(exp.getMessage(), true);
    } finally {

        if(!manager.isGlobalIndexingProcess(catID)) {
            if (writer != null) {
                try {
                    writer.close(true); 
                } catch(IOException ex) {
                    throw new SearchException(ex);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

使用lucene搜索并搜索您在文档中编辑的单词或短语,并告诉我们您是否获得了正确的匹配。如果您没有获得任何点击,那么可能您没有编辑编辑或新添加的文档。