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.servlet;
21
import java.io.StringWriter;
22
import java.util.ArrayList;
23
import java.util.HashMap;
24
import java.util.List;
26
import org.apache.solr.common.SolrException;
27
import org.apache.solr.common.params.CommonParams;
28
import org.apache.solr.common.params.MapSolrParams;
29
import org.apache.solr.common.params.SolrParams;
30
import org.apache.solr.common.util.ContentStream;
31
import org.apache.solr.common.util.ContentStreamBase;
32
import org.apache.solr.core.CoreContainer;
33
import org.apache.solr.core.CoreDescriptor;
34
import org.apache.solr.core.SolrConfig;
35
import org.apache.solr.core.SolrCore;
36
import org.apache.solr.core.SolrResourceLoader;
37
import org.apache.solr.request.SolrQueryRequest;
38
import org.apache.solr.request.SolrRequestHandler;
39
import org.apache.solr.response.QueryResponseWriter;
40
import org.apache.solr.response.SolrQueryResponse;
41
import org.apache.solr.schema.IndexSchema;
44
* DirectSolrConnection provides an interface to Solr that is similar to
45
* the the HTTP interface, but does not require an HTTP connection.
47
* This class is designed to be as simple as possible and allow for more flexibility
48
* in how you interface to Solr.
50
* @version $Id: DirectSolrConnection.java 1128442 2011-05-27 20:15:59Z mikemccand $
53
public class DirectSolrConnection
56
final SolrRequestParsers parser;
59
* Initialize using the static singleton SolrCore.getSolrCore().
61
* @deprecated use {@link #DirectSolrConnection(SolrCore)}
64
public DirectSolrConnection()
66
this( SolrCore.getSolrCore() );
70
* Initialize using an explicit SolrCore
72
public DirectSolrConnection( SolrCore c )
75
parser = new SolrRequestParsers( c.getSolrConfig() );
79
* This constructor is designed to make it easy for JNI embedded applications
80
* to setup the entire solr environment with a simple interface. It takes three parameters:
82
* <code>instanceDir:</code> The solr instance directory. If null, it will check the standard
83
* places first (JNDI,properties,"solr" directory)
85
* <code>dataDir:</code> where the index is stored.
87
* <code>loggingPath:</code> Path to a java.util.logging.config.file. If the path represents
88
* an absolute path or is relative to the CWD, it will use that. Next it will try a path
89
* relative to the instanceDir. If none of these files exist, it will error.
91
public DirectSolrConnection( String instanceDir, String dataDir, String loggingPath )
93
// If a loggingPath is specified, try using that (this needs to happen first)
94
if( loggingPath != null ) {
95
File loggingConfig = new File( loggingPath );
96
if( !loggingConfig.exists() && instanceDir != null ) {
97
loggingConfig = new File( new File(instanceDir), loggingPath );
99
if( loggingConfig.exists() ) {
100
System.setProperty("java.util.logging.config.file", loggingConfig.getAbsolutePath() );
103
throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "can not find logging file: "+loggingConfig );
107
if( instanceDir == null ) {
108
instanceDir = SolrResourceLoader.locateInstanceDir();
113
CoreContainer cores = new CoreContainer(new SolrResourceLoader(instanceDir));
114
SolrConfig solrConfig = new SolrConfig(instanceDir, SolrConfig.DEFAULT_CONF_FILE, null);
115
CoreDescriptor dcore = new CoreDescriptor(cores, "", solrConfig.getResourceLoader().getInstanceDir());
116
IndexSchema indexSchema = new IndexSchema(solrConfig, instanceDir+"/conf/schema.xml", null);
117
core = new SolrCore( null, dataDir, solrConfig, indexSchema, dcore);
118
cores.register("", core, false);
119
parser = new SolrRequestParsers( solrConfig );
121
catch (Exception ee) {
122
throw new RuntimeException(ee);
130
* String json = solr.request( "/select?qt=dismax&wt=json&q=...", null );
131
* String xml = solr.request( "/update", "<add><doc><field ..." );
133
public String request( String pathAndParams, String body ) throws Exception
136
SolrParams params = null;
137
int idx = pathAndParams.indexOf( '?' );
139
path = pathAndParams.substring( 0, idx );
140
params = SolrRequestParsers.parseQueryString( pathAndParams.substring(idx+1) );
144
params = new MapSolrParams( new HashMap<String, String>() );
147
// Extract the handler from the path or params
148
SolrRequestHandler handler = core.getRequestHandler( path );
149
if( handler == null ) {
150
if( "/select".equals( path ) || "/select/".equalsIgnoreCase( path) ) {
151
String qt = params.get( CommonParams.QT );
152
handler = core.getRequestHandler( qt );
153
if( handler == null ) {
154
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "unknown handler: "+qt);
159
if( handler == null ) {
160
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "unknown handler: "+path );
163
return request(handler, params, body);
166
public String request(SolrRequestHandler handler, SolrParams params, String body ) throws Exception {
169
params = new MapSolrParams( new HashMap<String, String>() );
171
// Make a stream for the 'body' content
172
List<ContentStream> streams = new ArrayList<ContentStream>( 1 );
173
if( body != null && body.length() > 0 ) {
174
streams.add( new ContentStreamBase.StringStream( body ) );
177
SolrQueryRequest req = null;
179
req = parser.buildRequestFrom( core, params, streams );
180
SolrQueryResponse rsp = new SolrQueryResponse();
181
core.execute( handler, req, rsp );
182
if( rsp.getException() != null ) {
183
throw rsp.getException();
187
QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
188
StringWriter out = new StringWriter();
189
responseWriter.write(out, req, rsp);
190
return out.toString();
199
* Use this method to close the underlying SolrCore.
203
public void close() {