thecoop commented on code in PR #17018:
URL: https://github.com/apache/lucene/pull/17018#discussion_r4613510084


##########
lucene/queries/src/java/org/apache/lucene/queries/spans/SpanWeight.java:
##########
@@ -111,14 +111,40 @@ private Similarity.SimScorer buildSimWeight(
             searcher.termStats(entry.getKey(), ts.docFreq(), 
ts.totalTermFreq());
       }
     }
-    FieldStats fieldStats = searcher.fieldStats(query.getField());
     if (termUpTo > 0) {
-      return similarity.scorer(boost, fieldStats, 
ArrayUtil.copyOfSubArray(termStats, 0, termUpTo));
+      termStats = ArrayUtil.copyOfSubArray(termStats, 0, termUpTo);
+      FieldStats fieldStats = searcher.fieldStats(query.getField());
+      if (fieldStats == null) {
+        // the query field and term fields can be different (see 
FieldMaskingSpanQuery);
+        // if the query field has no indexed documents then the stats will be 
missing
+        // So calculate some stats that are roughly right from the term stats
+        fieldStats = fieldStatsFromTerms(query.getField(), searcher, 
termStats);
+      }
+      return similarity.scorer(boost, fieldStats, termStats);
     } else {
       return null; // no terms at all exist, we won't use similarity
     }
   }
 
+  /**
+   * The query field can differ from the fields of its terms (see {@link 
FieldMaskingSpanQuery}), so
+   * the query field can have no indexed documents when the terms do. In that 
case, derive pseudo
+   * field statistics from the term statistics.
+   */
+  private static FieldStats fieldStatsFromTerms(

Review Comment:
   Yup - I've modified it to use field stats first, where available



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to