You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2023/01/10 15:49:55 UTC

[GitHub] [lucene] benwtrent commented on a diff in pull request #12072: Fix exponential runtime for Boolean#rewrite

benwtrent commented on code in PR #12072:
URL: https://github.com/apache/lucene/pull/12072#discussion_r1065950478


##########
lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java:
##########
@@ -203,9 +203,17 @@ BooleanQuery rewriteNoScoring(IndexSearcher indexSearcher) throws IOException {
 
     for (BooleanClause clause : clauses) {
       Query query = clause.getQuery();
-      Query rewritten = new ConstantScoreQuery(query).rewrite(indexSearcher);
-      if (rewritten instanceof ConstantScoreQuery) {
-        rewritten = ((ConstantScoreQuery) rewritten).getQuery();
+      Query rewritten;
+      // If the clause query is a boolean query, we shouldn't call ConstantScoreQuery#rewrite as
+      // this causes
+      // exponential growth of runtime.
+      if (query instanceof BooleanQuery booleanQuery) {
+        rewritten = booleanQuery.rewriteNoScoring(indexSearcher);
+      } else {
+        rewritten = new ConstantScoreQuery(query).rewrite(indexSearcher);
+        if (rewritten instanceof ConstantScoreQuery constantScoreQuery) {
+          rewritten = constantScoreQuery.getQuery();
+        }

Review Comment:
   I have been trying to come up with ideas where calling `new ConstantScoreQuery(query).rewrite(indexSearcher)` when `query instanceof BooleanQuery` is absolutely necessary. But, it seems like it isn't. `ConstantScoreQuery#rewrite` already calls the underlying `query.rewrite` BEFORE attempting the `rewriteNoScoring`.



-- 
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-unsubscribe@lucene.apache.org

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


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