From 1dc206413207898ead4a0914e46f68db4d68a18e Mon Sep 17 00:00:00 2001
From: patsonluk <patsonluk@users.noreply.github.com>
Date: Thu, 7 Dec 2023 12:29:49 -0800
Subject: [PATCH] SAI-4737: System prop `solr.MaxBasicQueriesOverride` for
 surround query (#172)

* Added support of system prop solr.absoluteMaxBasicQueries for surround query

* Moved param lookup logic to static, as we should only need to do and log it once.

And solr.absoluteMaxBasicQueries should only kick in if the original maxBasicQueries is greater

* ./gradlew tidy

* Changed solr.absoluteMaxBasicQueries to solr.maxBasicQueriesOverride, this requires less deployment but is a more temporary implementation.

* Renamed variable

* ./gradlew tidy
---
 .../solr/search/SurroundQParserPlugin.java    | 34 +++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/solr/core/src/java/org/apache/solr/search/SurroundQParserPlugin.java b/solr/core/src/java/org/apache/solr/search/SurroundQParserPlugin.java
index 204477353e0..182e025e5c7 100644
--- a/solr/core/src/java/org/apache/solr/search/SurroundQParserPlugin.java
+++ b/solr/core/src/java/org/apache/solr/search/SurroundQParserPlugin.java
@@ -40,6 +40,35 @@
 public class SurroundQParserPlugin extends QParserPlugin {
   private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
   public static final String NAME = "surround";
+  private static final String MAX_BASIC_QUERIES_SYSTEM_PROP = "solr.maxBasicQueriesOverride";
+  private static final int MAX_BASIC_QUERIES_OVERRIDE = readMaxBasicQueriesOverride();
+
+  private static int readMaxBasicQueriesOverride() {
+    String maxBasicQueriesSystemProp = System.getProperty(MAX_BASIC_QUERIES_SYSTEM_PROP);
+    if (maxBasicQueriesSystemProp != null) {
+      try {
+        int maxBasicQueriesOverride = Integer.parseInt(maxBasicQueriesSystemProp);
+        if (maxBasicQueriesOverride > 0) {
+          log.info(
+              "maxBasicQueries with system property {} with value {}",
+              MAX_BASIC_QUERIES_SYSTEM_PROP,
+              maxBasicQueriesSystemProp);
+          return maxBasicQueriesOverride;
+        } else {
+          log.info(
+              "Ignoring system property {} value {} since it is non-positive",
+              MAX_BASIC_QUERIES_SYSTEM_PROP,
+              maxBasicQueriesSystemProp);
+        }
+      } catch (NumberFormatException e) {
+        log.warn(
+            "Invalid system property {} value {}",
+            MAX_BASIC_QUERIES_SYSTEM_PROP,
+            maxBasicQueriesSystemProp);
+      }
+    }
+    return -1; // -1 indicates no max basic queries override
+  }
 
   @Override
   public QParser createParser(
@@ -77,6 +106,11 @@ public Query parse() throws SyntaxError {
           this.maxBasicQueries = DEFMAXBASICQUERIES;
         }
       }
+
+      if (MAX_BASIC_QUERIES_OVERRIDE > 0) {
+        this.maxBasicQueries = MAX_BASIC_QUERIES_OVERRIDE;
+      }
+
       // ugh .. colliding ParseExceptions
       try {
         sq = org.apache.lucene.queryparser.surround.parser.QueryParser.parse(qstr);