You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ds...@apache.org on 2025/04/09 14:35:26 UTC
(lucene) branch main updated: Tests: TestLongNormValueSource, AssertingScorable (#14448)
This is an automated email from the ASF dual-hosted git repository.
dsmiley pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git
The following commit(s) were added to refs/heads/main by this push:
new a74d85c689c Tests: TestLongNormValueSource, AssertingScorable (#14448)
a74d85c689c is described below
commit a74d85c689c6c4653efa2daec54d91d864333b30
Author: David Smiley <ds...@salesforce.com>
AuthorDate: Wed Apr 9 10:35:20 2025 -0400
Tests: TestLongNormValueSource, AssertingScorable (#14448)
TestLongNormValueSource: improve
AssortingScorable: don't wrap needlessly
---
.../queries/function/TestLongNormValueSource.java | 101 +++++++--------------
.../lucene/tests/search/AssertingScorable.java | 2 +-
2 files changed, 36 insertions(+), 67 deletions(-)
diff --git a/lucene/queries/src/test/org/apache/lucene/queries/function/TestLongNormValueSource.java b/lucene/queries/src/test/org/apache/lucene/queries/function/TestLongNormValueSource.java
index 0e8a78204be..cd2b983e077 100644
--- a/lucene/queries/src/test/org/apache/lucene/queries/function/TestLongNormValueSource.java
+++ b/lucene/queries/src/test/org/apache/lucene/queries/function/TestLongNormValueSource.java
@@ -16,99 +16,68 @@
*/
package org.apache.lucene.queries.function;
-import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field;
+import org.apache.lucene.document.Field.Store;
+import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
-import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queries.function.valuesource.NormValueSource;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
-import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.similarities.ClassicSimilarity;
import org.apache.lucene.search.similarities.Similarity;
-import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.analysis.MockAnalyzer;
import org.apache.lucene.tests.index.RandomIndexWriter;
import org.apache.lucene.tests.search.CheckHits;
import org.apache.lucene.tests.util.LuceneTestCase;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
public class TestLongNormValueSource extends LuceneTestCase {
- static Directory dir;
- static IndexReader reader;
- static IndexSearcher searcher;
- static Analyzer analyzer;
-
- private static final Similarity sim = new ClassicSimilarity();
-
- @BeforeClass
- public static void beforeClass() throws Exception {
- dir = newDirectory();
- analyzer = new MockAnalyzer(random());
+ public void testNorm() throws Exception {
+ Similarity sim = new ClassicSimilarity();
+ final var field = "text";
+ var analyzer = new MockAnalyzer(random());
+ var dir = newDirectory();
IndexWriterConfig iwConfig = newIndexWriterConfig(analyzer);
iwConfig.setMergePolicy(newLogMergePolicy());
iwConfig.setSimilarity(sim);
- RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwConfig);
- Document doc = new Document();
- doc.add(new TextField("text", "this is a test test test", Field.Store.NO));
- iw.addDocument(doc);
+ try (RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwConfig)) {
- doc = new Document();
- doc.add(new TextField("text", "second test", Field.Store.NO));
- iw.addDocument(doc);
+ // the leading number is the number of terms / positions for that value
+ final var store = Store.YES;
+ Document doc = new Document();
+ doc.add(new StringField("id", "6", store));
+ doc.add(new TextField(field, "this is a test test test", store));
+ iw.addDocument(doc);
- reader = iw.getReader();
- searcher = newSearcher(reader);
- iw.close();
- }
+ doc = new Document();
+ doc.add(new StringField("id", "2", store));
+ doc.add(new TextField(field, "second test", store));
+ iw.addDocument(doc);
- @AfterClass
- public static void afterClass() throws Exception {
- searcher = null;
- reader.close();
- reader = null;
- dir.close();
- dir = null;
- analyzer.close();
- analyzer = null;
- }
+ IndexSearcher searcher;
+ try (var reader = iw.getReader()) {
+ searcher = newSearcher(reader);
+ searcher.setSimilarity(sim);
- public void testNorm() throws Exception {
- Similarity saved = searcher.getSimilarity();
- try {
- // no norm field (so agnostic to indexed similarity)
- searcher.setSimilarity(sim);
- assertHits(new FunctionQuery(new NormValueSource("text")), new float[] {0f, 0f});
+ Query q = new FunctionQuery(new NormValueSource(field));
+ TopDocs docs = searcher.search(q, searcher.getIndexReader().numDocs(), new Sort(), true);
+ final var expectedDocIds = new int[] {1, 0};
+ final var expectedScoreDocs =
+ new ScoreDoc[] {new ScoreDoc(1, 0.70710677f), new ScoreDoc(0, 0.4082483f)};
+ CheckHits.checkHitsQuery(q, expectedScoreDocs, docs.scoreDocs, expectedDocIds);
+ CheckHits.checkExplanations(q, "", searcher);
+ final var storedFields = searcher.getIndexReader().storedFields();
+ assertEquals("2", storedFields.document(expectedDocIds[0]).get("id"));
+ assertEquals("6", storedFields.document(expectedDocIds[1]).get("id"));
+ }
} finally {
- searcher.setSimilarity(saved);
+ dir.close();
+ analyzer.close();
}
}
-
- void assertHits(Query q, float[] scores) throws Exception {
- ScoreDoc[] expected = new ScoreDoc[scores.length];
- int[] expectedDocs = new int[scores.length];
- for (int i = 0; i < expected.length; i++) {
- expectedDocs[i] = i;
- expected[i] = new ScoreDoc(i, scores[i]);
- }
- TopDocs docs = searcher.search(q, 2, new Sort(new SortField("id", SortField.Type.STRING)));
-
- /*
- for (int i=0;i<docs.scoreDocs.length;i++) {
- System.out.println(searcher.explain(q, docs.scoreDocs[i].doc));
- }
- */
-
- CheckHits.checkHits(random(), q, "", searcher, expectedDocs);
- CheckHits.checkHitsQuery(q, expected, docs.scoreDocs, expectedDocs);
- CheckHits.checkExplanations(q, "", searcher);
- }
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorable.java b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorable.java
index 2f3d604ad25..7acdd2a224a 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorable.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorable.java
@@ -44,7 +44,7 @@ public class AssertingScorable extends FilterScorable {
}
public static Scorable wrap(Scorable in) {
- if (in instanceof AssertingScorable) {
+ if (in instanceof WrappedScorer || in instanceof AssertingScorable) {
return in;
}
// If `in` is Scorer, we need to wrap it as a Scorer instead of Scorable because