CavalliumDBEngine/src/main/java/it/cavallium/dbengine/lucene/searcher/AdaptiveLocalSearcher.java

81 lines
3.1 KiB
Java
Raw Normal View History

package it.cavallium.dbengine.lucene.searcher;
2022-01-28 21:12:10 +01:00
import static it.cavallium.dbengine.lucene.searcher.GlobalQueryRewrite.NO_REWRITE;
2021-09-18 18:34:21 +02:00
import it.cavallium.dbengine.database.disk.LLIndexSearcher;
2022-07-02 11:44:13 +02:00
import it.cavallium.dbengine.lucene.LuceneUtils;
import java.io.IOException;
2021-12-12 16:41:49 +01:00
import org.jetbrains.annotations.Nullable;
public class AdaptiveLocalSearcher implements LocalSearcher {
2022-03-15 12:36:33 +01:00
private static final StandardSearcher standardSearcher = new StandardSearcher();
2021-12-12 16:41:49 +01:00
private static final LocalSearcher scoredPaged = new PagedLocalSearcher();
2021-11-09 00:05:26 +01:00
private static final LocalSearcher countSearcher = new CountMultiSearcher();
2021-07-06 01:30:37 +02:00
2021-12-23 02:13:51 +01:00
private static final MultiSearcher unsortedUnscoredContinuous = new UnsortedStreamingMultiSearcher();
2021-11-21 12:31:23 +01:00
2021-12-12 23:40:30 +01:00
/**
* Use in-memory collectors if the expected results count is lower or equal than this limit
*/
private final int maxInMemoryResultEntries;
public AdaptiveLocalSearcher(int maxInMemoryResultEntries) {
2021-12-12 23:40:30 +01:00
this.maxInMemoryResultEntries = maxInMemoryResultEntries;
2021-11-21 12:31:23 +01:00
}
@Override
public LuceneSearchResult collect(LLIndexSearcher indexSearcher,
2021-07-06 01:30:37 +02:00
LocalQueryParams queryParams,
2022-02-26 03:28:20 +01:00
@Nullable String keyFieldName,
2022-01-28 21:12:10 +01:00
GlobalQueryRewrite transformer) {
2022-07-02 11:44:13 +02:00
if (transformer != NO_REWRITE) {
2023-02-22 16:59:35 +01:00
return LuceneUtils.rewrite(this, indexSearcher, queryParams, keyFieldName, transformer);
2022-07-02 11:44:13 +02:00
}
return transformedCollect(indexSearcher, queryParams, keyFieldName, transformer);
}
@Override
public String getName() {
return "adaptivelocal";
}
2021-11-21 12:31:23 +01:00
// Remember to change also AdaptiveMultiSearcher
public LuceneSearchResult transformedCollect(LLIndexSearcher indexSearcher,
LocalQueryParams queryParams,
String keyFieldName,
2022-01-28 21:12:10 +01:00
GlobalQueryRewrite transformer) {
2021-11-21 12:31:23 +01:00
// offset + limit
long realLimit = queryParams.offsetLong() + queryParams.limitLong();
long maxAllowedInMemoryLimit
2021-12-12 23:40:30 +01:00
= Math.max(maxInMemoryResultEntries, (long) queryParams.pageLimits().getPageLimit(0));
2021-11-21 12:31:23 +01:00
if (queryParams.limitLong() == 0) {
return countSearcher.collect(indexSearcher, queryParams, keyFieldName, transformer);
} else if (realLimit <= maxInMemoryResultEntries) {
return standardSearcher.collect(indexSearcher, queryParams, keyFieldName, transformer);
2022-08-12 02:08:15 +02:00
} else if (queryParams.isSorted()) {
if (realLimit <= maxAllowedInMemoryLimit) {
return scoredPaged.collect(indexSearcher, queryParams, keyFieldName, transformer);
2021-11-21 12:31:23 +01:00
} else {
2021-12-23 02:13:51 +01:00
if (queryParams.isSortedByScore()) {
if (queryParams.limitLong() < maxInMemoryResultEntries) {
2021-12-12 23:40:30 +01:00
throw new UnsupportedOperationException("Allowed limit is " + maxInMemoryResultEntries + " or greater");
2021-11-21 12:31:23 +01:00
}
return scoredPaged.collect(indexSearcher, queryParams, keyFieldName, transformer);
2021-11-21 12:31:23 +01:00
} else {
if (queryParams.limitLong() < maxInMemoryResultEntries) {
2021-12-12 23:40:30 +01:00
throw new UnsupportedOperationException("Allowed limit is " + maxInMemoryResultEntries + " or greater");
2021-11-21 12:31:23 +01:00
}
return scoredPaged.collect(indexSearcher, queryParams, keyFieldName, transformer);
2021-11-21 12:31:23 +01:00
}
}
2021-07-06 01:30:37 +02:00
} else {
2021-11-21 12:31:23 +01:00
// Run large/unbounded searches using the continuous multi searcher
return unsortedUnscoredContinuous.collect(indexSearcher, queryParams, keyFieldName, transformer);
2021-07-06 01:30:37 +02:00
}
}
}