Apache Lucene open-source search software
SOLR-6573: QueryElevationComponent now works with localParams in the query
Jan Høydahlc1bdf27d9751
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 78ad141c648..ed411ea2e67 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -248,6 +248,8 @@ Bug Fixes* SOLR-6307: Atomic update remove does not work for int array or date array(Anurag Sharma , noble)+* SOLR-6573: QueryElevationComponent now works with localParams in the query (janhoy)+Optimizations----------------------diff --git a/solr/core/src/java/org/apache/solr/handler/component/QueryElevationComponent.java b/solr/core/src/java/org/apache/solr/handler/component/QueryElevationComponent.javaindex 1c0ca43ae04..ee4736ef276 100644--- a/solr/core/src/java/org/apache/solr/handler/component/QueryElevationComponent.java+++ b/solr/core/src/java/org/apache/solr/handler/component/QueryElevationComponent.java@@ -388,7 +388,7 @@ public class QueryElevationComponent extends SearchComponent implements SolrCoreString exStr = params.get(QueryElevationParams.EXCLUDE);Query query = rb.getQuery();- String qstr = rb.getQueryString();+ String qstr = QueryElevationComponent.stripLocalParams(rb.getQueryString());if (query == null || qstr == null) {return;}@@ -490,6 +490,19 @@ public class QueryElevationComponent extends SearchComponent implements SolrCore}}+ /**+ * Simple stripping of localParam at start of query+ * @param queryString the raw query string+ * @return the query string without localParams, or the original queryString if no valid localParam found at beginning of string+ */+ protected static String stripLocalParams(String queryString) {+ if (queryString == null || !queryString.startsWith("{!") || queryString.indexOf("}") == -1) {+ return queryString;+ }++ return queryString.substring(queryString.indexOf("}")+1);+ }+private Sort modifySort(SortField[] current, boolean force, ElevationComparatorSource comparator) {SortSpec tmp = new SortSpec(new Sort(current), Arrays.asList(new SchemaField[current.length]));tmp = modifySortSpec(tmp, force, comparator);diff --git a/solr/core/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.java b/solr/core/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.javaindex a26ee20d7b5..0f4905ad923 100644--- a/solr/core/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.java+++ b/solr/core/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.java@@ -725,4 +725,31 @@ public class QueryElevationComponentTest extends SolrTestCaseJ4 {delete();}}++ @Test+ public void testWithLocalParam() throws Exception {+ assertEquals("foo", QueryElevationComponent.stripLocalParams("foo"));+ assertEquals("foo", QueryElevationComponent.stripLocalParams("{!param=value}foo"));+ assertEquals("", QueryElevationComponent.stripLocalParams("{!param=value}"));+ assertEquals("{!notTerminated", QueryElevationComponent.stripLocalParams("{!notTerminated"));+ assertEquals("{notLocalParam}foo", QueryElevationComponent.stripLocalParams("{notLocalParam}foo"));+ assertEquals(null, QueryElevationComponent.stripLocalParams(null));++ try {+ init("schema11.xml");+ clearIndex();+ assertU(commit());+ assertU(adoc("id", "7", "text", "AAAA", "str_s", "a"));+ assertU(commit());++ assertQ("", req(CommonParams.Q, "{!q.op=AND}AAAA", CommonParams.QT, "/elevate",+ CommonParams.FL, "id, score, [elevated]")+ , "//*[@numFound='1']"+ , "//result/doc[1]/float[@name='id'][.='7.0']"+ , "//result/doc[1]/bool[@name='[elevated]'][.='true']"+ );+ } finally {+ delete();+ }+ }}