Re: indexReader close method
Hi Otis, Thanks for the reply. I'm using lucene 1.4.2. I believe that the IndexSearcher in this version will close the IndexReader if the IndexReader was supplied implicitly but I'm constructing the IndexReader first and passing it to the IndexSearcher constructor as I want to use the reader object later. Do you know why I can't close the IndexReader explicitly under some circumstances and why, when I do manage to close it I can still call methods on the reader? Thanks, Helen. On Monday, December 6, 2004, at 05:04 pm, Otis Gospodnetic wrote: Helen, I don't know what version of Lucene you are using. The version in CVS has IndexSearcher that can close the underlying IndexReader for you. Otis --- Helen Warren [EMAIL PROTECTED] wrote: Hi All, I'm having some trouble closing an index reader. I'm getting a IOException (bad file descriptor). Can anybody tell me what I'm doing wrong and how I should be closing this object? An outline of an extract of my code: // Make a new searcher (indexName passed in as string). IndexReader myReader = IndexReader.open(indexName); searcher = new IndexSearcher(myReader); //Create a snowball analyzer object. //Create a MultifieldQueryParser object with the snowball analyzer and build a luceneQuery from a user entered search string. //Create a new BooleanQuery (overallQuery), add the luceneQuery to it and add several other BooleanQueries specific to other fields in the documents. // Do the search hits = searcher.search(overallQuery); //close the IndexSearcher object searcher.close(); //close the IndexReader object myReader.close(); //return results return hits; The myReader.close() line causes the IOException to be thrown. To try to debug, I wrote a simplified code to open and close a reader and searcher, and in the process discovered something else that I have found confusing: Here's an extract of the simplified code: String indexName = foo; // Make a new searcher IndexReader myReader = IndexReader.open(indexName); IndexSearcher searcher = new IndexSearcher(myReader); //try to do something with the reader Collection theseFields = myReader.getFieldNames(); //close the IndexSearcher object searcher.close(); //try to close the reader now myReader.close(); //now try to do something with the reader again; try { int maximum = myReader.maxDoc(); System.err.println(Next document would be: +maximum); } catch (Exception e) { System.err.println(Exception: +e.getMessage()); } In this case, the reader appears to close without error but even after I've called myReader.close() I can execute the maxDoc() method on that object and return results. Anybody shed any light? Thanks, Helen. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: indexReader close method
: Do you know why I can't close the IndexReader explicitly under some : circumstances and why, when I do manage to close it I can still call : methods on the reader? 1) I tried to create a test case that demonstrated your bug based on the code outline you provided, and i couldn't (see below). that implies to me that somethine else is going on. If you can create a completely self contained program that demonstrates your bug and mail it to the list that would help us help you. 2) the documentation for IndexReader.close() says... Closes files associated with this index. Also saves any new deletions to disk. No other methods should be called after this has been called. ...note the word should. it doesn't say what the other methods will do if you try to call them, just that you shouldn't try. In some cases they may generate exceptions, in other cases they may just be able to return you data based on state internal to the object which is unaffected by the fact that the files have all been closed. -Hoss public static void main(String argv[]) throws IOException { /* create a directory */ String d = System.getProperty(java.io.tmpdir, tmp) + System.getProperty(file.separator) + index-dir- + (new Random()).nextInt(1000); Directory trash = FSDirectory.getDirectory(d, true); /* build index */ Document doc; IndexWriter w = new IndexWriter(d, new SimpleAnalyzer(), true); doc = new Document(); doc.add(Field.Text(words, apple emu)); w.addDocument(doc); w.optimize(); w.close(); /* search index */ IndexReader r = IndexReader.open(d); IndexSearcher s = new IndexSearcher(r); Hits h = s.search(new TermQuery(new Term(words, apple))); s.close(); r.close(); System.out.println(Reader? - + r.maxDoc()); } - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: indexReader close method
Helen Warren writes: //close the IndexReader object myReader.close(); //return results return hits; The myReader.close() line causes the IOException to be thrown. To try Are you sure it's the myReader.close() that fails? I'd suspect that to fail as soon as you want to do anything meaningful with the hits objects you return. You need an open searcher/reader for that and in general it should be the one, you used during search. This is assuming hits is an instance of class org.apache.lucene.search.Hits. The method Document doc(int n) relys on the searcher used for search not being closed. So I'd suspect the IOException to be thrown later. Of course removing the myReader.close(); will prevent the exception. You cannot close the reader as long as you want to access search results. In this case, the reader appears to close without error but even after I've called myReader.close() I can execute the maxDoc() method on that object and return results. Anybody shed any light? yes. the source ;-) maxDoc does not access the index files but returns an integer stored in the class itself. Morus - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]