use of org.apache.solr.client.solrj.embedded.EmbeddedSolrServer in project jackrabbit-oak by apache.
the class EmbeddedSolrServerProvider method createSolrServer.
private SolrServer createSolrServer() throws Exception {
log.info("creating new embedded solr server with config: {}", solrServerConfiguration);
String solrHomePath = solrServerConfiguration.getSolrHomePath();
String coreName = solrServerConfiguration.getCoreName();
EmbeddedSolrServerConfiguration.HttpConfiguration httpConfiguration = solrServerConfiguration.getHttpConfiguration();
if (solrHomePath != null && coreName != null) {
checkSolrConfiguration(solrHomePath, coreName);
if (httpConfiguration != null) {
if (log.isInfoEnabled()) {
log.info("starting embedded Solr server with http bindings");
}
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(JettySolrRunner.class.getClassLoader());
Integer httpPort = httpConfiguration.getHttpPort();
String context = httpConfiguration.getContext();
JettySolrRunner jettySolrRunner = null;
try {
jettySolrRunner = new JettySolrRunner(solrHomePath, context, httpPort, "solrconfig.xml", "schema.xml", true);
if (log.isInfoEnabled()) {
log.info("Jetty runner instantiated");
}
jettySolrRunner.start(true);
if (log.isInfoEnabled()) {
log.info("Jetty runner started");
}
} catch (Exception t) {
if (log.isErrorEnabled()) {
log.error("an error has occurred while starting Solr Jetty", t);
}
} finally {
if (jettySolrRunner != null && !jettySolrRunner.isRunning()) {
try {
jettySolrRunner.stop();
if (log.isInfoEnabled()) {
log.info("Jetty runner stopped");
}
} catch (Exception e) {
if (log.isErrorEnabled()) {
log.error("error while stopping the Jetty runner", e);
}
}
}
Thread.currentThread().setContextClassLoader(classLoader);
}
if (log.isInfoEnabled()) {
log.info("starting HTTP Solr server");
}
return new HttpWithJettySolrServer(SolrServerConfigurationDefaults.LOCAL_BASE_URL + ':' + httpPort + context + '/' + coreName, jettySolrRunner);
} else {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(CoreContainer.class.getClassLoader());
CoreContainer coreContainer = new CoreContainer(solrHomePath);
try {
if (!coreContainer.isLoaded(coreName)) {
coreContainer.load();
}
} catch (Exception e) {
log.error("cannot load core {}, shutting down embedded Solr..", coreName, e);
try {
coreContainer.shutdown();
} catch (Exception se) {
log.error("could not shutdown embedded Solr", se);
}
return null;
} finally {
Thread.currentThread().setContextClassLoader(classLoader);
}
EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, coreName);
if (server.ping().getStatus() == 0) {
return server;
} else {
throw new IOException("the embedded Solr server is not alive");
}
}
} else {
throw new Exception("SolrServer configuration proprties not set");
}
}
use of org.apache.solr.client.solrj.embedded.EmbeddedSolrServer in project lucene-solr by apache.
the class SolrIndexSplitterTest method testSplitByCores.
@Test
public void testSplitByCores() throws Exception {
// add two docs
String id1 = "dorothy";
assertU(adoc("id", id1));
String id2 = "kansas";
assertU(adoc("id", id2));
assertU(commit());
assertJQ(req("q", "*:*"), "/response/numFound==2");
List<DocRouter.Range> ranges = getRanges(id1, id2);
SolrCore core1 = null, core2 = null;
try {
core1 = h.getCoreContainer().create("split1", ImmutableMap.of("dataDir", indexDir1.getAbsolutePath(), "configSet", "minimal"));
core2 = h.getCoreContainer().create("split2", ImmutableMap.of("dataDir", indexDir2.getAbsolutePath(), "configSet", "minimal"));
LocalSolrQueryRequest request = null;
try {
request = lrf.makeRequest("q", "dummy");
SplitIndexCommand command = new SplitIndexCommand(request, null, Lists.newArrayList(core1, core2), ranges, new PlainIdRouter(), null, null);
new SolrIndexSplitter(command).split();
} finally {
if (request != null)
request.close();
}
EmbeddedSolrServer server1 = new EmbeddedSolrServer(h.getCoreContainer(), "split1");
EmbeddedSolrServer server2 = new EmbeddedSolrServer(h.getCoreContainer(), "split2");
server1.commit(true, true);
server2.commit(true, true);
assertEquals("id:dorothy should be present in split index1", 1, server1.query(new SolrQuery("id:dorothy")).getResults().getNumFound());
assertEquals("id:kansas should not be present in split index1", 0, server1.query(new SolrQuery("id:kansas")).getResults().getNumFound());
assertEquals("id:dorothy should not be present in split index2", 0, server2.query(new SolrQuery("id:dorothy")).getResults().getNumFound());
assertEquals("id:kansas should be present in split index2", 1, server2.query(new SolrQuery("id:kansas")).getResults().getNumFound());
} finally {
h.getCoreContainer().unload("split2");
h.getCoreContainer().unload("split1");
}
}
use of org.apache.solr.client.solrj.embedded.EmbeddedSolrServer in project lucene-solr by apache.
the class SolrExampleTests method testRawFields.
@Test
public void testRawFields() throws Exception {
String rawJson = "{ \"raw\": 1.234, \"id\":\"111\" }";
String rawXml = "<hello>this is <some/><xml/></hello>";
SolrClient client = getSolrClient();
// Empty the database...
// delete everything!
client.deleteByQuery("*:*");
// Now add something...
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "111");
doc.addField("name", "doc1");
doc.addField("json_s", rawJson);
doc.addField("xml_s", rawXml);
client.add(doc);
// make sure this gets in first
client.commit();
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.set(CommonParams.FL, "id,json_s:[json],xml_s:[xml]");
QueryRequest req = new QueryRequest(query);
req.setResponseParser(new BinaryResponseParser());
QueryResponse rsp = req.process(client);
SolrDocumentList out = rsp.getResults();
assertEquals(1, out.getNumFound());
SolrDocument out1 = out.get(0);
assertEquals("111", out1.getFieldValue("id"));
// Check that the 'raw' fields are unchanged using the standard formats
assertEquals(rawJson, out1.get("json_s"));
assertEquals(rawXml, out1.get("xml_s"));
if (client instanceof EmbeddedSolrServer) {
// the EmbeddedSolrServer ignores the configured parser
return;
}
// Check raw JSON Output
query.set("fl", "id,json_s:[json],xml_s:[xml]");
query.set(CommonParams.WT, "json");
req = new QueryRequest(query);
req.setResponseParser(new NoOpResponseParser("json"));
NamedList<Object> resp = client.request(req);
String raw = (String) resp.get("response");
// Check that the response parses as JSON
JSONParser parser = new JSONParser(raw);
int evt = parser.nextEvent();
while (evt != JSONParser.EOF) {
evt = parser.nextEvent();
}
// no escaping
assertTrue(raw.indexOf(rawJson) > 0);
// quoted xml
assertTrue(raw.indexOf('"' + rawXml + '"') > 0);
// Check raw XML Output
req.setResponseParser(new NoOpResponseParser("xml"));
query.set("fl", "id,json_s:[json],xml_s:[xml]");
query.set(CommonParams.WT, "xml");
req = new QueryRequest(query);
req.setResponseParser(new NoOpResponseParser("xml"));
resp = client.request(req);
raw = (String) resp.get("response");
// Check that we get raw xml and json is escaped
// escaped
assertTrue(raw.indexOf('>' + rawJson + '<') > 0);
// raw xml
assertTrue(raw.indexOf(rawXml) > 0);
}
use of org.apache.solr.client.solrj.embedded.EmbeddedSolrServer in project lucene-solr by apache.
the class SolrExampleTests method testUpdateRequestWithParameters.
@Test
public void testUpdateRequestWithParameters() throws Exception {
SolrClient client = createNewSolrClient();
client.deleteByQuery("*:*");
client.commit();
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "id1");
UpdateRequest req = new UpdateRequest();
req.setParam("overwrite", "false");
req.add(doc);
client.request(req);
client.request(req);
client.commit();
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
QueryResponse rsp = client.query(query);
SolrDocumentList out = rsp.getResults();
assertEquals(2, out.getNumFound());
if (!(client instanceof EmbeddedSolrServer)) {
/* Do not close in case of using EmbeddedSolrServer,
* as that would close the CoreContainer */
client.close();
}
}
use of org.apache.solr.client.solrj.embedded.EmbeddedSolrServer in project lucene-solr by apache.
the class TestIntervalFaceting method testSolrJ.
@Test
public void testSolrJ() throws Exception {
assertU(adoc("id", "1", "test_i_dv", "0"));
assertU(adoc("id", "2", "test_i_dv", "1"));
assertU(adoc("id", "3", "test_i_dv", "2"));
assertU(commit());
// Don't close this client, it would shutdown the CoreContainer
@SuppressWarnings("resource") SolrClient client = new EmbeddedSolrServer(h.getCoreContainer(), h.coreName);
SolrQuery q = new SolrQuery();
q.setQuery("*:*");
q.addIntervalFacets("test_i_dv", new String[] { "[0,1]", "[2,*]" });
QueryResponse response = client.query(q);
assertEquals(1, response.getIntervalFacets().size());
assertEquals("test_i_dv", response.getIntervalFacets().get(0).getField());
assertEquals(2, response.getIntervalFacets().get(0).getIntervals().size());
assertEquals("[0,1]", response.getIntervalFacets().get(0).getIntervals().get(0).getKey());
assertEquals("[2,*]", response.getIntervalFacets().get(0).getIntervals().get(1).getKey());
q = new SolrQuery();
q.setQuery("*:*");
q.setFacet(true);
q.add("facet.interval", "{!key=foo}test_i_dv");
q.add("f.test_i_dv.facet.interval.set", "{!key=first}[0,1]");
q.add("f.test_i_dv.facet.interval.set", "{!key=second}[2,*]");
response = client.query(q);
assertEquals(1, response.getIntervalFacets().size());
assertEquals("foo", response.getIntervalFacets().get(0).getField());
assertEquals(2, response.getIntervalFacets().get(0).getIntervals().size());
assertEquals("first", response.getIntervalFacets().get(0).getIntervals().get(0).getKey());
assertEquals("second", response.getIntervalFacets().get(0).getIntervals().get(1).getKey());
}
Aggregations