2021-07-06 00:30:14 +02:00
|
|
|
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-20 12:51:27 +02:00
|
|
|
import it.cavallium.dbengine.database.disk.LLIndexSearchers;
|
2022-07-02 11:44:13 +02:00
|
|
|
import it.cavallium.dbengine.lucene.LuceneUtils;
|
2022-01-28 21:12:10 +01:00
|
|
|
import java.io.IOException;
|
2021-12-12 16:41:49 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-07-06 00:30:14 +02:00
|
|
|
|
2021-10-16 14:35:04 +02:00
|
|
|
public class AdaptiveMultiSearcher implements MultiSearcher {
|
2021-07-06 00:30:14 +02:00
|
|
|
|
2022-03-15 12:36:33 +01:00
|
|
|
private static final StandardSearcher standardSearcher = new StandardSearcher();
|
2021-12-12 16:41:49 +01:00
|
|
|
|
2021-11-09 00:05:26 +01:00
|
|
|
private static final MultiSearcher count = new CountMultiSearcher();
|
2021-07-06 00:30:14 +02:00
|
|
|
|
2021-10-15 22:03:53 +02:00
|
|
|
private static final MultiSearcher scoredPaged = new ScoredPagedMultiSearcher();
|
2021-07-27 19:34:51 +02:00
|
|
|
|
2021-12-23 02:13:51 +01:00
|
|
|
private static final MultiSearcher unsortedUnscoredContinuous = new UnsortedStreamingMultiSearcher();
|
2021-09-25 13:07:52 +02: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;
|
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
public AdaptiveMultiSearcher(int maxInMemoryResultEntries) {
|
2021-12-12 23:40:30 +01:00
|
|
|
this.maxInMemoryResultEntries = maxInMemoryResultEntries;
|
2021-10-13 00:23:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-06 00:30:14 +02:00
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public LuceneSearchResult collectMulti(LLIndexSearchers indexSearchers,
|
2021-09-19 19:59: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.rewriteMulti(this, indexSearchers, queryParams, keyFieldName, transformer);
|
2022-07-02 11:44:13 +02:00
|
|
|
}
|
2023-02-09 23:34:25 +01:00
|
|
|
return transformedCollectMulti(indexSearchers, queryParams, keyFieldName, transformer);
|
2021-10-08 11:50:28 +02:00
|
|
|
}
|
|
|
|
|
2021-11-21 12:31:23 +01:00
|
|
|
// Remember to change also AdaptiveLocalSearcher
|
2023-02-09 23:34:25 +01:00
|
|
|
public LuceneSearchResult transformedCollectMulti(LLIndexSearchers indexSearchers,
|
2021-10-08 11:50:28 +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) {
|
2021-10-08 11:50:28 +02:00
|
|
|
// offset + limit
|
2021-10-15 22:03:53 +02:00
|
|
|
long realLimit = queryParams.offsetLong() + queryParams.limitLong();
|
2021-10-22 01:26:22 +02:00
|
|
|
long maxAllowedInMemoryLimit
|
2021-12-12 23:40:30 +01:00
|
|
|
= Math.max(maxInMemoryResultEntries, (long) queryParams.pageLimits().getPageLimit(0));
|
2021-10-08 11:50:28 +02:00
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
if (queryParams.limitLong() == 0) {
|
2022-07-02 11:44:13 +02:00
|
|
|
return count.collectMulti(indexSearchers, queryParams, keyFieldName, transformer);
|
2023-02-09 23:34:25 +01:00
|
|
|
} else if (realLimit <= maxInMemoryResultEntries) {
|
2022-07-02 11:44:13 +02:00
|
|
|
return standardSearcher.collectMulti(indexSearchers, queryParams, keyFieldName, transformer);
|
2022-08-12 02:08:15 +02:00
|
|
|
} else if (queryParams.isSorted()) {
|
2023-02-09 23:34:25 +01:00
|
|
|
if (realLimit <= maxAllowedInMemoryLimit) {
|
2022-07-02 11:44:13 +02:00
|
|
|
return scoredPaged.collectMulti(indexSearchers, queryParams, keyFieldName, transformer);
|
2022-06-14 13:10:38 +02:00
|
|
|
} else {
|
|
|
|
if (queryParams.isSortedByScore()) {
|
2023-02-09 23:34:25 +01:00
|
|
|
if (queryParams.limitLong() < maxInMemoryResultEntries) {
|
2022-06-14 13:10:38 +02:00
|
|
|
throw new UnsupportedOperationException("Allowed limit is " + maxInMemoryResultEntries + " or greater");
|
|
|
|
}
|
2023-02-09 23:34:25 +01:00
|
|
|
return scoredPaged.collectMulti(indexSearchers, queryParams, keyFieldName, transformer);
|
2021-10-13 00:23:56 +02:00
|
|
|
} else {
|
2023-02-09 23:34:25 +01:00
|
|
|
if (queryParams.limitLong() < maxInMemoryResultEntries) {
|
2022-06-14 13:10:38 +02:00
|
|
|
throw new UnsupportedOperationException("Allowed limit is " + maxInMemoryResultEntries + " or greater");
|
|
|
|
}
|
2023-02-09 23:34:25 +01:00
|
|
|
return scoredPaged.collectMulti(indexSearchers, queryParams, keyFieldName, transformer);
|
2021-10-13 00:23:56 +02:00
|
|
|
}
|
2021-09-25 13:07:52 +02:00
|
|
|
}
|
2022-06-14 13:10:38 +02:00
|
|
|
} else {
|
|
|
|
// Run large/unbounded searches using the continuous multi searcher
|
2022-07-02 11:44:13 +02:00
|
|
|
return unsortedUnscoredContinuous.collectMulti(indexSearchers, queryParams, keyFieldName, transformer);
|
2022-06-14 13:10:38 +02:00
|
|
|
}
|
2021-07-06 00:30:14 +02:00
|
|
|
}
|
2021-10-13 00:23:56 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
2021-10-13 12:25:32 +02:00
|
|
|
return "adaptive local";
|
2021-10-13 00:23:56 +02:00
|
|
|
}
|
2021-07-06 00:30:14 +02:00
|
|
|
}
|