2012-05-05 28 views
12

Tôi mới dùng Java và Lucene. Mã của tôi nhận được một dòng từ một tệp và lưu trữ nó trong Lucene Index. Nhưng khi tôi tạo một IndexReader để tìm kiếm và đọc từ chỉ mục, nó sẽ đưa ra một ngoại lệ.org.apache.lucene.index.IndexNotFoundException: không tìm thấy phân đoạn * nào trong org.apache.lucene.store.RAMDirectory

Mã java của tôi bên dưới. Vào việc tạo ra các IndexReader nó ném một 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); 
    } 

Trả lời

14

Để viết những thay đổi chỉ số bạn phải đóng nhà văn chỉ mục và sau đó mở IndexReader.

writer.close(); 

Nếu bạn phải mở IndexReader trước khi viết xong, bạn phải yêu cầu IndexReader mở lại chỉ mục để xem thay đổi.

+0

tôi áp dụng giải pháp này và nó làm việc. –

16

trước khi mở chỉ số bằng cách sử dụng một trình đọc, hãy gọi một lần writer.commit()

+1

Tôi đã gặp phải sự cố tương tự và sử dụng 'writer.commit()' đã sửa nó. Nếu tôi đã sử dụng 'writer.close()', tôi sẽ phải mở lại nhà văn một lần nữa. Đề xuất của bạn tốt hơn đề xuất trước đó. – tuxdna

+2

Thật vậy, đây là giải pháp đúng vì nó được khuyến cáo bởi tài liệu Lucene để tái sử dụng các trường hợp đơn lẻ của IndexWriter và IndexReader; Bằng cách này tôi không bị buộc phải đóng cửa nhà văn và tạo lại nó khi cần thiết. – Serg

3

Bạn cần phải làm là để gọi một cách rõ ràng cam kết trước khi mở IndexSearcher của bạn.

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

Bây giờ mở Searcher

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

Cũng nên nhớ bạn cần phải gọi cam kết sau khi thêm tài liệu khác tìm kiếm có thể không tìm thấy nó. Người tìm kiếm cần mở lại sau khi cam kết (tất nhiên là tìm kiếm cũ).

iwriter.commit(); 
0

tôi đã nhận lỗi này (trong Lucene.Net, C#) bởi vì tôi đã tạo ra một chỉ số bằng cách tạo ra các thư mục thích hợp trên hệ thống tập tin của tôi và FSDirectory trong bộ nhớ, nhưng vẫn chưa thực sự được thêm bất kỳ tài liệu nào.

Cụ thể, mã để thêm tài liệu mới đang kiểm tra để đảm bảo rằng nó không thêm bản sao với người đọc, nhưng ngoại lệ đã được ném khi cố gắng thêm tài liệu đầu tiên vì chưa có phân đoạn nào.

tôi xử lý này như sau:

// 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(); 
} 
+0

Xin chào, tôi đang gặp vấn đề tương tự như bạn. Nhưng tôi không hiểu cách bạn giải quyết nó. Nếu không có tệp thì nó sẽ đi vào khối catch và đúng không? – Valentin

+0

Đã tìm thấy sự cố. Tôi gặp lỗi này khi tạo chỉ mụcWinditer với tham số create = false – Valentin

Các vấn đề liên quan