Changeset 12999
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl/src/java/org/nzdl/gsdl/LuceneWrap/GS2LuceneQuery.java
r12993 r12999 40 40 static private String TEXTFIELD = "TX"; 41 41 42 // Fairly self-explanatory I should hope 43 static private boolean query_result_caching_enabled = false; 44 42 45 // Use the standard set of English stop words by default 43 46 static private String[] stop_words = StandardAnalyzer.STOP_WORDS; … … 45 48 // Command-line options 46 49 static private String fuzziness = null; 50 static private String filter_string = null; 47 51 static private Filter filter = null; 52 static private String sort_string = null; 48 53 static private Sort sorter = new Sort(); 49 54 static private String default_conjuction_operator = "OR"; … … 60 65 61 66 try { 62 Searcher searcher = new IndexSearcher(args[0]); 67 String index_directory = args[0]; 68 Searcher searcher = new IndexSearcher(index_directory); 63 69 IndexReader reader = ((IndexSearcher) searcher).getIndexReader(); 70 71 // Prepare the index cache directory, if query result caching is enabled 72 if (query_result_caching_enabled) { 73 // Make the index cache directory if it doesn't already exist 74 File index_cache_directory = new File(index_directory, "cache"); 75 if (!index_cache_directory.exists()) { 76 index_cache_directory.mkdir(); 77 } 78 79 // Disable caching if the index cache directory isn't available 80 if (!index_cache_directory.exists() || !index_cache_directory.isDirectory()) { 81 query_result_caching_enabled = false; 82 } 83 } 64 84 65 85 // Create one query parser with the standard set of stop words, and one with none … … 73 93 if (args[i].equals("-sort")) { 74 94 i++; 75 sorter = new Sort(args[i]); 95 sort_string = args[i]; 96 sorter = new Sort(sort_string); 76 97 } 77 98 else if (args[i].equals("-filter")) { 78 99 i++; 79 filter = parseFilterString(args[i]); 100 filter_string = args[i]; 101 filter = parseFilterString(filter_string); 80 102 } 81 103 else if (args[i].equals("-dco")) { … … 112 134 // The query string has been specified as a command-line argument 113 135 if (query_string != null) { 114 runQuery( searcher, reader, query_parser, query_parser_no_stop_words, query_string);136 runQuery(index_directory, searcher, reader, query_parser, query_parser_no_stop_words, query_string); 115 137 } 116 138 … … 125 147 } 126 148 127 runQuery( searcher, reader, query_parser, query_parser_no_stop_words, query_string);149 runQuery(index_directory, searcher, reader, query_parser, query_parser_no_stop_words, query_string); 128 150 } 129 151 } … … 137 159 138 160 139 private static void runQuery(S earcher searcher, IndexReader reader, QueryParser query_parser, QueryParser query_parser_no_stop_words, String query_string)161 private static void runQuery(String index_directory, Searcher searcher, IndexReader reader, QueryParser query_parser, QueryParser query_parser_no_stop_words, String query_string) 140 162 throws IOException 141 163 { 142 164 StringBuffer query_results_xml = new StringBuffer(); 143 165 144 query_results_xml.append("<ResultSet>\n"); 166 // Check if this query result has been cached from a previous search (if it's enabled) 167 File query_result_cache_file = null; 168 if (query_result_caching_enabled) { 169 // Generate the cache file name from the query options 170 String query_result_cache_file_name = query_string + "-"; 171 query_result_cache_file_name += ((fuzziness != null) ? fuzziness : "") + "-"; 172 query_result_cache_file_name += ((filter_string != null) ? filter_string : "") + "-"; 173 query_result_cache_file_name += ((sort_string != null) ? sort_string : "") + "-"; 174 query_result_cache_file_name += default_conjuction_operator + "-"; 175 query_result_cache_file_name += start_results + "-" + end_results; 176 query_result_cache_file_name = fileSafe(query_result_cache_file_name); 177 178 // If the query result cache file exists, just return its contents and we're done 179 File index_cache_directory = new File(index_directory, "cache"); 180 query_result_cache_file = new File(index_cache_directory, query_result_cache_file_name); 181 if (query_result_cache_file.exists() && query_result_cache_file.isFile()) { 182 FileInputStream fis = new FileInputStream(query_result_cache_file); 183 InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); 184 BufferedReader buffered_reader = new BufferedReader(isr); 185 String line = ""; 186 while ((line = buffered_reader.readLine()) != null) { 187 query_results_xml.append(line + "\n"); 188 } 189 String query_results_xml_string = query_results_xml.toString(); 190 query_results_xml_string = query_results_xml_string.replaceFirst("cached=\"false\"", "cached=\"true\""); 191 System.out.print(query_results_xml_string); 192 return; 193 } 194 } 195 196 query_results_xml.append("<ResultSet cached=\"false\">\n"); 145 197 query_results_xml.append(" <QueryString>" + xmlSafe(query_string) + "</QueryString>\n"); 146 198 if (filter != null) { … … 227 279 228 280 System.out.print(query_results_xml); 281 282 // Cache this query result, if desired 283 if (query_result_caching_enabled) { 284 FileWriter query_result_cache_file_writer = new FileWriter(query_result_cache_file); 285 query_result_cache_file_writer.write(query_results_xml.toString()); 286 query_result_cache_file_writer.close(); 287 } 288 } 289 290 291 private static String fileSafe(String text) 292 { 293 StringBuffer file_safe_text = new StringBuffer(); 294 for (int i = 0; i < text.length(); i++) { 295 char character = text.charAt(i); 296 if ((character >= 'A' && character <= 'Z') || (character >= 'a' && character <= 'z') || (character >= '0' && character <= '9') || character == '-') { 297 file_safe_text.append(character); 298 } 299 else { 300 file_safe_text.append('%'); 301 file_safe_text.append((int) character); 302 } 303 } 304 return file_safe_text.toString(); 229 305 } 230 306 -
trunk/indexers/lucene-gs/src/org/greenstone/LuceneWrapper/GS2LuceneQuery.java
r12993 r12999 40 40 static private String TEXTFIELD = "TX"; 41 41 42 // Fairly self-explanatory I should hope 43 static private boolean query_result_caching_enabled = false; 44 42 45 // Use the standard set of English stop words by default 43 46 static private String[] stop_words = StandardAnalyzer.STOP_WORDS; … … 45 48 // Command-line options 46 49 static private String fuzziness = null; 50 static private String filter_string = null; 47 51 static private Filter filter = null; 52 static private String sort_string = null; 48 53 static private Sort sorter = new Sort(); 49 54 static private String default_conjuction_operator = "OR"; … … 60 65 61 66 try { 62 Searcher searcher = new IndexSearcher(args[0]); 67 String index_directory = args[0]; 68 Searcher searcher = new IndexSearcher(index_directory); 63 69 IndexReader reader = ((IndexSearcher) searcher).getIndexReader(); 70 71 // Prepare the index cache directory, if query result caching is enabled 72 if (query_result_caching_enabled) { 73 // Make the index cache directory if it doesn't already exist 74 File index_cache_directory = new File(index_directory, "cache"); 75 if (!index_cache_directory.exists()) { 76 index_cache_directory.mkdir(); 77 } 78 79 // Disable caching if the index cache directory isn't available 80 if (!index_cache_directory.exists() || !index_cache_directory.isDirectory()) { 81 query_result_caching_enabled = false; 82 } 83 } 64 84 65 85 // Create one query parser with the standard set of stop words, and one with none … … 73 93 if (args[i].equals("-sort")) { 74 94 i++; 75 sorter = new Sort(args[i]); 95 sort_string = args[i]; 96 sorter = new Sort(sort_string); 76 97 } 77 98 else if (args[i].equals("-filter")) { 78 99 i++; 79 filter = parseFilterString(args[i]); 100 filter_string = args[i]; 101 filter = parseFilterString(filter_string); 80 102 } 81 103 else if (args[i].equals("-dco")) { … … 112 134 // The query string has been specified as a command-line argument 113 135 if (query_string != null) { 114 runQuery( searcher, reader, query_parser, query_parser_no_stop_words, query_string);136 runQuery(index_directory, searcher, reader, query_parser, query_parser_no_stop_words, query_string); 115 137 } 116 138 … … 125 147 } 126 148 127 runQuery( searcher, reader, query_parser, query_parser_no_stop_words, query_string);149 runQuery(index_directory, searcher, reader, query_parser, query_parser_no_stop_words, query_string); 128 150 } 129 151 } … … 137 159 138 160 139 private static void runQuery(S earcher searcher, IndexReader reader, QueryParser query_parser, QueryParser query_parser_no_stop_words, String query_string)161 private static void runQuery(String index_directory, Searcher searcher, IndexReader reader, QueryParser query_parser, QueryParser query_parser_no_stop_words, String query_string) 140 162 throws IOException 141 163 { 142 164 StringBuffer query_results_xml = new StringBuffer(); 143 165 144 query_results_xml.append("<ResultSet>\n"); 166 // Check if this query result has been cached from a previous search (if it's enabled) 167 File query_result_cache_file = null; 168 if (query_result_caching_enabled) { 169 // Generate the cache file name from the query options 170 String query_result_cache_file_name = query_string + "-"; 171 query_result_cache_file_name += ((fuzziness != null) ? fuzziness : "") + "-"; 172 query_result_cache_file_name += ((filter_string != null) ? filter_string : "") + "-"; 173 query_result_cache_file_name += ((sort_string != null) ? sort_string : "") + "-"; 174 query_result_cache_file_name += default_conjuction_operator + "-"; 175 query_result_cache_file_name += start_results + "-" + end_results; 176 query_result_cache_file_name = fileSafe(query_result_cache_file_name); 177 178 // If the query result cache file exists, just return its contents and we're done 179 File index_cache_directory = new File(index_directory, "cache"); 180 query_result_cache_file = new File(index_cache_directory, query_result_cache_file_name); 181 if (query_result_cache_file.exists() && query_result_cache_file.isFile()) { 182 FileInputStream fis = new FileInputStream(query_result_cache_file); 183 InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); 184 BufferedReader buffered_reader = new BufferedReader(isr); 185 String line = ""; 186 while ((line = buffered_reader.readLine()) != null) { 187 query_results_xml.append(line + "\n"); 188 } 189 String query_results_xml_string = query_results_xml.toString(); 190 query_results_xml_string = query_results_xml_string.replaceFirst("cached=\"false\"", "cached=\"true\""); 191 System.out.print(query_results_xml_string); 192 return; 193 } 194 } 195 196 query_results_xml.append("<ResultSet cached=\"false\">\n"); 145 197 query_results_xml.append(" <QueryString>" + xmlSafe(query_string) + "</QueryString>\n"); 146 198 if (filter != null) { … … 227 279 228 280 System.out.print(query_results_xml); 281 282 // Cache this query result, if desired 283 if (query_result_caching_enabled) { 284 FileWriter query_result_cache_file_writer = new FileWriter(query_result_cache_file); 285 query_result_cache_file_writer.write(query_results_xml.toString()); 286 query_result_cache_file_writer.close(); 287 } 288 } 289 290 291 private static String fileSafe(String text) 292 { 293 StringBuffer file_safe_text = new StringBuffer(); 294 for (int i = 0; i < text.length(); i++) { 295 char character = text.charAt(i); 296 if ((character >= 'A' && character <= 'Z') || (character >= 'a' && character <= 'z') || (character >= '0' && character <= '9') || character == '-') { 297 file_safe_text.append(character); 298 } 299 else { 300 file_safe_text.append('%'); 301 file_safe_text.append((int) character); 302 } 303 } 304 return file_safe_text.toString(); 229 305 } 230 306
Note:
See TracChangeset
for help on using the changeset viewer.