Search in sources :

Example 16 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 17 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 18 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 19 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)

Example 20 with QueryRequest

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

the class MergeIndexesExampleTestBase method setupCores.

private UpdateRequest setupCores() throws SolrServerException, IOException {
    UpdateRequest up = new UpdateRequest();
    up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
    up.deleteByQuery("*:*");
    up.process(getSolrCore0());
    up.process(getSolrCore1());
    up.clear();
    // Add something to each core
    SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", "AAA");
    doc.setField("name", "core0");
    // Add to core0
    up.add(doc);
    up.process(getSolrCore0());
    // Add to core1
    doc.setField("id", "BBB");
    doc.setField("name", "core1");
    up.add(doc);
    up.process(getSolrCore1());
    // Now Make sure AAA is in 0 and BBB in 1
    SolrQuery q = new SolrQuery();
    QueryRequest r = new QueryRequest(q);
    q.setQuery("id:AAA");
    assertEquals(1, r.process(getSolrCore0()).getResults().size());
    assertEquals(0, r.process(getSolrCore1()).getResults().size());
    assertEquals(1, getSolrCore0().query(new SolrQuery("id:AAA")).getResults().size());
    assertEquals(0, getSolrCore0().query(new SolrQuery("id:BBB")).getResults().size());
    assertEquals(0, getSolrCore1().query(new SolrQuery("id:AAA")).getResults().size());
    assertEquals(1, getSolrCore1().query(new SolrQuery("id:BBB")).getResults().size());
    return up;
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) AbstractUpdateRequest(org.apache.solr.client.solrj.request.AbstractUpdateRequest) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest)

Aggregations

QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)110 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)75 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