sgup432 commented on code in PR #12383:
URL: https://github.com/apache/lucene/pull/12383#discussion_r1246890555


##########
lucene/core/src/test/org/apache/lucene/search/TestTermQuery.java:
##########
@@ -164,6 +170,57 @@ public void testGetTermStates() throws Exception {
     IOUtils.close(reader, w, dir);
   }
 
+  public void testWithWithDifferentScoreModes() throws Exception {
+    Directory dir = newDirectory();
+    RandomIndexWriter w =
+        new RandomIndexWriter(
+            random(), dir, 
newIndexWriterConfig().setMergePolicy(NoMergePolicy.INSTANCE));
+    // segment that contains the term
+    Document doc = new Document();
+    doc.add(new StringField("foo", "bar", Store.NO));
+    w.addDocument(doc);
+    w.getReader().close();
+    DirectoryReader reader = w.getReader();
+    IndexSearcher searcher = new IndexSearcher(reader);
+    Similarity existingSimilarity = searcher.getSimilarity();
+
+    for (ScoreMode scoreMode : ScoreMode.values()) {
+      final AtomicReference<ScoreMode> scoreModeInWeight = new 
AtomicReference<ScoreMode>();
+      final AtomicBoolean scorerCalled = new AtomicBoolean();
+      searcher.setSimilarity(
+          new Similarity() { // Wrapping existing similarity for testing
+            @Override
+            public long computeNorm(FieldInvertState state) {
+              return getDefaultSimilarity().computeNorm(state);

Review Comment:
   Yes. Missed it. Corrected.



##########
lucene/core/src/test/org/apache/lucene/search/TestTermQuery.java:
##########
@@ -164,6 +170,57 @@ public void testGetTermStates() throws Exception {
     IOUtils.close(reader, w, dir);
   }
 
+  public void testWithWithDifferentScoreModes() throws Exception {
+    Directory dir = newDirectory();
+    RandomIndexWriter w =
+        new RandomIndexWriter(
+            random(), dir, 
newIndexWriterConfig().setMergePolicy(NoMergePolicy.INSTANCE));
+    // segment that contains the term
+    Document doc = new Document();
+    doc.add(new StringField("foo", "bar", Store.NO));
+    w.addDocument(doc);
+    w.getReader().close();
+    DirectoryReader reader = w.getReader();
+    IndexSearcher searcher = new IndexSearcher(reader);
+    Similarity existingSimilarity = searcher.getSimilarity();
+
+    for (ScoreMode scoreMode : ScoreMode.values()) {
+      final AtomicReference<ScoreMode> scoreModeInWeight = new 
AtomicReference<ScoreMode>();
+      final AtomicBoolean scorerCalled = new AtomicBoolean();
+      searcher.setSimilarity(
+          new Similarity() { // Wrapping existing similarity for testing
+            @Override
+            public long computeNorm(FieldInvertState state) {
+              return getDefaultSimilarity().computeNorm(state);
+            }
+
+            @Override
+            public SimScorer scorer(
+                float boost, CollectionStatistics collectionStats, 
TermStatistics... termStats) {
+              scorerCalled.set(true);
+              return existingSimilarity.scorer(boost, collectionStats, 
termStats);
+            }
+          });
+      TermQuery termQuery =
+          new TermQuery(new Term("foo", "bar")) {
+            @Override
+            public Weight createWeight(IndexSearcher searcher, ScoreMode 
scoreMode, float boost)
+                throws IOException {
+              scoreModeInWeight.set(scoreMode);
+              return super.createWeight(searcher, scoreMode, boost);
+            }
+          };
+      termQuery.createWeight(searcher, scoreMode, 1f);
+      assertEquals(scoreMode, scoreModeInWeight.get());
+      if (scoreMode.needsScores()) {
+        assertTrue(scorerCalled.get());
+      } else {
+        assertFalse(scorerCalled.get());
+      }

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to