List of usage examples for org.apache.lucene.index DirectoryReader open
public static DirectoryReader open(final IndexCommit commit) throws IOException
From source file:com.gprasad.searchwithlucene.Searcher.java
private static void createIndexSearcher(String indexPath) throws IOException { Directory directory = FSDirectory.open(Paths.get(indexPath)); DirectoryReader directoryReader = DirectoryReader.open(directory); indexSearcher = new IndexSearcher(directoryReader); }
From source file:com.helger.pd.indexer.lucene.PDLucene.java
License:Apache License
@Nullable private DirectoryReader _getReader() throws IOException { _checkClosing();/* w ww . j ava 2 s. c o m*/ try { // Commit the writer changes only if a reader is requested if (m_aWriterChanges.intValue() > 0) { s_aLogger.info("Lazily committing " + m_aWriterChanges.intValue() + " changes to the Lucene index"); _getWriter().commit(); m_aWriterChanges.set(0); } // Is a new reader required because the index changed? final DirectoryReader aNewReader = m_aIndexReader != null ? DirectoryReader.openIfChanged(m_aIndexReader) : DirectoryReader.open(m_aDir); if (aNewReader != null) { // Something changed in the index m_aIndexReader = aNewReader; m_aSearcher = null; if (s_aLogger.isDebugEnabled()) s_aLogger.debug("Contents of index changed. Creating new index reader"); } return m_aIndexReader; } catch (final IndexNotFoundException ex) { // No such index return null; } }
From source file:com.hrstc.lucene.queryexpansion.PatentClassCodeBasedQueryExpansion.java
License:Apache License
public PatentClassCodeBasedQueryExpansion(String classCodesIndexDir, int Nbr_Terms, float alpha, float beta, float decay) throws IOException, Exception { Directory classCodesDir = FSDirectory.open(new File(classCodesIndexDir)); classCodesSearcher = new IndexSearcher(DirectoryReader.open(classCodesDir)); parameters = new HashMap<>(); parameters.put(RocchioQueryExpansion.ROCCHIO_ALPHA_FLD, alpha); parameters.put(RocchioQueryExpansion.ROCCHIO_BETA_FLD, beta); parameters.put(RocchioQueryExpansion.DECAY_FLD, decay); this.Nbr_Terms = Nbr_Terms; }
From source file:com.hrstc.lucene.queryexpansion.PatentRocchioQueryExpansion.java
License:Apache License
public PatentRocchioQueryExpansion(String indexDir, int Nbr_Docs, int Nbr_Terms, int source, float alpha, float beta, float gamma, float decay) throws IOException, Exception { Directory dir = FSDirectory.open(new File(indexDir)); searcher = new IndexSearcher(DirectoryReader.open(dir)); parameters = new HashMap<>(); parameters.put(RocchioQueryExpansion.ROCCHIO_ALPHA_FLD, alpha); parameters.put(RocchioQueryExpansion.ROCCHIO_BETA_FLD, beta); parameters.put(RocchioQueryExpansion.ROCCHIO_GAMMA_FLD, gamma); parameters.put(RocchioQueryExpansion.DECAY_FLD, decay); this.Nbr_Docs = Nbr_Docs; this.Nbr_Terms = Nbr_Terms; this.source = source; if (source <= 0 || source == 4 || source == 6 || source > 7) { throw new Exception("Invalid source of expansion!"); }//from w w w . j av a2 s . co m }
From source file:com.hrstc.lucene.queryreduction.PatentClassCodeBasedQueryReduction.java
License:Apache License
public PatentClassCodeBasedQueryReduction(String classCodesIndexDir, int Nbr_Terms) throws IOException, Exception { Directory classCodesDir = FSDirectory.open(new File(classCodesIndexDir)); classCodesSearcher = new IndexSearcher(DirectoryReader.open(classCodesDir)); this.Nbr_Terms = Nbr_Terms; }
From source file:com.hrstc.lucene.queryreduction.PatentRocchioQueryReduction.java
License:Apache License
public PatentRocchioQueryReduction(String indexDir, int Nbr_Docs, int Nbr_Terms, int source, float alpha, float beta, float gamma, float decay) throws IOException, Exception { Directory dir = FSDirectory.open(new File(indexDir)); searcher = new IndexSearcher(DirectoryReader.open(dir)); parameters = new HashMap<>(); parameters.put(RocchioQueryReduction.ROCCHIO_ALPHA_FLD, alpha); parameters.put(RocchioQueryReduction.ROCCHIO_BETA_FLD, beta); parameters.put(RocchioQueryReduction.ROCCHIO_GAMMA_FLD, gamma); parameters.put(RocchioQueryReduction.DECAY_FLD, decay); this.Nbr_Docs = Nbr_Docs; this.Nbr_Terms = Nbr_Terms; this.source = source; if (source <= 0 || source == 4 || source == 6 || source > 7) { throw new Exception("Invalid source of expansion!"); }/* w ww.ja va 2 s.c o m*/ }
From source file:com.humi.lucene.test.SearchFiles.java
License:Apache License
/** Simple command-line based search demo. */ public static void main(String[] args) throws Exception { String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details."; if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) { System.out.println(usage); System.exit(0);/*w w w . j av a 2 s. co m*/ } String index = "index"; String field = "contents"; String queries = null; int repeat = 0; boolean raw = false; String queryString = null; int hitsPerPage = 10; for (int i = 0; i < args.length; i++) { if ("-index".equals(args[i])) { index = args[i + 1]; i++; } else if ("-field".equals(args[i])) { field = args[i + 1]; i++; } else if ("-queries".equals(args[i])) { queries = args[i + 1]; i++; } else if ("-query".equals(args[i])) { queryString = args[i + 1]; i++; } else if ("-repeat".equals(args[i])) { repeat = Integer.parseInt(args[i + 1]); i++; } else if ("-raw".equals(args[i])) { raw = true; } else if ("-paging".equals(args[i])) { hitsPerPage = Integer.parseInt(args[i + 1]); if (hitsPerPage <= 0) { System.err.println("There must be at least 1 hit per page."); System.exit(1); } i++; } } IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index))); IndexSearcher searcher = new IndexSearcher(reader); // :Post-Release-Update-Version.LUCENE_XY: Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_10_0); BufferedReader in = null; if (queries != null) { in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), StandardCharsets.UTF_8)); } else { in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)); } // :Post-Release-Update-Version.LUCENE_XY: QueryParser parser = new QueryParser(Version.LUCENE_4_10_0, field, analyzer); while (true) { if (queries == null && queryString == null) { // prompt the user System.out.println("Enter query: "); } String line = queryString != null ? queryString : in.readLine(); if (line == null || line.length() == -1) { break; } line = line.trim(); if (line.length() == 0) { break; } Query query = parser.parse(line); System.out.println("Searching for: " + query.toString(field)); if (repeat > 0) { // repeat & time as benchmark Date start = new Date(); for (int i = 0; i < repeat; i++) { searcher.search(query, null, 100); } Date end = new Date(); System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms"); } doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null); if (queryString != null) { break; } } reader.close(); }
From source file:com.ibm.watson.developer_cloud.professor_languo.pipeline.primary_search.LuceneSearcher.java
License:Open Source License
@Override public void initialize(Properties properties) throws SearchException { String resDirPath = properties.getProperty(ConfigurationConstants.INGESTION_BASE_DIR) + File.separator; if (properties.getProperty(ConfigurationConstants.INDEX_DIR_TYPE) .equals(ConfigurationConstants.IndexDirTypes.FS.toString())) { String indexDirPath = resDirPath + properties.getProperty(ConfigurationConstants.INDEX_DIR); try {//from w w w . j a va2 s.c o m Directory indexDir = FSDirectory.open(new File(indexDirPath).toPath()); indexReader = DirectoryReader.open(indexDir); searcher = new IndexSearcher(indexReader); } catch (IOException e) { throw new SearchException(e); } } else { throw new SearchException(Messages.getString("RetrieveAndRank.LUCENE_SEARCHER_INIT")); //$NON-NLS-1$ } candidateAnswerNum = Integer.parseInt(properties.getProperty(ConfigurationConstants.CANDIDATE_ANSWER_NUM)); }
From source file:com.ibm.watson.developer_cloud.professor_languo.pipeline.primary_search.LuceneSearcher.java
License:Open Source License
/** * @deprecated - should only be used in the Unit Tests<br> * Initialize this {@link LuceneSearcher} using a given Lucene directory * * @param indexDir - The {@link Directory} containing the index * @throws SearchException/*ww w . ja va 2 s .c o m*/ */ private void initialize(Directory indexDir) throws SearchException { try { indexReader = DirectoryReader.open(indexDir); searcher = new IndexSearcher(indexReader); } catch (IOException e) { throw new SearchException(e); } }
From source file:com.icdd.lucence.SearchFiles.java
License:Apache License
public void searchFiles() { // //from w w w.j av a 2 s. co m String index = "F:/download/index/"; // String field = "contents"; // String queries = null; // String queryString = null; int repeat = 0; // ???? boolean raw = false; // ?10? int hitsPerPage = 10; try { IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index))); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(); BufferedReader in = null; if (queries != null) { in = Files.newBufferedReader(Paths.get(queries), StandardCharsets.UTF_8); } else { in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)); } QueryParser parser = new QueryParser(field, analyzer); while (true) { if (queryString == null && queries == null) { System.out.println("Enter query:"); } String line = queryString != null ? queryString : in.readLine(); if (line == null || line.length() == -1) { break; } line = line.trim(); if (line.length() == 0) { break; } try { Query query = parser.parse(line); System.out.println("Searching for:" + query.toString(field)); if (repeat > 0) { // repeat & time as benchmark Date start = new Date(); for (int i = 0; i < repeat; i++) { searcher.search(query, 100); } Date end = new Date(); System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms"); } doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null); } catch (ParseException e) { e.printStackTrace(); } if (queryString != null) { break; } } reader.close(); } catch (IOException e) { e.printStackTrace(); } }