List of usage examples for org.apache.lucene.index DirectoryReader open
public static DirectoryReader open(final IndexCommit commit) throws IOException
From source file:com.doculibre.constellio.lucene.LuceneSearchResultsProvider.java
License:Open Source License
private synchronized void initIfNecessary() { if (topDocs == null) { try {//ww w . j a v a2 s . c o m Directory directory = FSDirectory.open(indexDir); Analyzer analyzer = analyzerProvider.getAnalyzer(Locale.FRENCH); MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_44, searchFields, analyzer); parser.setAllowLeadingWildcard(true); try { luceneQuery = parser.parse(luceneTextQuery); } catch (ParseException e) { try { luceneQuery = parser.parse(QueryParser.escape(luceneTextQuery)); } catch (ParseException e1) { throw new RuntimeException(e1); } } indexReader = DirectoryReader.open(directory); indexSearcher = new IndexSearcher(indexReader); // Sort sort; // if (sortField != null) { // sort = new Sort(new SortField(sortField, Locale.CANADA_FRENCH, Boolean.FALSE.equals(sortAscending))); // } else { // sort = null; // } topDocs = indexSearcher.search(luceneQuery, indexReader.maxDoc()); directory.close(); } catch (CorruptIndexException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } }
From source file:com.doculibre.constellio.services.ImportExportServicesImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w w w .j a v a2 s . 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/* ww w . j a va 2 s . co 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;// w w w . j a va2 s . c om try { 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; }
From source file:com.dreamerpartner.codereview.lucene.SearchHelper.java
License:Apache License
/** * ?/*from ww w. j a v a2 s. c o m*/ * @param module ? * @param fields ? * @param queryStr ? * @param sort * @param pageNo * @param pageSize * @return */ @SuppressWarnings("deprecation") public static PageBean<Document> search(String module, String[] fields, String[] queryStr, Sort sort, int pageNo, int pageSize) { if (ArrayUtils.isEmpty(fields) || ArrayUtils.isEmpty(queryStr)) return null; IndexReader reader = null; try { reader = DirectoryReader.open(FSDirectory.open(new File(LuceneUtil.getIndexPath(module)))); IndexSearcher searcher = new IndexSearcher(reader); BooleanClause.Occur[] flags = new BooleanClause.Occur[fields.length]; for (int i = 0; i < fields.length; i++) { flags[i] = BooleanClause.Occur.SHOULD; } Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_10_0); Query query = MultiFieldQueryParser.parse(Version.LUCENE_4_10_0, queryStr, fields, flags, analyzer); // return doPagingSearch(searcher, query, sort, pageNo, pageSize); } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) reader.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
From source file:com.dreamerpartner.codereview.lucene.SearchHelper.java
License:Apache License
/** * //from ww w.j ava 2 s . c om * @param groupField * @param searchField * @param searchStr * @param pageNo * @param pageSize * @param orderField ? * @param orderFieldType ? * @param desc ? ?? * @return */ @SuppressWarnings("deprecation") public static Map<String, List<Document>> group(String module, String groupField, String searchField, String searchStr, int pageNo, int pageSize, String orderField, Type orderFieldType, boolean desc) { Map<String, List<Document>> result = new LinkedHashMap<String, List<Document>>(10); IndexReader reader = null; try { reader = DirectoryReader.open(FSDirectory.open(new File(LuceneUtil.getIndexPath(module)))); IndexSearcher indexSearcher = new IndexSearcher(reader); GroupingSearch groupingSearch = new GroupingSearch(groupField); Sort sort = new Sort(new SortField(orderField, orderFieldType, desc)); groupingSearch.setGroupSort(sort); groupingSearch.setSortWithinGroup(sort); groupingSearch.setFillSortFields(true); groupingSearch.setCachingInMB(4.0, true); groupingSearch.setAllGroups(true); //groupingSearch.setAllGroupHeads(true); groupingSearch.setGroupDocsLimit(pageSize); QueryParser parser = new QueryParser(Version.LUCENE_4_10_0, searchField, new StandardAnalyzer(Version.LUCENE_4_10_0)); Query query = parser.parse(searchStr); TopGroups<BytesRef> groupResult = groupingSearch.search(indexSearcher, query, (pageNo - 1) * pageSize, pageSize); System.out.println("?" + groupResult.totalHitCount + ", ?" + groupResult.groups.length); List<Document> groupData = null; for (GroupDocs<BytesRef> groupDocs : groupResult.groups) { groupData = new ArrayList<Document>(pageSize); String groupName = groupDocs.groupValue.utf8ToString(); for (ScoreDoc scoreDoc : groupDocs.scoreDocs) { groupData.add(indexSearcher.doc(scoreDoc.doc)); } result.put(groupName, groupData); groupData = null; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) reader.close(); } catch (IOException e) { e.printStackTrace(); } } return result; }
From source file:com.dreamerpartner.codereview.lucene.SearchHelper.java
License:Apache License
/** Simple command-line based search demo. */ @SuppressWarnings("deprecation") public static void main(String[] args) throws Exception { String module = "test"; String field = "contents"; String queryString = "produce"; int pageNo = 1; int pageSize = 10; Sort sort = new Sort(new SortField("createTime", Type.LONG)); IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(LuceneUtil.getIndexPath(module)))); IndexSearcher searcher = new IndexSearcher(reader); // :Post-Release-Update-Version.LUCENE_XY: Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_10_0); // :Post-Release-Update-Version.LUCENE_XY: QueryParser parser = new QueryParser(Version.LUCENE_4_10_0, field, analyzer); Query query = parser.parse(queryString); System.out.println("Searching for: " + query.toString(field)); ///*from w w w . j av a 2 s .com*/ PageBean<Document> docs = doPagingSearch(searcher, query, sort, pageNo, pageSize); System.out.println(JsonUtil.toString(docs)); reader.close(); }
From source file:com.eclipsesource.connect.search.Searcher.java
License:Open Source License
private void performSearch(LuceneSearchResult searchResult, String query) { try (DirectoryReader reader = DirectoryReader.open(directory)) { IndexSearcher searcher = new IndexSearcher(reader); FuzzyQuery luceneQuery = new FuzzyQuery(new Term(Indexer.CONTENT_KEY, query)); TopDocs topDocs = searcher.search(luceneQuery, Integer.MAX_VALUE); populateSearchResult(searchResult, reader, topDocs); } catch (IOException shouldNotHappen) { throw new IllegalStateException(shouldNotHappen); }// w ww . j a va 2s . co m }
From source file:com.eden.lucene.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 . ja v a 2 s . co m*/ } String basePath = "D:/test/lucene"; String index = basePath + "/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_46); 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_46, 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.epam.catgenome.dao.index.FeatureIndexDao.java
License:Open Source License
/** * Queries a feature index of a project, specified by ID * * @param projectId ID of a project, which index to work with * @param query a query to search in index * @return a {List} of {@code FeatureIndexEntry} objects that satisfy index query * @deprecated// w w w. j av a2 s . co m * @throws IOException */ @Deprecated private IndexSearchResult searchLuceneIndexForProject(final long projectId, Query query, List<String> vcfInfoFields, Integer maxResultsCount, Sort sort) throws IOException { Map<Integer, FeatureIndexEntry> entryMap = new LinkedHashMap<>(); int totalHits = 0; try (Directory index = fileManager.getIndexForProject(projectId); IndexReader reader = DirectoryReader.open(index)) { if (reader.numDocs() == 0) { return new IndexSearchResult(Collections.emptyList(), false, 0); } IndexSearcher searcher = new IndexSearcher(reader); final TopDocs docs; int resultsCount = maxResultsCount == null ? reader.numDocs() : maxResultsCount; if (sort == null) { docs = searcher.search(query, resultsCount); } else { docs = searcher.search(query, resultsCount, sort); } totalHits = docs.totalHits; final ScoreDoc[] hits = docs.scoreDocs; Map<Long, BookmarkIndexEntry> foundBookmarkEntries = new HashMap<>(); // for batch bookmarks loading createIndexEntries(hits, entryMap, foundBookmarkEntries, searcher, vcfInfoFields); setBookmarks(foundBookmarkEntries); } catch (IOException e) { LOGGER.error(MessageHelper.getMessage(MessagesConstants.ERROR_FEATURE_INDEX_SEARCH_FAILED), e); return new IndexSearchResult(Collections.emptyList(), false, 0); } return new IndexSearchResult(new ArrayList<>(entryMap.values()), maxResultsCount != null && totalHits > maxResultsCount, totalHits); }