Apache Lucene open-source search software
SOLR-46 - support for appended and invariant SolrParams in solrconfig.xml
Chris M. Hostetterec38c40d94fb
diff --git a/CHANGES.txt b/CHANGES.txtindex 265749cf7b1..131a13e43d4 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -49,6 +49,10 @@ New Features24. Simple faceted search support for fields (enumerating terms)and arbitrary queries added to both StandardRequestHandler andDisMaxRequestHandler. (hossman, SOLR-44)+25. In addition to specifying default RequestHandler params in the+ solrconfig.xml, support has been added for configuring values to be+ appended to the multi-val request params, as well as for configuring+ invariant params that can not overridden in the query. (hossman, SOLR-46)Changes in runtime behavior1. classes reorganized into different packages, package names changed to Apachediff --git a/example/solr/conf/solrconfig.xml b/example/solr/conf/solrconfig.xmlindex f63576fc965..8d7a08c91dc 100755--- a/example/solr/conf/solrconfig.xml+++ b/example/solr/conf/solrconfig.xml@@ -216,6 +216,51 @@<!-- Note how you can register the same handler multiple times withdifferent names (and different init parameters)-->+ <requestHandler name="partitioned" class="solr.DisMaxRequestHandler" >+ <lst name="defaults">+ <str name="qf">text^0.5 features^1.0 name^1.2 sku^1.5 id^10.0</str>+ <str name="mm">2<-1 5<-2 6<90%</str>+ </lst>+ <!-- In addition to defaults, "appends" params can be specified+ to identify values which should be appended to the list of+ multi-val params from the query (or the existing "defaults").++ In this example, the param "fq=instock:true" will be appended to+ any query time fq params the user may specify, as a mechanism for+ partitioning the index, independent of any user selected filtering+ that may also be desired (perhaps as a result of faceted searching).++ NOTE: there is *absolutely* nothing a client can do to prevent these+ "appends" values from being used, so don't use this mechanism+ unless you are sure you always want it.+ -->+ <lst name="appends">+ <str name="fq">inStock:true</str>+ </lst>+ <!-- "invariants" are a way of letting the Solr maintainer lock down+ the options available to Solr clients. Any params values+ specified here are used regardless of what values may be specified+ in either the query, the "defaults", or the "appends" params.++ In this example, the facet.field and facet.query params are fixed,+ limiting the facets clients can use. Faceting is not turned on by+ default - but if the client does specify facet=true in the request,+ these are the only facets they will be able to see counts for;+ regardless of what other facet.field or facet.query params they+ may specify.++ NOTE: there is *absolutely* nothing a client can do to prevent these+ "invariants" values from being used, so don't use this mechanism+ unless you are sure you always want it.+ -->+ <lst name="invariants">+ <str name="facet.field">cat</str>+ <str name="facet.field">manu_exact</str>+ <str name="facet.query">price:[* TO 500]</str>+ <str name="facet.query">price:[500 TO *]</str>+ </lst>+ </requestHandler>+<requestHandler name="instock" class="solr.DisMaxRequestHandler" ><!-- for legacy reasons, DisMaxRequestHandler will assume all initparams are "defaults" if you don't explicitly specify any defaults.diff --git a/src/java/org/apache/solr/request/AppendedSolrParams.java b/src/java/org/apache/solr/request/AppendedSolrParams.javanew file mode 100644index 00000000000..e14e9482675--- /dev/null+++ b/src/java/org/apache/solr/request/AppendedSolrParams.java@@ -0,0 +1,47 @@+/**+ * Copyright 2006 The Apache Software Foundation+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.solr.request;++/**+ * SolrParams wrapper which acts similar to DefaultSolrParams except that+ * it "appends" the values of multi-value params from both sub instances, so+ * that all of the values are returned.+ */+public class AppendedSolrParams extends DefaultSolrParams {+ public AppendedSolrParams(SolrParams main, SolrParams extra) {+ super(main, extra);+ }++ public String[] getParams(String param) {+ String[] main = params.getParams(param);+ String[] extra = defaults.getParams(param);+ if (null == extra || 0 == extra.length) {+ return main;+ }+ if (null == main || 0 == main.length) {+ return extra;+ }+ String[] result = new String[main.length + extra.length];+ System.arraycopy(main,0,result,0,main.length);+ System.arraycopy(extra,0,result,main.length,extra.length);+ return result;+ }++ public String toString() {+ return "{main("+params+"),extra("+defaults+")}";+ }+}diff --git a/src/java/org/apache/solr/request/DisMaxRequestHandler.java b/src/java/org/apache/solr/request/DisMaxRequestHandler.javaindex abee8596e50..7fb238bad00 100644--- a/src/java/org/apache/solr/request/DisMaxRequestHandler.java+++ b/src/java/org/apache/solr/request/DisMaxRequestHandler.java@@ -140,6 +140,8 @@ public class DisMaxRequestHandlerlong numErrors;SolrParams defaults;+ SolrParams appends;+ SolrParams invariants;/** shorten the class referneces for utilities */private static class U extends SolrPluginUtils {@@ -206,6 +208,14 @@ public class DisMaxRequestHandlerif (o != null && o instanceof NamedList) {defaults = SolrParams.toSolrParams((NamedList)o);}+ o = args.get("appends");+ if (o != null && o instanceof NamedList) {+ appends = SolrParams.toSolrParams((NamedList)o);+ }+ o = args.get("invariants");+ if (o != null && o instanceof NamedList) {+ invariants = SolrParams.toSolrParams((NamedList)o);+ }}}@@ -213,7 +223,7 @@ public class DisMaxRequestHandlernumRequests++;try {- U.setDefaults(req,defaults);+ U.setDefaults(req,defaults,appends,invariants);SolrParams params = req.getParams();int flags = 0;diff --git a/src/java/org/apache/solr/request/StandardRequestHandler.java b/src/java/org/apache/solr/request/StandardRequestHandler.javaindex b2a57fdbc9a..6172d7c09c9 100644--- a/src/java/org/apache/solr/request/StandardRequestHandler.java+++ b/src/java/org/apache/solr/request/StandardRequestHandler.java@@ -60,6 +60,8 @@ public class StandardRequestHandler implements SolrRequestHandler, SolrInfoMBeanlong numRequests;long numErrors;SolrParams defaults;+ SolrParams appends;+ SolrParams invariants;/** shorten the class references for utilities */private static class U extends SolrPluginUtils {@@ -71,13 +73,22 @@ public class StandardRequestHandler implements SolrRequestHandler, SolrInfoMBeanif (o != null && o instanceof NamedList) {defaults = SolrParams.toSolrParams((NamedList)o);}+ o = args.get("appends");+ if (o != null && o instanceof NamedList) {+ appends = SolrParams.toSolrParams((NamedList)o);+ }+ o = args.get("invariants");+ if (o != null && o instanceof NamedList) {+ invariants = SolrParams.toSolrParams((NamedList)o);+ }+}public void handleRequest(SolrQueryRequest req, SolrQueryResponse rsp) {numRequests++;try {- U.setDefaults(req,defaults);+ U.setDefaults(req,defaults,appends,invariants);SolrParams p = req.getParams();String sreq = p.get(Q);diff --git a/src/java/org/apache/solr/util/SolrPluginUtils.java b/src/java/org/apache/solr/util/SolrPluginUtils.javaindex cfd6e93231f..05fffaf56da 100644--- a/src/java/org/apache/solr/util/SolrPluginUtils.java+++ b/src/java/org/apache/solr/util/SolrPluginUtils.java@@ -32,6 +32,7 @@ import org.apache.solr.request.SolrParams;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.request.SolrQueryResponse;import org.apache.solr.request.DefaultSolrParams;+import org.apache.solr.request.AppendedSolrParams;import org.apache.solr.schema.IndexSchema;import org.apache.solr.search.*;@@ -56,14 +57,41 @@ import java.util.regex.Pattern;*/public class SolrPluginUtils {- /** set defaults on a SolrQueryRequest */+ /**+ * Set defaults on a SolrQueryRequest.+ *+ * RequestHandlers can use this method to ensure their defaults are+ * visible to other components such as the response writer+ */public static void setDefaults(SolrQueryRequest req, SolrParams defaults) {+ setDefaults(req, defaults, null, null);+ }++ /**+ * Set default-ish params on a SolrQueryRequest.+ *+ * RequestHandlers can use this method to ensure their defaults and+ * overrides are visible to other components such as the response writer+ *+ * @param req The request whose params we are interested i+ * @param defaults values to be used if no values are specified in the request params+ * @param appends values to be appended to those from the request (or defaults) when dealing with multi-val params, or treated as another layer of defaults for singl-val params.+ * @param invariants values which will be used instead of any request, or default values, regardless of context.+ */+ public static void setDefaults(SolrQueryRequest req, SolrParams defaults,+ SolrParams appends, SolrParams invariants) {+SolrParams p = req.getParams();if (defaults != null) {p = new DefaultSolrParams(p,defaults);- // set params so they will be visible to other components such as the response writer- req.setParams(p);}+ if (appends != null) {+ p = new AppendedSolrParams(p,appends);+ }+ if (invariants != null) {+ p = new DefaultSolrParams(invariants,p);+ }+ req.setParams(p);}diff --git a/src/test/org/apache/solr/BasicFunctionalityTest.java b/src/test/org/apache/solr/BasicFunctionalityTest.javaindex 08a1a37f511..c5b5928c0e5 100644--- a/src/test/org/apache/solr/BasicFunctionalityTest.java+++ b/src/test/org/apache/solr/BasicFunctionalityTest.java@@ -284,6 +284,22 @@ public class BasicFunctionalityTest extends AbstractSolrTestCase {assertEquals(p.getBool("foo",true), true);assertEquals(p.getBool("foo",false), false);assertEquals(!!p.getBool("bt"), !p.getBool("bf"));++ NamedList more = new NamedList();+ more.add("s", "aaa");+ more.add("s", "ccc");+ more.add("ss","YYY");+ more.add("xx","XXX");+ p = new AppendedSolrParams(p, SolrParams.toSolrParams(more));+ assertEquals(3, p.getParams("s").length);+ assertEquals("bbb", p.getParams("s")[0]);+ assertEquals("aaa", p.getParams("s")[1]);+ assertEquals("ccc", p.getParams("s")[2]);+ assertEquals(3, p.getParams("s").length);+ assertEquals("SSS", p.get("ss"));+ assertEquals("XXX", p.get("xx"));++}