List of usage examples for org.apache.lucene.search TopDocsCollector topDocs
public TopDocs topDocs(int start)
From source file:org.wso2.carbon.analytics.dataservice.core.indexing.AnalyticsDataIndexer.java
License:Open Source License
@SuppressWarnings("rawtypes") private List<SearchResultEntry> doSearch(Set<Integer> shardIndices, int tenantId, String tableName, String query, int start, int count, List<SortByField> sortByFields) throws AnalyticsIndexException { List<SearchResultEntry> results = new ArrayList<>(); IndexReader reader = null;/* ww w . j av a 2s . co m*/ ExecutorService searchExecutor = Executors.newCachedThreadPool(); if (count <= 0) { log.error("Record Count/Page size is ZERO!. Please set Record count/Page size."); } try { reader = this.getCombinedIndexReader(shardIndices, tenantId, tableName); IndexSearcher searcher = new IndexSearcher(reader, searchExecutor); Map<String, ColumnDefinition> indices = this.lookupIndices(tenantId, tableName); Query indexQuery = getSearchQueryFromString(query, indices); TopDocsCollector collector = getTopDocsCollector(start, count, sortByFields, indices); searcher.search(indexQuery, collector); ScoreDoc[] hits = collector.topDocs(start).scoreDocs; Document indexDoc; for (ScoreDoc doc : hits) { indexDoc = searcher.doc(doc.doc); results.add(new SearchResultEntry(indexDoc.get(INDEX_ID_INTERNAL_FIELD), doc.score)); } if (log.isDebugEnabled()) { log.debug("Local Search: " + results.size()); } return results; } catch (Exception e) { log.error("Error in index search: " + e.getMessage(), e); throw new AnalyticsIndexException("Error in index search: " + e.getMessage(), e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { log.error("Error in closing the reader: " + e.getMessage(), e); ; } } searchExecutor.shutdown(); } }