List of usage examples for org.apache.lucene.index IndexReader close
@Override public final synchronized void close() throws IOException
From source file:com.common.search.IKAnalyzerDemo.java
License:Apache License
public static void main(String[] args) { // Lucene Document String fieldName = "text"; // // ww w. j a v a 2 s . c o m String text = "IK Analyzer "; // IKAnalyzer Analyzer analyzer = new IKAnalyzer(); Directory directory = null; IndexWriter iwriter = null; IndexReader ireader = null; IndexSearcher isearcher = null; try { // directory = new RAMDirectory(); // IndexWriterConfig IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_34, analyzer); iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); iwriter = new IndexWriter(directory, iwConfig); // Document doc = new Document(); doc.add(new Field("ID", "10000", Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new Field(fieldName, text, Field.Store.YES, Field.Index.ANALYZED)); iwriter.addDocument(doc); iwriter.close(); // ********************************** // ireader = IndexReader.open(directory); isearcher = new IndexSearcher(ireader); String keyword = ""; // QueryParserQuery QueryParser qp = new QueryParser(Version.LUCENE_34, fieldName, analyzer); qp.setDefaultOperator(QueryParser.AND_OPERATOR); Query query = qp.parse(keyword); // 5 TopDocs topDocs = isearcher.search(query, 5); System.out.println(" " + topDocs.totalHits); // ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (int i = 0; i < topDocs.totalHits; i++) { Document targetDoc = isearcher.doc(scoreDocs[i].doc); System.out.println(" " + targetDoc.toString()); } } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } finally { if (ireader != null) { try { ireader.close(); } catch (IOException e) { e.printStackTrace(); } } if (directory != null) { try { directory.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.daniel.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.che.org/java/4_0/demo.html for details."; if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) { System.out.println(usage); System.exit(0);// w w w .jav a 2 s . c o 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); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); BufferedReader in = null; if (queries != null) { in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8")); } else { in = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); } QueryParser parser = new QueryParser(Version.LUCENE_40, 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.devb.search.IndicSearcher.java
License:Apache License
private void callSearch(boolean j) { System.out.println("Servlet Ctx " + servletContext.getRealPath("/")); String indexPath = servletContext.getRealPath("/") + "/hindex/"; String docsPath = servletContext.getRealPath("/") + "/hdocs/"; final File docDir = new File(docsPath); if (!docDir.exists() || !docDir.canRead()) { System.out.println("Document directory '" + docDir.getAbsolutePath() + "' does not exist or is not readable, " + "please check the path\n"); return;// ww w . j av a2 s . c om } IndexReader reader = null; IndexSearcher searcher = null; Analyzer analyzer = null; String field = "contents"; try { reader = DirectoryReader.open(FSDirectory.open(new File(indexPath))); searcher = new IndexSearcher(reader); analyzer = new HindiAnalyzer(); } catch (IOException ioe) { ioe.printStackTrace(); } QueryParser parser = new QueryParser(field, analyzer); String /*ByteBuffer*/ line = null; Query query = null; try { // line = Charset.forName("UTF-8").encode(this.id); line = this.id; // line = line.trim(); if (line == null) { return; } System.out.println("Hindi StandardSearcher / callSearch Line " + line); query = parser.parse(line); System.out.println("Hindi StandardSearcher / callSearch Hindi Query " + query); final int maxHits = 10; ScoreDoc[] hits = searcher.search(query, null, maxHits).scoreDocs; try { // Iterate through the results: for (int i = 0; i < hits.length; i++) { Document hitDoc = searcher.doc(hits[i].doc); if (j) { JSONObject jo = new JSONObject(); jo.put("query", query.toString(field)); jo.put("path", hitDoc.get("path")); jo.put("line", hitDoc.get("linenumber")); jo.put("contents", hitDoc.get("contents")); ja.put(jo); } else { SearchResult ns = new SearchResult(); ns.setQuery(query.toString(field)); ns.setDocPath(hitDoc.get("path")); ns.setLineNum(hitDoc.get("linenumber")); ns.setContents(hitDoc.get("contents")); contentProvider.put(String.valueOf(i), ns); } } } catch (Exception ito) { ito.printStackTrace(); } } catch (Exception ex) { ex.printStackTrace(); } try { reader.close(); } catch (IOException ioe) { } }
From source file:com.devb.search.StandardSearcher.java
License:Apache License
private void callSearch(boolean j) { System.out.println("Servlet Ctx " + servletContext.getRealPath("/")); String indexPath = servletContext.getRealPath("/") + "/index/"; String docsPath = servletContext.getRealPath("/") + "/docs/"; final File docDir = new File(docsPath); if (!docDir.exists() || !docDir.canRead()) { System.out.println("Document directory '" + docDir.getAbsolutePath() + "' does not exist or is not readable, " + "please check the path\n"); return;/*w w w. j ava 2 s.c om*/ } IndexReader reader = null; IndexSearcher searcher = null; Analyzer analyzer = null; String field = "contents"; try { reader = DirectoryReader.open(FSDirectory.open(new File(indexPath))); searcher = new IndexSearcher(reader); analyzer = new StandardAnalyzer(); } catch (IOException ioe) { ioe.printStackTrace(); } QueryParser parser = new QueryParser(field, analyzer); String line = null; Query query = null; try { line = this.id; line = line.trim(); if (line.length() == 0) { return; } query = parser.parse(line); final int maxHits = 10; ScoreDoc[] hits = searcher.search(query, null, maxHits).scoreDocs; try { // Iterate through the results: for (int i = 0; i < hits.length; i++) { Document hitDoc = searcher.doc(hits[i].doc); if (j) { JSONObject jo = new JSONObject(); jo.put("query", query.toString(field)); jo.put("path", hitDoc.get("path")); jo.put("line", hitDoc.get("linenumber")); jo.put("contents", hitDoc.get("contents")); ja.put(jo); } else { SearchResult ns = new SearchResult(); ns.setQuery(query.toString(field)); ns.setDocPath(hitDoc.get("path")); ns.setLineNum(hitDoc.get("linenumber")); ns.setContents(hitDoc.get("contents")); contents.put(String.valueOf(i), ns); } } } catch (Exception ito) { ito.printStackTrace(); } } catch (Exception ex) { ex.printStackTrace(); } try { reader.close(); } catch (IOException ioe) { } }
From source file:com.doculibre.constellio.lucene.BaseLuceneIndexHelper.java
License:Open Source License
@Override public synchronized boolean isEmpty() { boolean empty; try {/*from w w w. ja va 2s . co m*/ Directory directory = FSDirectory.open(indexDir); IndexReader indexReader = DirectoryReader.open(directory); empty = indexReader.numDocs() <= 1; indexReader.close(); directory.close(); } catch (IOException e) { throw new RuntimeException(e); } return empty; }
From source file:com.doculibre.constellio.lucene.BaseLuceneIndexHelper.java
License:Open Source License
protected synchronized int getDocNum(T object) { int docNum;//from w w w . j a va 2s. c om String uniqueIndexFieldName = getUniqueIndexFieldName(); String uniqueIndexFieldValue = getUniqueIndexFieldValue(object); if (uniqueIndexFieldValue != null) { String query = uniqueIndexFieldName + ":" + uniqueIndexFieldValue; try { Analyzer analyzer = analyzerProvider.getAnalyzer(Locale.FRENCH); QueryParser multiFielsQP = new QueryParser(Version.LUCENE_44, uniqueIndexFieldName, analyzer); Query luceneQuery = multiFielsQP.parse(query); Directory directory = FSDirectory.open(indexDir); IndexReader reader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(reader); TopDocs topDocs = indexSearcher.search(luceneQuery, reader.maxDoc()); if (topDocs.totalHits > 0) { docNum = topDocs.scoreDocs[0].doc; } else { docNum = -1; } // indexSearcher.close(); // TODO add finally reader.close(); directory.close(); } catch (ParseException e) { throw new RuntimeException(e); } catch (CorruptIndexException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } else { docNum = -1; } return docNum; }
From source file:com.doculibre.constellio.lucene.BaseLuceneIndexHelper.java
License:Open Source License
public String highlight(String strToHighlight, String fieldName, Query luceneQuery) { String highlightedText;//from w w w.ja va 2 s.co m Analyzer analyzer = analyzerProvider.getAnalyzer(Locale.FRENCH); try { Directory directory = FSDirectory.open(indexDir); IndexReader indexReader = DirectoryReader.open(directory); Query rewrittenLuceneQuery = luceneQuery.rewrite(indexReader); QueryScorer luceneScorer = new QueryScorer(rewrittenLuceneQuery); SimpleHTMLFormatter luceneFormatter = new SimpleHTMLFormatter("<span class=\"hit\">", "</span>"); Highlighter luceneHighlighter = new Highlighter(luceneFormatter, luceneScorer); Fragmenter luceneFragmenter; // Si la chaine highlighter est sup 250 carac if (strToHighlight.length() > TAILLE_CHAINE_NON_FRAGMENTEE) { // Cration de best fragments de 100 carac chaque luceneFragmenter = new SimpleFragmenter(TAILLE_FRAGMENT); } else { // Toute la chaine est highlight luceneFragmenter = new SimpleFragmenter(Integer.MAX_VALUE); } luceneHighlighter.setTextFragmenter(luceneFragmenter); TokenStream luceneTokenStream = analyzer.tokenStream(fieldName, new StringReader(strToHighlight)); String fragment = null; if (strToHighlight.length() > TAILLE_CHAINE_NON_FRAGMENTEE) { fragment = luceneHighlighter.getBestFragments(luceneTokenStream, strToHighlight, NB_BEST_FRAGMENT, FRAGMENT_SEP); } else { fragment = luceneHighlighter.getBestFragment(luceneTokenStream, strToHighlight); } if (StringUtils.isBlank(fragment) && fieldName.equalsIgnoreCase("titre")) { fragment = strToHighlight; } indexReader.close(); directory.close(); highlightedText = fragment; } catch (IOException e) { throw new RuntimeException(e); } catch (InvalidTokenOffsetsException e) { throw new RuntimeException(e); } return highlightedText; }
From source file:com.doculibre.constellio.services.ImportExportServicesImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Override// w w w. ja v a 2s .c o m public void importData(Directory directory, RecordCollection collection, ProgressInfo progressInfo) { try { ConnectorInstance connectorInstance = collection.getConnectorInstances().iterator().next(); RecordServices recordServices = ConstellioSpringUtils.getRecordServices(); String uniqueKeyMetaName = null; IndexField uniqueKeyIndexField = collection.getUniqueKeyIndexField(); for (ConnectorInstanceMeta connectorInstanceMeta : uniqueKeyIndexField.getConnectorInstanceMetas()) { if (connectorInstance.equals(connectorInstanceMeta.getConnectorInstance())) { uniqueKeyMetaName = connectorInstanceMeta.getName(); break; } } Pattern invalidDatePattern = Pattern .compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\\.[0-9]*)?$"); IndexReader indexReader = DirectoryReader.open(directory); if (progressInfo != null) { progressInfo.setTotal(indexReader.numDocs()); } for (int i = 0; i < indexReader.numDocs(); i++) { Document document = indexReader.document(i); Record record = new Record(); record.setLastModified(new Date()); record.setConnectorInstance(connectorInstance); for (IndexableField field : document.getFields()) { // for (String fieldName : (Collection<String>) // indexReader.getFieldNames(FieldOption.ALL)) { if (field != null && field.fieldType().stored() && field.binaryValue() == null) { String metaName = field.name(); String metaContent = field.stringValue(); Matcher invalidDateMatcher = invalidDatePattern.matcher(metaContent); if (invalidDateMatcher.matches()) { metaContent = metaContent + "Z"; } if (uniqueKeyMetaName.equals(metaName)) { record.setUrl(metaContent); } RecordMeta meta = new RecordMeta(); ConnectorInstanceMeta connectorInstanceMeta = connectorInstance.getOrCreateMeta(metaName); meta.setConnectorInstanceMeta(connectorInstanceMeta); meta.setRecord(record); meta.setContent(metaContent); record.addContentMeta(meta); } } try { recordServices.makePersistent(record); // if (i % 500 == 0) { // EntityManager entityManager = // ConstellioPersistenceContext.getCurrentEntityManager(); // entityManager.getTransaction().commit(); // entityManager.getTransaction().begin(); // } } catch (Exception e) { e.printStackTrace(); } if (progressInfo != null) { progressInfo.setCurrentIndex(i); } } indexReader.close(); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.doculibre.constellio.services.ImportExportServicesImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/*from ww w . j ava2 s . c o m*/ public void convertData(Directory directory, OutputStream output) { try { WritableWorkbook workbook = Workbook.createWorkbook(output); WritableSheet sheet = workbook.createSheet("fields", 0); WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10); WritableCellFormat arial10format = new WritableCellFormat(arial10font); IndexReader indexReader = DirectoryReader.open(directory); // { // int column = 0; // for (String fieldName : (Collection<String>) indexReader.getFieldNames()) { // Label label = new Label(column, 0, fieldName, arial10format); // sheet.addCell(label); // column++; // } // } int row = 1; for (int i = 0; i < indexReader.numDocs() /* && i != 502 */; i++) { Document document = indexReader.document(i); int column = 0; for (IndexableField field : document.getFields()) { if (row == 1) { Label label = new Label(column, 0, field.name(), arial10format); sheet.addCell(label); } if (field != null && field.fieldType().stored() && field.binaryValue() == null) { String indexedContent = field.stringValue(); indexedContent = convertText(indexedContent); Label label = new Label(column, row, indexedContent, arial10format); sheet.addCell(label); } column++; } row++; // if (i == 502) { // break; // } } indexReader.close(); workbook.write(); workbook.close(); } catch (IOException e) { throw new RuntimeException(e); } catch (RowsExceededException e) { throw new RuntimeException(e); } catch (WriteException e) { throw new RuntimeException(e); } }
From source file:com.dreamerpartner.codereview.lucene.SearchHelper.java
License:Apache License
@SuppressWarnings("deprecation") public static Document getById(String module, String id) { if (StringUtils.isEmpty(id)) return null; IndexReader reader = null; try {// www.j a va 2 s . c om reader = DirectoryReader.open(FSDirectory.open(new File(LuceneUtil.getIndexPath(module)))); IndexSearcher searcher = new IndexSearcher(reader); BufferedReader in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_10_0); //? QueryParser parser = new QueryParser(Version.LUCENE_4_10_0, "id", analyzer); Query query = parser.parse(id); return searchOne(in, searcher, query); } catch (Exception e) { logger.error(id + " getById fail.", e); } finally { try { if (reader != null) reader.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }