Search in sources :

Example 16 with HttpSolrClient

use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.

the class SolrExampleTests method testExampleConfig.

/**
   * query the example
   */
@Test
public void testExampleConfig() throws Exception {
    SolrClient client = getSolrClient();
    // Empty the database...
    // delete everything!
    client.deleteByQuery("*:*");
    // Now add something...
    SolrInputDocument doc = new SolrInputDocument();
    String docID = "1112211111";
    doc.addField("id", docID);
    doc.addField("name", "my name!");
    Assert.assertEquals(null, doc.getField("foo"));
    Assert.assertTrue(doc.getField("name").getValue() != null);
    UpdateResponse upres = client.add(doc);
    // System.out.println( "ADD:"+upres.getResponse() );
    Assert.assertEquals(0, upres.getStatus());
    upres = client.commit(true, true);
    // System.out.println( "COMMIT:"+upres.getResponse() );
    Assert.assertEquals(0, upres.getStatus());
    upres = client.optimize(true, true);
    // System.out.println( "OPTIMIZE:"+upres.getResponse() );
    Assert.assertEquals(0, upres.getStatus());
    SolrQuery query = new SolrQuery();
    query.setQuery("id:" + docID);
    QueryResponse response = client.query(query);
    Assert.assertEquals(docID, response.getResults().get(0).getFieldValue("id"));
    // Now add a few docs for facet testing...
    List<SolrInputDocument> docs = new ArrayList<>();
    SolrInputDocument doc2 = new SolrInputDocument();
    doc2.addField("id", "2");
    doc2.addField("inStock", true);
    doc2.addField("price", 2);
    doc2.addField("timestamp_dt", new java.util.Date());
    docs.add(doc2);
    SolrInputDocument doc3 = new SolrInputDocument();
    doc3.addField("id", "3");
    doc3.addField("inStock", false);
    doc3.addField("price", 3);
    doc3.addField("timestamp_dt", new java.util.Date());
    docs.add(doc3);
    SolrInputDocument doc4 = new SolrInputDocument();
    doc4.addField("id", "4");
    doc4.addField("inStock", true);
    doc4.addField("price", 4);
    doc4.addField("timestamp_dt", new java.util.Date());
    docs.add(doc4);
    SolrInputDocument doc5 = new SolrInputDocument();
    doc5.addField("id", "5");
    doc5.addField("inStock", false);
    doc5.addField("price", 5);
    doc5.addField("timestamp_dt", new java.util.Date());
    docs.add(doc5);
    upres = client.add(docs);
    // System.out.println( "ADD:"+upres.getResponse() );
    Assert.assertEquals(0, upres.getStatus());
    upres = client.commit(true, true);
    // System.out.println( "COMMIT:"+upres.getResponse() );
    Assert.assertEquals(0, upres.getStatus());
    upres = client.optimize(true, true);
    // System.out.println( "OPTIMIZE:"+upres.getResponse() );
    Assert.assertEquals(0, upres.getStatus());
    query = new SolrQuery("*:*");
    query.addFacetQuery("price:[* TO 2]");
    query.addFacetQuery("price:[2 TO 4]");
    query.addFacetQuery("price:[5 TO *]");
    query.addFacetField("inStock");
    query.addFacetField("price");
    query.addFacetField("timestamp_dt");
    query.removeFilterQuery("inStock:true");
    response = client.query(query);
    Assert.assertEquals(0, response.getStatus());
    Assert.assertEquals(5, response.getResults().getNumFound());
    Assert.assertEquals(3, response.getFacetQuery().size());
    Assert.assertEquals(2, response.getFacetField("inStock").getValueCount());
    Assert.assertEquals(4, response.getFacetField("price").getValueCount());
    // test a second query, test making a copy of the main query
    SolrQuery query2 = query.getCopy();
    query2.addFilterQuery("inStock:true");
    Assert.assertFalse(query.getFilterQueries() == query2.getFilterQueries());
    response = client.query(query2);
    Assert.assertEquals(1, query2.getFilterQueries().length);
    Assert.assertEquals(0, response.getStatus());
    Assert.assertEquals(2, response.getResults().getNumFound());
    for (SolrDocument outDoc : response.getResults()) {
        assertEquals(true, outDoc.getFieldValue("inStock"));
    }
    // sanity check round tripping of params...
    query = new SolrQuery("foo");
    query.addFilterQuery("{!field f=inStock}true");
    query.addFilterQuery("{!term f=name}hoss");
    query.addFacetQuery("price:[* TO 2]");
    query.addFacetQuery("price:[2 TO 4]");
    response = client.query(query);
    assertTrue("echoed params are not a NamedList: " + response.getResponseHeader().get("params").getClass(), response.getResponseHeader().get("params") instanceof NamedList);
    NamedList echo = (NamedList) response.getResponseHeader().get("params");
    List values = null;
    assertEquals("foo", echo.get("q"));
    assertTrue("echoed fq is not a List: " + echo.get("fq").getClass(), echo.get("fq") instanceof List);
    values = (List) echo.get("fq");
    Assert.assertEquals(2, values.size());
    Assert.assertEquals("{!field f=inStock}true", values.get(0));
    Assert.assertEquals("{!term f=name}hoss", values.get(1));
    assertTrue("echoed facet.query is not a List: " + echo.get("facet.query").getClass(), echo.get("facet.query") instanceof List);
    values = (List) echo.get("facet.query");
    Assert.assertEquals(2, values.size());
    Assert.assertEquals("price:[* TO 2]", values.get(0));
    Assert.assertEquals("price:[2 TO 4]", values.get(1));
    if (jetty != null) {
        // check system wide system handler + "/admin/info/system"
        String url = jetty.getBaseUrl().toString();
        try (HttpSolrClient adminClient = getHttpSolrClient(url)) {
            SolrQuery q = new SolrQuery();
            q.set("qt", "/admin/info/system");
            QueryResponse rsp = adminClient.query(q);
            assertNotNull(rsp.getResponse().get("mode"));
            assertNotNull(rsp.getResponse().get("lucene"));
        }
    }
}
Also used : NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) StringContains.containsString(org.junit.internal.matchers.StringContains.containsString) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) 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) SolrDocumentList(org.apache.solr.common.SolrDocumentList) List(java.util.List) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) Test(org.junit.Test)

Example 17 with HttpSolrClient

use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.

the class SolrExampleTests method testUnicode.

public void testUnicode() throws Exception {
    Random random = random();
    int numIterations = atLeast(3);
    SolrClient client = getSolrClient();
    // save the old parser, so we can set it back.
    ResponseParser oldParser = null;
    if (client instanceof HttpSolrClient) {
        HttpSolrClient httpSolrClient = (HttpSolrClient) client;
        oldParser = httpSolrClient.getParser();
    }
    try {
        for (int iteration = 0; iteration < numIterations; iteration++) {
            // choose format
            if (client instanceof HttpSolrClient) {
                if (random.nextBoolean()) {
                    ((HttpSolrClient) client).setParser(new BinaryResponseParser());
                } else {
                    ((HttpSolrClient) client).setParser(new XMLResponseParser());
                }
            }
            int numDocs = TestUtil.nextInt(random(), 1, 10 * RANDOM_MULTIPLIER);
            // Empty the database...
            // delete everything!
            client.deleteByQuery("*:*");
            List<SolrInputDocument> docs = new ArrayList<>();
            for (int i = 0; i < numDocs; i++) {
                // Now add something...
                SolrInputDocument doc = new SolrInputDocument();
                doc.addField("id", "" + i);
                doc.addField("unicode_s", randomTestString(30));
                docs.add(doc);
            }
            client.add(docs);
            client.commit();
            SolrQuery query = new SolrQuery();
            query.setQuery("*:*");
            query.setRows(numDocs);
            QueryResponse rsp = client.query(query);
            for (int i = 0; i < numDocs; i++) {
                String expected = (String) docs.get(i).getFieldValue("unicode_s");
                String actual = (String) rsp.getResults().get(i).getFieldValue("unicode_s");
                assertEquals(expected, actual);
            }
        }
    } finally {
        if (oldParser != null) {
            // set the old parser back
            ((HttpSolrClient) client).setParser(oldParser);
        }
    }
}
Also used : NoOpResponseParser(org.apache.solr.client.solrj.impl.NoOpResponseParser) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser) ArrayList(java.util.ArrayList) StringContains.containsString(org.junit.internal.matchers.StringContains.containsString) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrInputDocument(org.apache.solr.common.SolrInputDocument) Random(java.util.Random) 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) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser)

