~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/handler/RequestHandlerBase.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
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
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
 
18
 
package org.apache.solr.handler;
19
 
 
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;
31
 
 
32
 
import java.net.URL;
33
 
 
34
 
/**
35
 
 *
36
 
 */
37
 
public abstract class RequestHandlerBase implements SolrRequestHandler, SolrInfoMBean {
38
 
 
39
 
  // statistics
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;
52
 
 
53
 
 
54
 
  /**
55
 
   * Initializes the {@link org.apache.solr.request.SolrRequestHandler} by creating three {@link org.apache.solr.common.params.SolrParams} named.
56
 
   * <table border="1">
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>
61
 
   * </table>
62
 
   *
63
 
   * Example:
64
 
   * <pre>
65
 
   * &lt;lst name="defaults"&gt;
66
 
   * &lt;str name="echoParams"&gt;explicit&lt;/str&gt;
67
 
   * &lt;str name="qf"&gt;text^0.5 features^1.0 name^1.2 sku^1.5 id^10.0&lt;/str&gt;
68
 
   * &lt;str name="mm"&gt;2&lt;-1 5&lt;-2 6&lt;90%&lt;/str&gt;
69
 
   * &lt;str name="bq"&gt;incubationdate_dt:[* TO NOW/DAY-1MONTH]^2.2&lt;/str&gt;
70
 
   * &lt;/lst&gt;
71
 
   * &lt;lst name="appends"&gt;
72
 
   * &lt;str name="fq"&gt;inStock:true&lt;/str&gt;
73
 
   * &lt;/lst&gt;
74
 
   *
75
 
   * &lt;lst name="invariants"&gt;
76
 
   * &lt;str name="facet.field"&gt;cat&lt;/str&gt;
77
 
   * &lt;str name="facet.field"&gt;manu_exact&lt;/str&gt;
78
 
   * &lt;str name="facet.query"&gt;price:[* TO 500]&lt;/str&gt;
79
 
   * &lt;str name="facet.query"&gt;price:[500 TO *]&lt;/str&gt;
80
 
   * &lt;/lst&gt;
81
 
   * </pre>
82
 
   *
83
 
   *
84
 
   * @param args The {@link org.apache.solr.common.util.NamedList} to initialize from
85
 
   *
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)
90
 
   *
91
 
   * See also the example solrconfig.xml located in the Solr codebase (example/solr/conf).
92
 
   */
93
 
  public void init(NamedList args) {
94
 
    initArgs = args;
95
 
 
96
 
    // Copied from StandardRequestHandler 
97
 
    if( args != null ) {
98
 
      Object o = args.get("defaults");
99
 
      if (o != null && o instanceof NamedList) {
100
 
        defaults = SolrParams.toSolrParams((NamedList)o);
101
 
      }
102
 
      o = args.get("appends");
103
 
      if (o != null && o instanceof NamedList) {
104
 
        appends = SolrParams.toSolrParams((NamedList)o);
105
 
      }
106
 
      o = args.get("invariants");
107
 
      if (o != null && o instanceof NamedList) {
108
 
        invariants = SolrParams.toSolrParams((NamedList)o);
109
 
      }
110
 
    }
111
 
    
112
 
    if (initArgs != null) {
113
 
      Object caching = initArgs.get("httpCaching");
114
 
      httpCaching = caching != null ? Boolean.parseBoolean(caching.toString()) : true;
115
 
    }
116
 
  }
117
 
 
118
 
  public NamedList getInitArgs() {
119
 
    return initArgs;
120
 
  }
121
 
  
122
 
  public abstract void handleRequestBody( SolrQueryRequest req, SolrQueryResponse rsp ) throws Exception;
123
 
 
124
 
  public void handleRequest(SolrQueryRequest req, SolrQueryResponse rsp) {
125
 
    numRequests++;
126
 
    try {
127
 
      SolrPluginUtils.setDefaults(req,defaults,appends,invariants);
128
 
      rsp.setHttpCaching(httpCaching);
129
 
      handleRequestBody( req, rsp );
130
 
      // count timeouts
131
 
      NamedList header = rsp.getResponseHeader();
132
 
      if(header != null) {
133
 
        Object partialResults = header.get("partialResults");
134
 
        boolean timedOut = partialResults == null ? false : (Boolean)partialResults;
135
 
        if( timedOut ) {
136
 
          numTimeouts++;
137
 
          rsp.setHttpCaching(false);
138
 
        }
139
 
      }
140
 
    } catch (Exception e) {
141
 
      SolrException.log(SolrCore.log,e);
142
 
      if (e instanceof ParseException) {
143
 
        e = new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
144
 
      }
145
 
      rsp.setException(e);
146
 
      numErrors++;
147
 
    }
148
 
    totalTime += rsp.getEndTime() - req.getStartTime();
149
 
  }
150
 
  
151
 
 
152
 
  //////////////////////// SolrInfoMBeans methods //////////////////////
153
 
 
154
 
  public String getName() {
155
 
    return this.getClass().getName();
156
 
  }
157
 
 
158
 
  public abstract String getDescription();
159
 
  public abstract String getSourceId();
160
 
  public abstract String getSource();
161
 
  public abstract String getVersion();
162
 
  
163
 
  public Category getCategory() {
164
 
    return Category.QUERYHANDLER;
165
 
  }
166
 
 
167
 
  public URL[] getDocs() {
168
 
    return null;  // this can be overridden, but not required
169
 
  }
170
 
 
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));   
180
 
    return lst;
181
 
  }
182
 
  
183
 
}
184
 
 
185