use of org.apache.lucene.search.Sort in project zm-mailbox by Zimbra.
the class LuceneIndex method warmup.
/**
* Runs a common search query + common sort order (and throw away the result) to warm up the Lucene cache and OS
* file system cache.
*/
@Override
public synchronized void warmup() {
if (SEARCHER_CACHE.asMap().containsKey(mailbox.getId()) || GAL_SEARCHER_CACHE.containsKey(mailbox.getId())) {
// already warmed up
return;
}
long start = System.currentTimeMillis();
IndexSearcher searcher = null;
try {
searcher = (IndexSearcher) openSearcher();
searcher.search(new TermQuery(new Term(LuceneFields.L_CONTENT, "zimbra")), 1, new Sort(new SortField(LuceneFields.L_SORT_DATE, SortField.STRING, true)));
} catch (IOException e) {
ZimbraLog.search.warn("Failed to warm up", e);
} finally {
Closeables.closeQuietly(searcher);
}
ZimbraLog.search.debug("WarmUpLuceneSearcher elapsed=%d", System.currentTimeMillis() - start);
}
use of org.apache.lucene.search.Sort in project jackrabbit by apache.
the class QueryEngine method execute.
protected QueryResult execute(Column[] columns, Selector selector, Constraint constraint, Ordering[] orderings, long offset, long limit, int printIndentation) throws RepositoryException {
long time = System.currentTimeMillis();
Map<String, NodeType> selectorMap = getSelectorNames(selector);
String[] selectorNames = selectorMap.keySet().toArray(new String[selectorMap.size()]);
Map<String, PropertyValue> columnMap = getColumnMap(columns, selectorMap);
String[] columnNames = columnMap.keySet().toArray(new String[columnMap.size()]);
Sort sort = new Sort();
if (NATIVE_SORT) {
sort = new Sort(createSortFields(orderings, session));
}
// if true it means that the LuceneQueryFactory should just let the
// QueryEngine take care of sorting and applying offset and limit
// constraints
boolean externalSort = !NATIVE_SORT;
RowIterator rows = null;
try {
rows = new RowIteratorAdapter(lqf.execute(columnMap, selector, constraint, sort, externalSort, offset, limit));
} catch (IOException e) {
throw new RepositoryException("Failed to access the query index", e);
} finally {
log.debug("{}SQL2 SELECT took {} ms. selector: {}, columns: {}, constraint: {}, offset {}, limit {}", new Object[] { genString(printIndentation), System.currentTimeMillis() - time, selector, Arrays.toString(columnNames), constraint, offset, limit });
}
QueryResult result = new SimpleQueryResult(columnNames, selectorNames, rows);
if (NATIVE_SORT) {
return result;
}
long timeSort = System.currentTimeMillis();
QueryResult sorted = sort(result, orderings, evaluator, offset, limit);
log.debug("{}SQL2 SORT took {} ms.", genString(printIndentation), System.currentTimeMillis() - timeSort);
return sorted;
}
use of org.apache.lucene.search.Sort in project jackrabbit by apache.
the class SearchIndex method executeQuery.
/**
* Executes the query on the search index.
*
* @param session the session that executes the query.
* @param queryImpl the query impl.
* @param query the lucene query.
* @param orderProps name of the properties for sort order.
* @param orderSpecs the order specs for the sort order properties.
* <code>true</code> indicates ascending order,
* <code>false</code> indicates descending.
* @param orderFuncs functions for the properties for sort order.
* @param resultFetchHint a hint on how many results should be fetched. @return the query hits.
* @throws IOException if an error occurs while searching the index.
*/
public MultiColumnQueryHits executeQuery(SessionImpl session, AbstractQueryImpl queryImpl, Query query, Path[] orderProps, boolean[] orderSpecs, String[] orderFuncs, long resultFetchHint) throws IOException {
checkOpen();
Sort sort = new Sort(createSortFields(orderProps, orderSpecs, orderFuncs));
final IndexReader reader = getIndexReader(queryImpl.needsSystemTree());
JackrabbitIndexSearcher searcher = new JackrabbitIndexSearcher(session, reader, getContext().getItemStateManager());
searcher.setSimilarity(getSimilarity());
return new FilterMultiColumnQueryHits(searcher.execute(query, sort, resultFetchHint, QueryImpl.DEFAULT_SELECTOR_NAME)) {
public void close() throws IOException {
try {
super.close();
} finally {
Util.closeOrRelease(reader);
}
}
};
}
use of org.apache.lucene.search.Sort in project lucene-solr by apache.
the class TestBackwardsCompatibility method testCreateSortedIndex.
// ant test -Dtestcase=TestBackwardsCompatibility -Dtestmethod=testCreateSortedIndex -Dtests.codec=default -Dtests.useSecurityManager=false -Dtests.bwcdir=/tmp/sorted
public void testCreateSortedIndex() throws Exception {
Path indexDir = getIndexDir().resolve("sorted");
Files.deleteIfExists(indexDir);
Directory dir = newFSDirectory(indexDir);
LogByteSizeMergePolicy mp = new LogByteSizeMergePolicy();
mp.setNoCFSRatio(1.0);
mp.setMaxCFSSegmentSizeMB(Double.POSITIVE_INFINITY);
MockAnalyzer analyzer = new MockAnalyzer(random());
analyzer.setMaxTokenLength(TestUtil.nextInt(random(), 1, IndexWriter.MAX_TERM_LENGTH));
// TODO: remove randomness
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
conf.setMergePolicy(mp);
conf.setUseCompoundFile(false);
conf.setIndexSort(new Sort(new SortField("dateDV", SortField.Type.LONG, true)));
IndexWriter writer = new IndexWriter(dir, conf);
LineFileDocs docs = new LineFileDocs(random());
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT);
parser.setTimeZone(TimeZone.getTimeZone("UTC"));
ParsePosition position = new ParsePosition(0);
Field dateDVField = null;
for (int i = 0; i < 50; i++) {
Document doc = docs.nextDoc();
String dateString = doc.get("date");
position.setIndex(0);
Date date = parser.parse(dateString, position);
if (position.getErrorIndex() != -1) {
throw new AssertionError("failed to parse \"" + dateString + "\" as date");
}
if (position.getIndex() != dateString.length()) {
throw new AssertionError("failed to parse \"" + dateString + "\" as date");
}
if (dateDVField == null) {
dateDVField = new NumericDocValuesField("dateDV", 0l);
doc.add(dateDVField);
}
dateDVField.setLongValue(date.getTime());
if (i == 250) {
writer.commit();
}
writer.addDocument(doc);
}
writer.forceMerge(1);
writer.close();
dir.close();
}
use of org.apache.lucene.search.Sort in project lucene-solr by apache.
the class DefaultIndexingChain method maybeSortSegment.
private Sorter.DocMap maybeSortSegment(SegmentWriteState state) throws IOException {
Sort indexSort = state.segmentInfo.getIndexSort();
if (indexSort == null) {
return null;
}
List<Sorter.DocComparator> comparators = new ArrayList<>();
for (int i = 0; i < indexSort.getSort().length; i++) {
SortField sortField = indexSort.getSort()[i];
PerField perField = getPerField(sortField.getField());
if (perField != null && perField.docValuesWriter != null && finishedDocValues.contains(perField.fieldInfo.name) == false) {
perField.docValuesWriter.finish(state.segmentInfo.maxDoc());
Sorter.DocComparator cmp = perField.docValuesWriter.getDocComparator(state.segmentInfo.maxDoc(), sortField);
comparators.add(cmp);
finishedDocValues.add(perField.fieldInfo.name);
} else {
// safe to ignore, sort field with no values or already seen before
}
}
Sorter sorter = new Sorter(indexSort);
// returns null if the documents are already sorted
return sorter.sort(state.segmentInfo.maxDoc(), comparators.toArray(new Sorter.DocComparator[comparators.size()]));
}
Aggregations