MarcusSorealheis commented on code in PR #12208: URL: https://github.com/apache/lucene/pull/12208#discussion_r1157660036
########## lucene/sandbox/src/java/org/apache/lucene/sandbox/search/TermAutomatonQuery.java: ########## @@ -442,8 +442,44 @@ public boolean isCacheable(LeafReaderContext ctx) { @Override public Explanation explain(LeafReaderContext context, int doc) throws IOException { - // TODO - return null; + Scorer scorer = scorer(context); + if (scorer == null) { + return Explanation.noMatch("No matching terms in the document"); + } + + int advancedDoc = scorer.iterator().advance(doc); + if (advancedDoc != doc) { + return Explanation.noMatch("No matching terms in the document"); + } + + float score = scorer.score(); + LeafSimScorer leafSimScorer = ((TermAutomatonScorer) scorer).getLeafSimScorer(); + EnumAndScorer[] enums = ((TermAutomatonScorer) scorer).getEnums(); + + List<Explanation> termExplanations = new ArrayList<>(); + for (EnumAndScorer enumAndScorer : enums) { + if (enumAndScorer != null) { + PostingsEnum postingsEnum = enumAndScorer.posEnum; + if (postingsEnum.docID() == doc) { + float termScore = leafSimScorer.score(doc, postingsEnum.freq()); + termExplanations.add( + Explanation.match( + postingsEnum.freq(), + "term frequency in the document", + Explanation.match( + termScore, + "score for term: " + idToTerm.get(enumAndScorer.termID).utf8ToString()))); + } + } + } + + if (termExplanations.isEmpty()) { + return Explanation.noMatch("No matching terms in the document"); + } + + Explanation freqExplanation = + Explanation.match(score, "TermAutomatonQuery, product of:", termExplanations); Review Comment: Yep, this is is an error. Good catch. `termExplanations.add` I believe that maybe my thinking when I wrote this was born out of many operations in the explanation. The score is definitely a sum of products, differences, quotients and more. I'll fix in the next commit. -- 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