2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.solr.handler;
20
import org.apache.solr.common.SolrException;
21
import org.apache.solr.common.params.SolrParams;
22
import org.apache.solr.common.util.NamedList;
23
import org.apache.solr.common.util.SimpleOrderedMap;
24
import org.apache.solr.core.SolrCore;
25
import org.apache.solr.core.SolrInfoMBean;
26
import org.apache.solr.request.SolrQueryRequest;
27
import org.apache.solr.request.SolrRequestHandler;
28
import org.apache.solr.response.SolrQueryResponse;
29
import org.apache.solr.util.SolrPluginUtils;
30
import org.apache.lucene.queryParser.ParseException;
37
public abstract class RequestHandlerBase implements SolrRequestHandler, SolrInfoMBean {
40
// TODO: should we bother synchronizing these, or is an off-by-one error
41
// acceptable every million requests or so?
42
volatile long numRequests;
43
volatile long numErrors;
44
volatile long numTimeouts;
45
protected NamedList initArgs = null;
46
protected SolrParams defaults;
47
protected SolrParams appends;
48
protected SolrParams invariants;
49
volatile long totalTime = 0;
50
long handlerStart = System.currentTimeMillis();
51
protected boolean httpCaching = true;
55
* Initializes the {@link org.apache.solr.request.SolrRequestHandler} by creating three {@link org.apache.solr.common.params.SolrParams} named.
57
* <tr><th>Name</th><th>Description</th></tr>
58
* <tr><td>defaults</td><td>Contains all of the named arguments contained within the list element named "defaults".</td></tr>
59
* <tr><td>appends</td><td>Contains all of the named arguments contained within the list element named "appends".</td></tr>
60
* <tr><td>invariants</td><td>Contains all of the named arguments contained within the list element named "invariants".</td></tr>
65
* <lst name="defaults">
66
* <str name="echoParams">explicit</str>
67
* <str name="qf">text^0.5 features^1.0 name^1.2 sku^1.5 id^10.0</str>
68
* <str name="mm">2<-1 5<-2 6<90%</str>
69
* <str name="bq">incubationdate_dt:[* TO NOW/DAY-1MONTH]^2.2</str>
71
* <lst name="appends">
72
* <str name="fq">inStock:true</str>
75
* <lst name="invariants">
76
* <str name="facet.field">cat</str>
77
* <str name="facet.field">manu_exact</str>
78
* <str name="facet.query">price:[* TO 500]</str>
79
* <str name="facet.query">price:[500 TO *]</str>
84
* @param args The {@link org.apache.solr.common.util.NamedList} to initialize from
86
* @see #handleRequest(org.apache.solr.request.SolrQueryRequest, org.apache.solr.response.SolrQueryResponse)
87
* @see #handleRequestBody(org.apache.solr.request.SolrQueryRequest, org.apache.solr.response.SolrQueryResponse)
88
* @see org.apache.solr.util.SolrPluginUtils#setDefaults(org.apache.solr.request.SolrQueryRequest, org.apache.solr.common.params.SolrParams, org.apache.solr.common.params.SolrParams, org.apache.solr.common.params.SolrParams)
89
* @see SolrParams#toSolrParams(org.apache.solr.common.util.NamedList)
91
* See also the example solrconfig.xml located in the Solr codebase (example/solr/conf).
93
public void init(NamedList args) {
96
// Copied from StandardRequestHandler
98
Object o = args.get("defaults");
99
if (o != null && o instanceof NamedList) {
100
defaults = SolrParams.toSolrParams((NamedList)o);
102
o = args.get("appends");
103
if (o != null && o instanceof NamedList) {
104
appends = SolrParams.toSolrParams((NamedList)o);
106
o = args.get("invariants");
107
if (o != null && o instanceof NamedList) {
108
invariants = SolrParams.toSolrParams((NamedList)o);
112
if (initArgs != null) {
113
Object caching = initArgs.get("httpCaching");
114
httpCaching = caching != null ? Boolean.parseBoolean(caching.toString()) : true;
118
public NamedList getInitArgs() {
122
public abstract void handleRequestBody( SolrQueryRequest req, SolrQueryResponse rsp ) throws Exception;
124
public void handleRequest(SolrQueryRequest req, SolrQueryResponse rsp) {
127
SolrPluginUtils.setDefaults(req,defaults,appends,invariants);
128
rsp.setHttpCaching(httpCaching);
129
handleRequestBody( req, rsp );
131
NamedList header = rsp.getResponseHeader();
133
Object partialResults = header.get("partialResults");
134
boolean timedOut = partialResults == null ? false : (Boolean)partialResults;
137
rsp.setHttpCaching(false);
140
} catch (Exception e) {
141
SolrException.log(SolrCore.log,e);
142
if (e instanceof ParseException) {
143
e = new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
148
totalTime += rsp.getEndTime() - req.getStartTime();
152
//////////////////////// SolrInfoMBeans methods //////////////////////
154
public String getName() {
155
return this.getClass().getName();
158
public abstract String getDescription();
159
public abstract String getSourceId();
160
public abstract String getSource();
161
public abstract String getVersion();
163
public Category getCategory() {
164
return Category.QUERYHANDLER;
167
public URL[] getDocs() {
168
return null; // this can be overridden, but not required
171
public NamedList getStatistics() {
172
NamedList lst = new SimpleOrderedMap();
173
lst.add("handlerStart",handlerStart);
174
lst.add("requests", numRequests);
175
lst.add("errors", numErrors);
176
lst.add("timeouts", numTimeouts);
177
lst.add("totalTime",totalTime);
178
lst.add("avgTimePerRequest", (float) totalTime / (float) this.numRequests);
179
lst.add("avgRequestsPerSecond", (float) numRequests*1000 / (float)(System.currentTimeMillis()-handlerStart));