Rename sort

This commit is contained in:
Andrea Cavalli 2021-10-28 17:18:23 +02:00
parent 52488ad87a
commit d861e26a57
6 changed files with 67 additions and 162 deletions

View File

@ -1,119 +0,0 @@
package it.cavallium.dbengine.client;
import it.cavallium.dbengine.client.query.BasicType;
import it.cavallium.dbengine.client.query.current.data.DocSort;
import it.cavallium.dbengine.client.query.current.data.NoSort;
import it.cavallium.dbengine.client.query.current.data.NumericSort;
import it.cavallium.dbengine.client.query.current.data.RandomSort;
import it.cavallium.dbengine.client.query.current.data.ScoreSort;
import it.cavallium.dbengine.client.query.current.data.Sort;
import it.cavallium.dbengine.database.LLKeyScore;
import java.util.Comparator;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Mono;
public class MultiSort<T> {
private final Sort querySort;
public MultiSort(Sort querySort) {
this.querySort = querySort;
}
public boolean isSorted() {
return querySort.getBasicType$() != BasicType.NoSort;
}
/**
* Sort a lucene field and the results by a numeric sort field and an int value
* @param fieldName Lucene SortedNumericSortField field name
* @param reverse descending sort
* @param <T> result type
* @return MultiSort object
*/
public static <T> MultiSort<T> sortedNumericInt(String fieldName, boolean reverse) {
// Create lucene sort
Sort querySort = NumericSort.of(fieldName, reverse);
// Return the multi sort
return new MultiSort<>(querySort);
}
/**
* Sort a lucene field and the results by a numeric sort field and an long value
* @param fieldName Lucene SortedNumericSortField field name
* @param reverse descending sort
* @param <T> result type
* @return MultiSort object
*/
public static <T> MultiSort<T> sortedNumericLong(String fieldName, boolean reverse) {
// Create lucene sort
Sort querySort = NumericSort.of(fieldName, reverse);
// Return the multi sort
return new MultiSort<>(querySort);
}
public static <T> MultiSort<T> randomSortField() {
return new MultiSort<>(RandomSort.of());
}
public static MultiSort<LLKeyScore> topScoreRaw() {
return new MultiSort<>(ScoreSort.of());
}
public static <T> MultiSort<SearchResultKey<T>> topScore() {
return new MultiSort<>(ScoreSort.of());
}
public static <T> MultiSort<SearchResultKey<T>> noSort() {
return new MultiSort<>(NoSort.of());
}
public static <T> MultiSort<SearchResultKey<T>> docSort() {
return new MultiSort<>(DocSort.of());
}
public static <T> MultiSort<SearchResultKey<T>> numericSort(String field, boolean reverse) {
return new MultiSort<>(NumericSort.of(field, reverse));
}
public static <T, U> MultiSort<SearchResultItem<T, U>> topScoreWithValues() {
return new MultiSort<>(ScoreSort.of());
}
public static <T, U> MultiSort<SearchResultItem<T, U>> noScoreNoSortWithValues() {
return new MultiSort<>(NoSort.of());
}
public Sort getQuerySort() {
return querySort;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MultiSort<?> multiSort = (MultiSort<?>) o;
return Objects.equals(querySort, multiSort.querySort);
}
@Override
public int hashCode() {
return Objects.hash(querySort);
}
@Override
public String toString() {
return querySort.toString();
}
}

View File

@ -0,0 +1,41 @@
package it.cavallium.dbengine.client;
import it.cavallium.dbengine.client.query.BasicType;
import it.cavallium.dbengine.client.query.current.data.DocSort;
import it.cavallium.dbengine.client.query.current.data.NoSort;
import it.cavallium.dbengine.client.query.current.data.NumericSort;
import it.cavallium.dbengine.client.query.current.data.RandomSort;
import it.cavallium.dbengine.client.query.current.data.ScoreSort;
import org.jetbrains.annotations.NotNull;
public record Sort(@NotNull it.cavallium.dbengine.client.query.current.data.Sort querySort) {
public boolean isSorted() {
return querySort.getBasicType$() != BasicType.NoSort;
}
public static Sort random() {
return new Sort(RandomSort.of());
}
public static Sort score() {
return new Sort(ScoreSort.of());
}
public static Sort no() {
return new Sort(NoSort.of());
}
public static Sort doc() {
return new Sort(DocSort.of());
}
public static Sort numeric(String field, boolean reverse) {
return new Sort(NumericSort.of(field, reverse));
}
@Override
public String toString() {
return querySort.toString();
}
}

View File

