SOLR-46 - support for appended and invariant SolrParams in solrconfig.xml

git-svn-id: https://svn.apache.org/repos/asf/incubator/solr/trunk@442747 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Chris M. Hostetter 2006-09-12 22:50:06 +00:00
parent 7034514d2f
commit ec38c40d94
7 changed files with 166 additions and 5 deletions

View File

@ -49,6 +49,10 @@ New Features
24. Simple faceted search support for fields (enumerating terms)
and arbitrary queries added to both StandardRequestHandler and
DisMaxRequestHandler. (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 behavior
1. classes reorganized into different packages, package names changed to Apache

View File

@ -216,6 +216,51 @@
<!-- Note how you can register the same handler multiple times with
different 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&lt;-1 5&lt;-2 6&lt;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 init
params are "defaults" if you don't explicitly specify any defaults.

View File

@ -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+")}";
}
}

View File

@ -140,6 +140,8 @@ public class DisMaxRequestHandler
long 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 DisMaxRequestHandler
if (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 DisMaxRequestHandler
numRequests++;
try {
U.setDefaults(req,defaults);
U.setDefaults(req,defaults,appends,invariants);
SolrParams params = req.getParams();
int flags = 0;

View File

@ -60,6 +60,8 @@ public class StandardRequestHandler implements SolrRequestHandler, SolrInfoMBean
long 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, SolrInfoMBean
if (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);

View File

@ -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);
}

View File

@ -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"));
}