- Timestamp:
- 2018-11-20T21:35:42+13:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/common-src/indexers/lucene-gs/src/org/greenstone/LuceneWrapper4/GS2LuceneQuery.java
r32616 r32620 44 44 import org.apache.lucene.search.ConstantScoreQuery; 45 45 import org.apache.lucene.search.Filter; 46 import org.apache.lucene.search.IndexSearcher; 46 import org.apache.lucene.search.IndexSearcher; // Searcher is deprecated 47 47 import org.apache.lucene.search.MultiTermQuery; 48 48 import org.apache.lucene.search.MultiTermQuery.ConstantScoreAutoRewrite; 49 49 import org.apache.lucene.search.Query; 50 50 import org.apache.lucene.search.TermRangeFilter; 51 import org.apache.lucene.search.IndexSearcher; // Searcher is deprecated52 51 import org.apache.lucene.search.ScoreDoc; 53 52 import org.apache.lucene.search.Sort; … … 80 79 protected QueryParser query_parser_no_stop_words = null; 81 80 protected IndexSearcher searcher = null; 82 protected IndexReader reader = null; 81 protected IndexReader reader = null; // reference to a Reader resource. GS2LuceneQuery doesn't maintain it, GS2LuceneSearch maintains it! 82 // GS2LuceneSearch locally instantiates one GS2LuceneQuery object per query then allows each Query instance use a relevant Reader. 83 // But GS2LuceneSearch opens the IndexReaders and, more importantly, closes them all when a collection is deactivated. 83 84 84 85 public GS2LuceneQuery() { … … 89 90 query_parser = new QueryParser(GSLuceneConstants.MATCH_VERSION, TEXTFIELD, new GS2Analyzer()); // uses built-in stop_words_set 90 91 query_parser_no_stop_words = new QueryParser(GSLuceneConstants.MATCH_VERSION, TEXTFIELD, new GS2Analyzer(new String[] { })); 91 } 92 93 94 public boolean initialise() { 95 96 if (!super.initialise()) { 97 return false; 98 } 92 } 93 94 public boolean initialise(IndexReader reader) { 95 96 if (!super.initialise()) { 97 return false; 98 } 99 99 100 100 … … 104 104 return false; 105 105 } 106 107 try {108 106 109 if(reader != null) { 110 reader.close(); 111 searcher = null; 112 } 113 114 Directory full_indexdir_dir = FSDirectory.open(new File(full_indexdir)); 115 116 reader = DirectoryReader.open(full_indexdir_dir); // Returns a IndexReader reading the index in the given Directory. now readOnly=true by default, and therefore also for searcher 117 searcher = new IndexSearcher(reader); // during searcher.search() will get it to compute ranks when sorting by fields 118 119 this.sorter = new Sort(new SortField(this.sort_field, this.sort_type, this.reverse_sort)); 120 } 121 catch (IOException exception) { 122 exception.printStackTrace(); 123 return false; 124 } 125 return true; 126 107 if(reader == null) { 108 return false; 109 } 110 else { 111 this.reader = reader; 112 this.searcher = new IndexSearcher(reader); // during searcher.search() will get it to compute ranks when sorting by fields 113 this.sorter = new Sort(new SortField(this.sort_field, this.sort_type, this.reverse_sort)); 114 return true; 115 } 127 116 } 128 117 … … 170 159 LuceneQueryResult lucene_query_result=new LuceneQueryResult(); 171 160 lucene_query_result.clear(); 172 161 162 if(this.reader == null) { 163 System.err.println("#### Reader is null!"); 164 } 165 173 166 try { 174 167 Query query_including_stop_words = query_parser_no_stop_words.parse(query_string); … … 348 341 } 349 342 350 343 // This version of the cleanUp() method is just to clean up anything associated only with this instance of GS2LuceneQuery. 344 // So it won't clean up the singleton IndexReader instances maintained by the encapsulating GS2LuceneSearch class. 351 345 public void cleanUp() { 352 super.cleanUp(); 353 try { 354 if(reader != null) { 355 reader.close(); 356 // Closes files associated with this index. Also saves any new deletions to disk. 357 // No other methods should be called after this has been called. 358 } 346 super.cleanUp(); 359 347 360 } catch (IOException exception) { 361 exception.printStackTrace(); 362 } 363 } 364 348 searcher = null; 349 350 // Don't close the indexReader reference here. 351 // This has moved into the GS2LuceneSearch.cleanUp() method, as it maintains singleton IndexReaders 352 // for each index level (sidx, didix) with lifespans matching their collection's lifespan 353 // A collection's GS2LuceneSearch object lives for the duration of the Collection. 354 // A GS2LuceneQuery object is ephemeral: only lives for the duration of a query, allowing multiple 355 // users to queries concurrently, sharing a single IndexReader object for each indexing level 356 // as IndexReader support concurrency. 357 } 365 358 366 359 protected Query parseQuery(IndexReader reader, QueryParser query_parser, String query_string, String fuzziness)
Note:
See TracChangeset
for help on using the changeset viewer.