john-wagster commented on code in PR #17018:
URL: https://github.com/apache/lucene/pull/17018#discussion_r4569897327
##########
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:
I was trying to think through what this is actually doing and it will
probably miscount sometimes I think?
So like:
doc0: james
doc1: james, sally
doc2: greta, sally, james
doc3: lisa, bob
doc4: sally, linda, bubba
For `SpanOr(mask(first:james), mask(first:bob))` masked to `missing` the
stats come out as:
```
term-derived (PR) real `first` stats
docCount 3 5
sumDocFreq 4 11
sumTotalTermFreq 4 11
avgdl 1.33 2.2
BM25 idf (james, df=3) ~0.13 ~0.54
BM25 idf (bob, df=1) ~0.98 ~1.39
```
Since you have `termStates` in `buildSimWeight` is it possible to check the
`searcher.fieldStats(term.field())` for the stats first and then fall back to
what this function is doing. I think that would be more accurate? I'm still
trying to wrap may head around it a bit.
🤖 thinks this is a reasonable version of what that function would look like:
```
/**
* The query field can differ from the fields of its terms (see {@link
FieldMaskingSpanQuery}), so
* the query field may have no indexed documents even though the terms do.
In that case, use the
* statistics of the fields the terms come from, combined if there is more
than one.
*/
private static FieldStats fieldStatsFromTermFields(
String field, IndexSearcher searcher, Set<Term> terms) throws
IOException {
Set<String> termFields = new HashSet<>();
for (Term term : terms) {
termFields.add(term.field());
}
long maxDoc = 0;
long docCount = 0;
long sumTotalTermFreq = 0;
long sumDocFreq = 0;
for (String termField : termFields) {
FieldStats fs = searcher.fieldStats(termField);
if (fs != null) {
maxDoc = Math.max(maxDoc, fs.maxDoc());
docCount = Math.max(docCount, fs.docCount());
sumTotalTermFreq = Math.max(sumTotalTermFreq, fs.sumTotalTermFreq());
sumDocFreq = Math.max(sumDocFreq, fs.sumDocFreq());
}
}
// at least one term has docFreq > 0, so at least one of its fields has
stats
assert docCount > 0;
return new FieldStats(field, maxDoc, docCount, sumTotalTermFreq,
sumDocFreq);
}
```
--
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]