java org.apache.lucene.index.IndexNotFoundException: 在 org.apache.lucene.store.RAMDirectory 中没有找到segments* 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10460737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:08:18  来源:igfitidea点击:

org.apache.lucene.index.IndexNotFoundException: no segments* file found in org.apache.lucene.store.RAMDirectory

javanetbeanslucene

提问by Ahmed Khakwani

I am new to Java and Lucene. My code gets a line from a file and stores it in Lucene Index. But when I create an IndexReaderto search and read from the index it throws an exception.

我是 Java 和 Lucene 的新手。我的代码从文件中获取一行并将其存储在 Lucene 索引中。但是当我创建一个IndexReader来搜索和读取索引时,它会引发异常。

My java code is below. On creating the IndexReaderit throws an IndexNotFoundException

我的java代码如下。在创建IndexReader它时会抛出一个IndexNotFoundException

static String itemsfreq[];
static StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
static IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, analyzer);

public static void index_data(Directory indexed_document,int doc_num,IndexWriter w) throws IOException
    {
    for(int i = 0;i < itemsfreq.length;i++)
        {
        Document doc = new Document();
        doc.add(new Field(Integer.toString(doc_num)+","+itemsfreq[i],itemsfreq[i++], Field.Store.YES, Field.Index.ANALYZED));
        w.addDocument(doc);
        }
    }
//Gets string from a file and insert it in INDEX named indexed_document
public static void main(String[] args) throws IOException
    {
    BufferedReader reader = new BufferedReader(new FileReader("fullText100.txt"));
    String line;
    int i = 0;
    Directory indexed_document = new RAMDirectory();
    IndexWriter writer = new IndexWriter(indexed_document, config);
    while((line=reader.readLine()) != null)
        {
        if(i == 1)
            {
            break;
            }
        itemsfreq = line.split(" ");
        index_data(indexed_document,i,writer);
        i++;
        }

    IndexReader r = IndexReader.open(indexed_document);
    } 

回答by YJiao

before open the index by using a reader, call once writer.commit()

在使用阅读器打开索引之前,调用一次 writer.commit()

回答by csupnig

In order to write the changes to the Index you have to close the index writer and then open the IndexReader.

为了将更改写入索引,您必须关闭索引编写器,然后打开 IndexReader。

writer.close();

If you have to open the IndexReader before writing is completed, you have to tell the IndexReader to reopen the index in order to see the changes.

如果必须在写入完成之前打开 IndexReader,则必须告诉 IndexReader 重新打开索引以查看更改。

回答by software.wikipedia

You need to do is to explicitly call commit before opening your IndexSearcher.

您需要做的是在打开您的 IndexSearcher 之前显式调用 commit。

    directory = new RAMDirectory();
    iwriter = new IndexWriter(directory, config);
    iwriter.commit();

Now Open Searcher

现在打开搜索器

ireader = DirectoryReader.open(directory);
isearcher = new IndexSearcher(ireader);

Also remember you need to call commit after adding documents otherwise search may not find it. Searcher needs to reopened after commit (of course close old searcher).

还请记住,您需要在添加文档后调用 commit,否则搜索可能找不到它。提交后需要重新打开搜索器(当然关闭旧搜索器)。

iwriter.commit();

回答by DCShannon

I got this error (in Lucene.Net, C#) because I had created an index by creating the appropriate directory on my filesystem and FSDirectoryin memory, but hadn't actually added any documents yet.

我收到此错误(在 Lucene.Net、C# 中)是因为我通过在文件系统和FSDirectory内存中创建适当的目录来创建索引,但实际上还没有添加任何文档。

Specifically, the code to add new documents was checking to make sure that it wasn't adding a duplicate with a reader, but the exception was thrown when trying to add the first document, because there were no segments yet.

具体来说,添加新文档的代码正在检查以确保它没有使用阅读器添加重复项,但是在尝试添加第一个文档时抛出异常,因为还没有段。

I dealt with this like so:

我是这样处理的:

// Make a reader to check the index for duplicates
// The reader will only be aware of items that were in the index before it was created
IndexReader reader = null;
try {
    reader = IndexReader.Open( index, true );
} catch( IOException ) {
    // There is no segments file because the index is empty
    reader = null;
}

... // inside a method called by that scope, with reader passed in

// See if it exists already
// If the reader is null, then the index is empty
if( reader != null ) {
    var existsTerm = new Term( SingleFieldIndexManager.FieldName, formattedTerm );
    var matches = reader.TermDocs( existsTerm );
    if( matches.Next() ) {
        // It's already in there, no need to add again
        return;
    }
}

... // back to the first method after a return

// dispose of the reader if we managed to create one
if( reader != null ) {
    reader.Dispose();
}