Example 18 with HttpSolrClient

use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.

the class TestBatchUpdate method testWithBinaryBean.

@Test
public void testWithBinaryBean() throws Exception {
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    client.setRequestWriter(new BinaryRequestWriter());
    // delete everything!
    client.deleteByQuery("*:*");
    final int[] counter = new int[1];
    counter[0] = 0;
    client.addBeans(new Iterator<Bean>() {

        @Override
        public boolean hasNext() {
            return counter[0] < numdocs;
        }

        @Override
        public Bean next() {
            Bean bean = new Bean();
            bean.id = "" + (++counter[0]);
            bean.cat = "foocat";
            return bean;
        }

        @Override
        public void remove() {
        //do nothing
        }
    });
    client.commit();
    SolrQuery query = new SolrQuery("*:*");
    QueryResponse response = client.query(query);
    assertEquals(0, response.getStatus());
    assertEquals(numdocs, response.getResults().getNumFound());
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) BinaryRequestWriter(org.apache.solr.client.solrj.impl.BinaryRequestWriter) Test(org.junit.Test)

Example 19 with HttpSolrClient

use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.

the class TestBatchUpdate method testWithBinary.

@Test
public void testWithBinary() throws Exception {
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    client.setRequestWriter(new BinaryRequestWriter());
    // delete everything!
    client.deleteByQuery("*:*");
    doIt(client);
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) BinaryRequestWriter(org.apache.solr.client.solrj.impl.BinaryRequestWriter) Test(org.junit.Test)

Example 20 with HttpSolrClient

use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.

the class SolrExceptionTest method testSolrException.

public void testSolrException() throws Throwable {
    // test a connection to a solr server that probably doesn't exist
    // this is a very simple test and most of the test should be considered verified 
    // if the compiler won't let you by without the try/catch
    boolean gotExpectedError = false;
    CloseableHttpClient httpClient = null;
    try {
        // switched to a local address to avoid going out on the net, ns lookup issues, etc.
        // set a 1ms timeout to let the connection fail faster.
        httpClient = HttpClientUtil.createClient(null);
        try (HttpSolrClient client = getHttpSolrClient("http://[ff01::114]:11235/solr/", httpClient)) {
            client.setConnectionTimeout(1);
            SolrQuery query = new SolrQuery("test123");
            client.query(query);
        }
        httpClient.close();
    } catch (SolrServerException sse) {
        gotExpectedError = true;
    /***
      assertTrue(UnknownHostException.class == sse.getRootCause().getClass()
              //If one is using OpenDNS, then you don't get UnknownHostException, instead you get back that the query couldn't execute
              || (sse.getRootCause().getClass() == SolrException.class && ((SolrException) sse.getRootCause()).code() == 302 && sse.getMessage().equals("Error executing query")));
      ***/
    } finally {
        if (httpClient != null)
            HttpClientUtil.close(httpClient);
    }
    assertTrue(gotExpectedError);
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrTestCaseJ4.getHttpSolrClient(org.apache.solr.SolrTestCaseJ4.getHttpSolrClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient)

Aggregations

HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)187 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)48 SolrClient (org.apache.solr.client.solrj.SolrClient)46 Test (org.junit.Test)46 SolrQuery (org.apache.solr.client.solrj.SolrQuery)38 ArrayList (java.util.ArrayList)35 Replica (org.apache.solr.common.cloud.Replica)34 SolrInputDocument (org.apache.solr.common.SolrInputDocument)31 IOException (java.io.IOException)26 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)25 Slice (org.apache.solr.common.cloud.Slice)25 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)24 SolrException (org.apache.solr.common.SolrException)23 SolrServerException (org.apache.solr.client.solrj.SolrServerException)21 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)20 NamedList (org.apache.solr.common.util.NamedList)19 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)18 DocCollection (org.apache.solr.common.cloud.DocCollection)18 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)15 ZkCoreNodeProps (org.apache.solr.common.cloud.ZkCoreNodeProps)15