From fd565c80b26ee43fe905f53f18f696e33f112238 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Sat, 16 Oct 2021 01:35:09 +0200 Subject: [PATCH] Fix assertions --- .../lucene/searcher/OfficialSearcher.java | 8 ++-- .../lucene/searcher/PagedLocalSearcher.java | 8 ++-- .../searcher/ScoredPagedMultiSearcher.java | 8 ++-- .../SortedScoredFullMultiSearcher.java | 7 ++-- .../UnsortedScoredFullMultiSearcher.java | 7 ++-- ...nsortedUnscoredStreamingMultiSearcher.java | 7 ++-- .../dbengine/TestLuceneSearches.java | 38 +++++++++++++------ 7 files changed, 54 insertions(+), 29 deletions(-) diff --git a/src/main/java/it/cavallium/dbengine/lucene/searcher/OfficialSearcher.java b/src/main/java/it/cavallium/dbengine/lucene/searcher/OfficialSearcher.java index 1ade91a..9527ddc 100644 --- a/src/main/java/it/cavallium/dbengine/lucene/searcher/OfficialSearcher.java +++ b/src/main/java/it/cavallium/dbengine/lucene/searcher/OfficialSearcher.java @@ -15,6 +15,7 @@ import java.io.IOException; import java.util.List; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.ScoreMode; import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.TopDocsCollector; import org.apache.lucene.search.TopFieldCollector; @@ -82,9 +83,10 @@ public class OfficialSearcher implements MultiSearcher, Closeable { var collector = sharedManager.newCollector(); assert queryParams.complete() == collector.scoreMode().isExhaustive(); - queryParams.getScoreModeOptional().ifPresent(scoreMode -> { - assert scoreMode == collector.scoreMode(); - }); + assert queryParams + .getScoreModeOptional() + .map(scoreMode -> scoreMode == collector.scoreMode()) + .orElse(true); shard.search(queryParams.query(), collector); return collector; diff --git a/src/main/java/it/cavallium/dbengine/lucene/searcher/PagedLocalSearcher.java b/src/main/java/it/cavallium/dbengine/lucene/searcher/PagedLocalSearcher.java index 89cc7a3..6e2ccdf 100644 --- a/src/main/java/it/cavallium/dbengine/lucene/searcher/PagedLocalSearcher.java +++ b/src/main/java/it/cavallium/dbengine/lucene/searcher/PagedLocalSearcher.java @@ -18,6 +18,7 @@ import java.util.Objects; import org.apache.lucene.search.Collector; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.ScoreMode; import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.TopDocsCollector; import org.apache.lucene.search.TopFieldCollector; @@ -188,9 +189,10 @@ public class PagedLocalSearcher implements LocalSearcher { currentPageLimit, s.last(), queryParams.getTotalHitsThresholdInt(), allowPagination, queryParams.needsScores()); assert queryParams.complete() == collector.scoreMode().isExhaustive(); - queryParams.getScoreModeOptional().ifPresent(scoreMode -> { - assert scoreMode == collector.scoreMode(); - }); + assert currentPageLimit < Integer.MAX_VALUE || queryParams + .getScoreModeOptional() + .map(scoreMode -> scoreMode == collector.scoreMode()) + .orElse(true); indexSearchers.get(0).search(queryParams.query(), collector); if (resultsOffset > 0) { diff --git a/src/main/java/it/cavallium/dbengine/lucene/searcher/ScoredPagedMultiSearcher.java b/src/main/java/it/cavallium/dbengine/lucene/searcher/ScoredPagedMultiSearcher.java index 0d6f8ac..002d90d 100644 --- a/src/main/java/it/cavallium/dbengine/lucene/searcher/ScoredPagedMultiSearcher.java +++ b/src/main/java/it/cavallium/dbengine/lucene/searcher/ScoredPagedMultiSearcher.java @@ -15,6 +15,7 @@ import java.util.List; import java.util.concurrent.atomic.AtomicReference; import org.apache.lucene.search.FieldDoc; import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.ScoreMode; import org.apache.lucene.search.Sort; import org.jetbrains.annotations.Nullable; import org.warp.commonutils.log.Logger; @@ -188,9 +189,10 @@ public class ScoredPagedMultiSearcher implements MultiSearcher { var collector = sharedManager.newCollector(); assert queryParams.complete() == collector.scoreMode().isExhaustive(); - queryParams.getScoreModeOptional().ifPresent(scoreMode -> { - assert scoreMode == collector.scoreMode(); - }); + assert pageLimits.getPageLimit(s.pageIndex()) < Integer.MAX_VALUE || queryParams + .getScoreModeOptional() + .map(scoreMode -> scoreMode == collector.scoreMode()) + .orElse(true); shard.search(queryParams.query(), collector); return collector; diff --git a/src/main/java/it/cavallium/dbengine/lucene/searcher/SortedScoredFullMultiSearcher.java b/src/main/java/it/cavallium/dbengine/lucene/searcher/SortedScoredFullMultiSearcher.java index d9b0c48..c894916 100644 --- a/src/main/java/it/cavallium/dbengine/lucene/searcher/SortedScoredFullMultiSearcher.java +++ b/src/main/java/it/cavallium/dbengine/lucene/searcher/SortedScoredFullMultiSearcher.java @@ -73,9 +73,10 @@ public class SortedScoredFullMultiSearcher implements MultiSearcher, Closeable { var collector = sharedManager.newCollector(); assert queryParams.complete() == collector.scoreMode().isExhaustive(); - queryParams.getScoreModeOptional().ifPresent(scoreMode -> { - assert scoreMode == collector.scoreMode(); - }); + assert queryParams + .getScoreModeOptional() + .map(scoreMode -> scoreMode == collector.scoreMode()) + .orElse(true); shard.search(queryParams.query(), collector); return collector; diff --git a/src/main/java/it/cavallium/dbengine/lucene/searcher/UnsortedScoredFullMultiSearcher.java b/src/main/java/it/cavallium/dbengine/lucene/searcher/UnsortedScoredFullMultiSearcher.java index 0ca8e66..23a5c10 100644 --- a/src/main/java/it/cavallium/dbengine/lucene/searcher/UnsortedScoredFullMultiSearcher.java +++ b/src/main/java/it/cavallium/dbengine/lucene/searcher/UnsortedScoredFullMultiSearcher.java @@ -79,9 +79,10 @@ public class UnsortedScoredFullMultiSearcher implements MultiSearcher, Closeable var collector = sharedManager.newCollector(); assert queryParams.complete() == collector.scoreMode().isExhaustive(); - queryParams.getScoreModeOptional().ifPresent(scoreMode -> { - assert scoreMode == collector.scoreMode(); - }); + assert queryParams + .getScoreModeOptional() + .map(scoreMode -> scoreMode == collector.scoreMode()) + .orElse(true); shard.search(queryParams.query(), collector); return collector; diff --git a/src/main/java/it/cavallium/dbengine/lucene/searcher/UnsortedUnscoredStreamingMultiSearcher.java b/src/main/java/it/cavallium/dbengine/lucene/searcher/UnsortedUnscoredStreamingMultiSearcher.java index e216cac..cdd78c3 100644 --- a/src/main/java/it/cavallium/dbengine/lucene/searcher/UnsortedUnscoredStreamingMultiSearcher.java +++ b/src/main/java/it/cavallium/dbengine/lucene/searcher/UnsortedUnscoredStreamingMultiSearcher.java @@ -76,9 +76,10 @@ public class UnsortedUnscoredStreamingMultiSearcher implements MultiSearcher { try { var collector = cm.newCollector(); assert queryParams.complete() == collector.scoreMode().isExhaustive(); - queryParams.getScoreModeOptional().ifPresent(scoreMode -> { - assert scoreMode == collector.scoreMode(); - }); + assert queryParams + .getScoreModeOptional() + .map(scoreMode -> scoreMode == collector.scoreMode()) + .orElse(true); collector.setShardIndex(shardIndex); diff --git a/src/test/java/it/cavallium/dbengine/TestLuceneSearches.java b/src/test/java/it/cavallium/dbengine/TestLuceneSearches.java index 45a36d1..6f48517 100644 --- a/src/test/java/it/cavallium/dbengine/TestLuceneSearches.java +++ b/src/test/java/it/cavallium/dbengine/TestLuceneSearches.java @@ -18,10 +18,17 @@ 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; @@ -321,18 +328,27 @@ public class TestLuceneSearches { testSearch(queryBuilder, expectedQueryType); } + @ParameterizedTest + @MethodSource("provideQueryArgumentsScoreModeAndSort") + public void testSearchAdvancedText(boolean shards, MultiSort> multiSort) throws Throwable { + var queryBuilder = ClientQueryParams + .>builder() + .query(new BooleanQuery(List.of( + new BooleanQueryPart(new BoostQuery(new TermQuery(new Term("text", "hello")), 3), new OccurShould()), + new BooleanQueryPart(new TermQuery(new Term("text", "world")), new OccurShould()), + new BooleanQueryPart(new BoostQuery(new TermQuery(new Term("text", "hello")), 2), new OccurShould()), + new BooleanQueryPart(new BoostQuery(new TermQuery(new Term("text", "hello")), 100), new OccurShould()), + new BooleanQueryPart(new TermQuery(new Term("text", "hello")), new OccurMust()) + ), 1)) + .snapshot(null) + .complete(true) + .sort(multiSort); + + ExpectedQueryType expectedQueryType = new ExpectedQueryType(shards, multiSort, true, false); + testSearch(queryBuilder, expectedQueryType); + } + private void assertResults(List expectedKeys, List resultKeys, boolean sorted, boolean sortedByScore) { - - if (sortedByScore) { - float lastScore = Float.NEGATIVE_INFINITY; - for (Scored resultKey : resultKeys) { - if (!Float.isNaN(resultKey.score())) { - Assertions.assertTrue(resultKey.score() >= lastScore); - lastScore = resultKey.score(); - } - } - } - if (sortedByScore) { Assertions.assertEquals(expectedKeys, resultKeys); } else if (sorted) {