Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 IndexReader to search and read from the index it throws an exception.

My java code is below. On creating the IndexReader it throws an 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);
    } 
like image 582
Ahmed Khakwani Avatar asked May 05 '12 09:05

Ahmed Khakwani


2 Answers

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

like image 71
YJiao Avatar answered Sep 18 '22 01:09

YJiao


In order to write the changes to the Index you have to close the index writer and then open the 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.

like image 34
csupnig Avatar answered Sep 21 '22 01:09

csupnig