@ -3,12 +3,11 @@ package it.cavallium.dbengine.client.query;
import io.soabase.recordbuilder.core.RecordBuilder;
import it.cavallium.data.generator.nativedata.Nullablefloat;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.client.MultiSort;
import it.cavallium.dbengine.client.Sort;
import it.cavallium.dbengine.client.query.current.data.NoSort;
import it.cavallium.dbengine.client.query.current.data.Query;
import it.cavallium.dbengine.client.query.current.data.QueryParams;
import it.cavallium.dbengine.client.query.current.data.QueryParamsBuilder;
import it.cavallium.dbengine.database.LLScoreMode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -18,7 +17,7 @@ public final record ClientQueryParams<T>(@Nullable CompositeSnapshot snapshot,
long offset,
long limit,
@Nullable Float minCompetitiveScore,
@Nullable MultiSort<T> sort,
@Nullable Sort sort,
boolean complete) {
public static <T> ClientQueryParamsBuilder<T> builder() {
@ -40,7 +39,7 @@ public final record ClientQueryParams<T>(@Nullable CompositeSnapshot snapshot,
return QueryParamsBuilder
.builder()
.query(query())
.sort(sort != null ? sort.getQuerySort() : new NoSort())
.sort(sort != null ? sort.querySort() : new NoSort())
.minCompetitiveScore(Nullablefloat.ofNullable(minCompetitiveScore()))
.offset(offset())
.limit(limit())

View File

@ -1,12 +1,11 @@
package it.cavallium.dbengine;
import it.cavallium.dbengine.client.MultiSort;
import it.cavallium.dbengine.client.SearchResultKey;
import it.cavallium.dbengine.client.Sort;
import it.cavallium.dbengine.client.query.BasicType;
record ExpectedQueryType(boolean shard, boolean sorted, boolean sortedByScore, boolean complete, boolean onlyCount) {
public ExpectedQueryType(boolean shard, MultiSort<SearchResultKey<String>> multiSort, boolean complete, boolean onlyCount) {
this(shard, multiSort.isSorted(), multiSort.getQuerySort().getBasicType$() == BasicType.ScoreSort, complete, onlyCount);
public ExpectedQueryType(boolean shard, Sort multiSort, boolean complete, boolean onlyCount) {
this(shard, multiSort.isSorted(), multiSort.querySort().getBasicType$() == BasicType.ScoreSort, complete, onlyCount);
}
}

View File

@ -10,8 +10,7 @@ import static org.junit.jupiter.api.Assertions.fail;
import it.cavallium.dbengine.DbTestUtils.TempDb;
import it.cavallium.dbengine.DbTestUtils.TestAllocator;
import it.cavallium.dbengine.client.LuceneIndex;
import it.cavallium.dbengine.client.MultiSort;
import it.cavallium.dbengine.client.SearchResultKey;
import it.cavallium.dbengine.client.Sort;
import it.cavallium.dbengine.client.query.current.data.MatchAllDocsQuery;
import it.cavallium.dbengine.database.LLLuceneIndex;
import it.cavallium.dbengine.database.LLScoreMode;
@ -23,10 +22,7 @@ import it.cavallium.dbengine.lucene.searcher.LocalSearcher;
import it.cavallium.dbengine.lucene.searcher.MultiSearcher;
import it.cavallium.dbengine.lucene.searcher.UnsortedUnscoredSimpleMultiSearcher;
import java.io.IOException;
import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Stream;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.AfterAll;
@ -81,12 +77,12 @@ public class TestLuceneIndex {
LLScoreMode.COMPLETE_NO_SCORES,
LLScoreMode.COMPLETE
);
private static final Flux<MultiSort<SearchResultKey<String>>> multiSort = Flux.just(MultiSort.topScore(),
MultiSort.randomSortField(),
MultiSort.noSort(),
MultiSort.docSort(),
MultiSort.numericSort("longsort", false),
MultiSort.numericSort("longsort", true)
private static final Flux<Sort> multiSort = Flux.just(Sort.score(),
Sort.random(),
Sort.no(),
Sort.doc(),
Sort.numeric("longsort", false),
Sort.numeric("longsort", true)
);
public static Stream<Arguments> provideQueryArgumentsScoreMode() {

View File

@ -11,28 +11,22 @@ import io.net5.buffer.PooledByteBufAllocator;
import it.cavallium.dbengine.DbTestUtils.TempDb;
import it.cavallium.dbengine.DbTestUtils.TestAllocator;
import it.cavallium.dbengine.client.LuceneIndex;
import it.cavallium.dbengine.client.MultiSort;
import it.cavallium.dbengine.client.Sort;
import it.cavallium.dbengine.client.SearchResultKey;
import it.cavallium.dbengine.client.SearchResultKeys;
import it.cavallium.dbengine.client.query.BasicType;
import it.cavallium.dbengine.client.query.ClientQueryParams;
import it.cavallium.dbengine.client.query.ClientQueryParamsBuilder;
import it.cavallium.dbengine.client.query.QueryParser;
import it.cavallium.dbengine.client.query.current.data.BooleanQuery;
import it.cavallium.dbengine.client.query.current.data.BooleanQueryPart;
import it.cavallium.dbengine.client.query.current.data.BoostQuery;
import it.cavallium.dbengine.client.query.current.data.MatchAllDocsQuery;
import it.cavallium.dbengine.client.query.current.data.MatchNoDocsQuery;
import it.cavallium.dbengine.client.query.current.data.NoSort;
import it.cavallium.dbengine.client.query.current.data.OccurMust;
import it.cavallium.dbengine.client.query.current.data.OccurShould;
import it.cavallium.dbengine.client.query.current.data.ScoreSort;
import it.cavallium.dbengine.client.query.current.data.Term;
import it.cavallium.dbengine.client.query.current.data.TermQuery;
import it.cavallium.dbengine.client.query.current.data.TotalHitsCount;
import it.cavallium.dbengine.database.LLLuceneIndex;
import it.cavallium.dbengine.database.LLScoreMode;
import it.cavallium.dbengine.database.LLUtils;
import it.cavallium.dbengine.database.disk.LLTempLMDBEnv;
import it.cavallium.dbengine.lucene.searcher.AdaptiveLocalSearcher;
import it.cavallium.dbengine.lucene.searcher.AdaptiveMultiSearcher;
@ -47,17 +41,12 @@ import it.cavallium.dbengine.lucene.searcher.UnsortedUnscoredSimpleMultiSearcher
import it.cavallium.dbengine.lucene.searcher.UnsortedScoredFullMultiSearcher;
import it.cavallium.dbengine.lucene.searcher.UnsortedUnscoredStreamingMultiSearcher;
import java.io.IOException;
import java.time.Duration;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.function.FailableConsumer;
@ -149,16 +138,16 @@ public class TestLuceneSearches {
}
private static final Flux<Boolean> multi = Flux.just(false, true);
private static final Flux<MultiSort<SearchResultKey<String>>> multiSort = Flux.just(
MultiSort.topScore(),
private static final Flux<Sort> multiSort = Flux.just(
Sort.score(),
//todo: fix random sort field
//MultiSort.randomSortField(),
MultiSort.noSort(),
MultiSort.docSort(),
MultiSort.numericSort("longsort", false),
MultiSort.numericSort("longsort", true),
MultiSort.numericSort("intsort", false),
MultiSort.numericSort("intsort", true)
//Sort.randomSortField(),
Sort.no(),
Sort.doc(),
Sort.numeric("longsort", false),
Sort.numeric("longsort", true),
Sort.numeric("intsort", false),
Sort.numeric("intsort", true)
);
private static Flux<LocalSearcher> getSearchers(ExpectedQueryType info) {
@ -300,7 +289,7 @@ public class TestLuceneSearches {
@ParameterizedTest
@MethodSource("provideQueryArgumentsScoreModeAndSort")
public void testSearchNoDocs(boolean shards, MultiSort<SearchResultKey<String>> multiSort) throws Throwable {
public void testSearchNoDocs(boolean shards, Sort multiSort) throws Throwable {
var queryBuilder = ClientQueryParams
.<SearchResultKey<String>>builder()
.query(new MatchNoDocsQuery())
@ -314,7 +303,7 @@ public class TestLuceneSearches {
@ParameterizedTest
@MethodSource("provideQueryArgumentsScoreModeAndSort")
public void testSearchAllDocs(boolean shards, MultiSort<SearchResultKey<String>> multiSort) throws Throwable {
public void testSearchAllDocs(boolean shards, Sort multiSort) throws Throwable {
var queryBuilder = ClientQueryParams
.<SearchResultKey<String>>builder()
.query(new MatchAllDocsQuery())
@ -328,7 +317,7 @@ public class TestLuceneSearches {
@ParameterizedTest
@MethodSource("provideQueryArgumentsScoreModeAndSort")
public void testSearchAdvancedText(boolean shards, MultiSort<SearchResultKey<String>> multiSort) throws Throwable {
public void testSearchAdvancedText(boolean shards, Sort multiSort) throws Throwable {
var queryBuilder = ClientQueryParams
.<SearchResultKey<String>>builder()
.query(new BooleanQuery(List.of(