org.apache.solr.client.solrj.embedded.EmbeddedSolrServer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solr-core Show documentation
Show all versions of solr-core Show documentation
Apache Solr (module: core)
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.client.solrj.embedded;
import static org.apache.solr.common.params.CommonParams.PATH;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.function.Supplier;
import org.apache.lucene.search.TotalHits.Relation;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.StreamingResponseCallback;
import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
import org.apache.solr.client.solrj.request.RequestWriter;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ContentStreamBase;
import org.apache.solr.common.util.JavaBinCodec;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.Utils;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.core.NodeConfig;
import org.apache.solr.core.SolrCore;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrRequestHandler;
import org.apache.solr.request.SolrRequestInfo;
import org.apache.solr.response.BinaryResponseWriter;
import org.apache.solr.response.ResultContext;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.servlet.SolrRequestParsers;
/**
* SolrClient that connects directly to a CoreContainer.
*
* @since solr 1.3
*/
public class EmbeddedSolrServer extends SolrClient {
protected final CoreContainer coreContainer;
protected final String coreName;
private final SolrRequestParsers _parser;
private final RequestWriterSupplier supplier;
private boolean containerIsLocal = false;
@SuppressWarnings("ImmutableEnumChecker")
public enum RequestWriterSupplier {
JavaBin(() -> new BinaryRequestWriter()),
XML(() -> new RequestWriter());
private final Supplier supplier;
RequestWriterSupplier(final Supplier supplier) {
this.supplier = supplier;
}
public RequestWriter newRequestWriter() {
return supplier.get();
}
}
/**
* Create an EmbeddedSolrServer using a given solr home directory
*
* @param solrHome the solr home directory
* @param defaultCoreName the core to route requests to by default (optional)
*/
public EmbeddedSolrServer(Path solrHome, String defaultCoreName) {
this(load(new CoreContainer(solrHome, new Properties())), defaultCoreName);
containerIsLocal = true;
}
/**
* Create an EmbeddedSolrServer using a NodeConfig
*
* @param nodeConfig the configuration
* @param defaultCoreName the core to route requests to by default (optional)
*/
public EmbeddedSolrServer(NodeConfig nodeConfig, String defaultCoreName) {
this(load(new CoreContainer(nodeConfig)), defaultCoreName);
containerIsLocal = true;
}
private static CoreContainer load(CoreContainer cc) {
cc.load();
return cc;
}
/** Create an EmbeddedSolrServer wrapping a particular SolrCore */
public EmbeddedSolrServer(SolrCore core) {
this(core.getCoreContainer(), core.getName());
}
/**
* Create an EmbeddedSolrServer wrapping a CoreContainer.
*
* @param coreContainer the core container
* @param coreName the core to route requests to by default (optional)
*/
public EmbeddedSolrServer(CoreContainer coreContainer, String coreName) {
this(coreContainer, coreName, RequestWriterSupplier.JavaBin);
}
/**
* Create an EmbeddedSolrServer wrapping a CoreContainer.
*
* @param coreContainer the core container
* @param coreName the core to route requests to by default
* @param supplier the supplier used to create a {@link RequestWriter}
*/
public EmbeddedSolrServer(
CoreContainer coreContainer, String coreName, RequestWriterSupplier supplier) {
if (coreContainer == null) {
throw new NullPointerException("CoreContainer instance required");
}
this.coreContainer = coreContainer;
this.coreName = coreName;
_parser = new SolrRequestParsers(null);
this.supplier = supplier;
}
// TODO-- this implementation sends the response to XML and then parses it.
// It *should* be able to convert the response directly into a named list.
@Override
public NamedList