16

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

我的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);
    } 
4

4 回答 4

29

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

于 2013-04-25T11:47:49.140 回答
19

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

writer.close();

如果您必须在写入完成之前打开 IndexReader,您必须告诉 IndexReader 重新打开索引才能看到更改。

于 2012-05-05T10:16:25.430 回答
9

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

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

现在打开搜索器

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

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

iwriter.commit();
于 2014-05-09T22:01:23.533 回答
0

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

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

我是这样处理的:

// 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();
}
于 2015-02-07T00:18:18.713 回答