Search in sources :

Example 1 with EmbeddedSolrServer

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");
    }
}
Also used : EmbeddedSolrServerConfiguration(org.apache.jackrabbit.oak.plugins.index.solr.configuration.EmbeddedSolrServerConfiguration) CoreContainer(org.apache.solr.core.CoreContainer) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) IOException(java.io.IOException) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) IOException(java.io.IOException)

Example 2 with EmbeddedSolrServer

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");
    }
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrCore(org.apache.solr.core.SolrCore) PlainIdRouter(org.apache.solr.common.cloud.PlainIdRouter) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 3 with EmbeddedSolrServer

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);
}
Also used : NoOpResponseParser(org.apache.solr.client.solrj.impl.NoOpResponseParser) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) StringContains.containsString(org.junit.internal.matchers.StringContains.containsString) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) ErrorTrackingConcurrentUpdateSolrClient(org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) JSONParser(org.noggit.JSONParser) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) Test(org.junit.Test)

Example 4 with EmbeddedSolrServer

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();
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) ErrorTrackingConcurrentUpdateSolrClient(org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) AbstractUpdateRequest(org.apache.solr.client.solrj.request.AbstractUpdateRequest) ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) Test(org.junit.Test)

Example 5 with EmbeddedSolrServer

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());
}
Also used : SolrClient(org.apache.solr.client.solrj.SolrClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Aggregations

EmbeddedSolrServer (org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)17 File (java.io.File)5 IOException (java.io.IOException)5 CoreContainer (org.apache.solr.core.CoreContainer)5 CoreDescriptor (org.apache.solr.core.CoreDescriptor)5 SolrCore (org.apache.solr.core.SolrCore)5 Test (org.junit.Test)4 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)3 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)3 SolrInputDocument (org.apache.solr.common.SolrInputDocument)3 SolrResourceLoader (org.apache.solr.core.SolrResourceLoader)3 IndexSchema (org.apache.solr.schema.IndexSchema)3 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 EmbeddedSolrServerConfiguration (org.apache.jackrabbit.oak.plugins.index.solr.configuration.EmbeddedSolrServerConfiguration)2 SolrClient (org.apache.solr.client.solrj.SolrClient)2 SolrQuery (org.apache.solr.client.solrj.SolrQuery)2 SolrServer (org.apache.solr.client.solrj.SolrServer)2 ErrorTrackingConcurrentUpdateSolrClient (org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient)2