benwtrent commented on code in PR #13994: URL: https://github.com/apache/lucene/pull/13994#discussion_r1842353256
########## lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java: ########## @@ -558,6 +557,41 @@ public Query rewrite(IndexSearcher indexSearcher) throws IOException { } } + // Inline required / prohibited clauses. This helps run filtered conjunctive queries more + // efficiently by providing all clauses to the block-max AND scorer. + { + BooleanQuery.Builder builder = new BooleanQuery.Builder(); + builder.setMinimumNumberShouldMatch(minimumNumberShouldMatch); + boolean actuallyRewritten = false; + for (BooleanClause clause : clauses) { + if (clause.isRequired() && clause.query() instanceof BooleanQuery innerQuery) { + if (innerQuery.getMinimumNumberShouldMatch() == 0 + && innerQuery.getClauses(Occur.SHOULD).isEmpty()) { + actuallyRewritten = true; + for (BooleanClause innerClause : innerQuery) { + Occur occur = innerClause.occur(); + if (occur == Occur.FILTER + || occur == Occur.MUST_NOT + || clause.occur() == Occur.MUST) { + builder.add(innerClause); + } else { + assert clause.occur() == Occur.FILTER && occur == Occur.MUST; + // In this case we need to change the occur of the inner query from MUST to FILTER. + builder.add(innerClause.query(), Occur.FILTER); + } + } + } else { + builder.add(clause); + } + } else { + builder.add(clause); + } + } Review Comment: Holy smokes I had to read this like 5 times to fully grok what it is doing. Could you name `occur` to `innerClauseOccur` and change `clause` to `outerClause`? -- 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