Search in sources :

Example 41 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class TestCollectionAPI method clusterStatusWithCollectionAndShard.

private void clusterStatusWithCollectionAndShard() throws IOException, SolrServerException {
    try (CloudSolrClient client = createCloudClient(null)) {
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("action", CollectionParams.CollectionAction.CLUSTERSTATUS.toString());
        params.set("collection", COLLECTION_NAME);
        params.set("shard", SHARD1);
        SolrRequest request = new QueryRequest(params);
        request.setPath("/admin/collections");
        NamedList<Object> rsp = client.request(request);
        NamedList<Object> cluster = (NamedList<Object>) rsp.get("cluster");
        assertNotNull("Cluster state should not be null", cluster);
        NamedList<Object> collections = (NamedList<Object>) cluster.get("collections");
        assertNotNull("Collections should not be null in cluster state", collections);
        assertNotNull(collections.get(COLLECTION_NAME));
        assertEquals(1, collections.size());
        Map<String, Object> collection = (Map<String, Object>) collections.get(COLLECTION_NAME);
        Map<String, Object> shardStatus = (Map<String, Object>) collection.get("shards");
        assertEquals(1, shardStatus.size());
        Map<String, Object> selectedShardStatus = (Map<String, Object>) shardStatus.get(SHARD1);
        assertNotNull(selectedShardStatus);
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) NamedList(org.apache.solr.common.util.NamedList) SolrRequest(org.apache.solr.client.solrj.SolrRequest) HashMap(java.util.HashMap) Map(java.util.Map) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient)

Example 42 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class TestConfigSetsAPI method createCollection.

protected CollectionAdminResponse createCollection(String collectionName, String confSetName, int numShards, int replicationFactor, SolrClient client) throws SolrServerException, IOException {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("action", CollectionAction.CREATE.toString());
    params.set("collection.configName", confSetName);
    params.set("name", collectionName);
    params.set("numShards", numShards);
    params.set("replicationFactor", replicationFactor);
    SolrRequest request = new QueryRequest(params);
    request.setPath("/admin/collections");
    CollectionAdminResponse res = new CollectionAdminResponse();
    res.setResponse(client.request(request));
    return res;
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) CollectionAdminResponse(org.apache.solr.client.solrj.response.CollectionAdminResponse) SolrRequest(org.apache.solr.client.solrj.SolrRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 43 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class ShowFileRequestHandlerTest method testGetRawFile.

public void testGetRawFile() throws SolrServerException, IOException {
    SolrClient client = getSolrClient();
    //assertQ(req("qt", "/admin/file")); TODO file bug that SolrJettyTestBase extends SolrTestCaseJ4
    QueryRequest request = new QueryRequest(params("file", "managed-schema"));
    request.setPath("/admin/file");
    final AtomicBoolean readFile = new AtomicBoolean();
    request.setResponseParser(new ResponseParser() {

        @Override
        public String getWriterType() {
            //unfortunately this gets put onto params wt=mock but it apparently has no effect
            return "mock";
        }

        @Override
        public NamedList<Object> processResponse(InputStream body, String encoding) {
            try {
                if (body.read() >= 0)
                    readFile.set(true);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return null;
        }

        @Override
        public NamedList<Object> processResponse(Reader reader) {
            //TODO
            throw new UnsupportedOperationException("TODO unimplemented");
        }
    });
    //runs request
    client.request(request);
    //request.process(client); but we don't have a NamedList response
    assertTrue(readFile.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) ResponseParser(org.apache.solr.client.solrj.ResponseParser) SolrClient(org.apache.solr.client.solrj.SolrClient) InputStream(java.io.InputStream) NamedList(org.apache.solr.common.util.NamedList) Reader(java.io.Reader) IOException(java.io.IOException)

Example 44 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class FacetStream method open.

public void open() throws IOException {
    if (cache != null) {
        cloudSolrClient = cache.getCloudSolrClient(zkHost);
    } else {
        cloudSolrClient = new Builder().withZkHost(zkHost).build();
    }
    FieldComparator[] adjustedSorts = adjustSorts(buckets, bucketSorts);
    String json = getJsonFacetString(buckets, metrics, adjustedSorts, bucketSizeLimit);
    ModifiableSolrParams paramsLoc = new ModifiableSolrParams(params);
    paramsLoc.set("json.facet", json);
    paramsLoc.set("rows", "0");
    QueryRequest request = new QueryRequest(paramsLoc);
    try {
        NamedList response = cloudSolrClient.request(request, collection);
        getTuples(response, buckets, metrics);
        Collections.sort(tuples, getStreamSort());
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) NamedList(org.apache.solr.common.util.NamedList) Builder(org.apache.solr.client.solrj.impl.CloudSolrClient.Builder) FieldComparator(org.apache.solr.client.solrj.io.comp.FieldComparator) MultipleFieldComparator(org.apache.solr.client.solrj.io.comp.MultipleFieldComparator) IOException(java.io.IOException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) IOException(java.io.IOException)

Example 45 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest 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)

Aggregations

QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)112 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)77 SolrRequest (org.apache.solr.client.solrj.SolrRequest)35 NamedList (org.apache.solr.common.util.NamedList)35 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)29 Test (org.junit.Test)28 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)21 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)17 Map (java.util.Map)17 SolrQuery (org.apache.solr.client.solrj.SolrQuery)17 IOException (java.io.IOException)16 SolrException (org.apache.solr.common.SolrException)15 SolrInputDocument (org.apache.solr.common.SolrInputDocument)14 SolrClient (org.apache.solr.client.solrj.SolrClient)13 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)12 List (java.util.List)11 SolrServerException (org.apache.solr.client.solrj.SolrServerException)8 RemoteSolrException (org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException)8 SolrDocumentList (org.apache.solr.common.SolrDocumentList)8