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

2022-09-03 03:21:31

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

我的java代码在下面。在创建时,它会抛出一个IndexReaderIndexNotFoundException

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);
    } 

答案 1

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


答案 2

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

writer.close();

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


推荐