Search in sources :

Example 36 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.

the class TestSubQueryTransformerDistrib method test.

@SuppressWarnings("serial")
@Test
public void test() throws SolrServerException, IOException {
    int peopleMultiplier = atLeast(1);
    int deptMultiplier = atLeast(1);
    createIndex(people, peopleMultiplier, depts, deptMultiplier);
    Random random1 = random();
    {
        final QueryRequest qr = new QueryRequest(params(new String[] { "q", "name_s:dave", "indent", "true", "fl", "*,depts:[subquery " + ((random1.nextBoolean() ? "" : "separator=,")) + "]", "rows", "" + peopleMultiplier, "depts.q", "{!terms f=dept_id_s v=$row.dept_ss_dv " + ((random1.nextBoolean() ? "" : "separator=,")) + "}", "depts.fl", "text_t" + (differentUniqueId ? ",id:notid" : ""), "depts.indent", "true", "depts.collection", "departments", differentUniqueId ? "depts.distrib.singlePass" : "notnecessary", "true", "depts.rows", "" + (deptMultiplier * 2), "depts.logParamsList", "q,fl,rows,row.dept_ss_dv", random().nextBoolean() ? "depts.wt" : "whatever", anyWt(), random().nextBoolean() ? "wt" : "whatever", anyWt() }));
        final QueryResponse rsp = new QueryResponse();
        rsp.setResponse(cluster.getSolrClient().request(qr, people));
        final SolrDocumentList hits = rsp.getResults();
        assertEquals(peopleMultiplier, hits.getNumFound());
        int engineerCount = 0;
        int supportCount = 0;
        for (int res : new int[] { 0, (peopleMultiplier - 1) / 2, peopleMultiplier - 1 }) {
            SolrDocument doc = hits.get(res);
            assertEquals("dave", doc.getFieldValue("name_s_dv"));
            SolrDocumentList relDepts = (SolrDocumentList) doc.getFieldValue("depts");
            assertEquals("dave works in both depts " + rsp, deptMultiplier * 2, relDepts.getNumFound());
            for (int deptN = 0; deptN < relDepts.getNumFound(); deptN++) {
                SolrDocument deptDoc = relDepts.get(deptN);
                String actual = (String) deptDoc.get("text_t");
                assertTrue(deptDoc + "should be either " + engineering + " or " + support, (engineering.equals(actual) && ++engineerCount > 0) || (support.equals(actual) && ++supportCount > 0));
            }
        }
        assertEquals(hits.toString(), engineerCount, supportCount);
    }
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) Random(java.util.Random) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Test(org.junit.Test)

Example 37 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.

the class CloudSolrClientTest method testVersionsAreReturned.

@Test
public void testVersionsAreReturned() throws Exception {
    // assert that "adds" are returned
    UpdateRequest updateRequest = new UpdateRequest().add("id", "1", "a_t", "hello1").add("id", "2", "a_t", "hello2");
    updateRequest.setParam(UpdateParams.VERSIONS, Boolean.TRUE.toString());
    NamedList<Object> response = updateRequest.commit(getRandomClient(), COLLECTION).getResponse();
    Object addsObject = response.get("adds");
    assertNotNull("There must be a adds parameter", addsObject);
    assertTrue(addsObject instanceof NamedList<?>);
    NamedList<?> adds = (NamedList<?>) addsObject;
    assertEquals("There must be 2 versions (one per doc)", 2, adds.size());
    Map<String, Long> versions = new HashMap<>();
    Object object = adds.get("1");
    assertNotNull("There must be a version for id 1", object);
    assertTrue("Version for id 1 must be a long", object instanceof Long);
    versions.put("1", (Long) object);
    object = adds.get("2");
    assertNotNull("There must be a version for id 2", object);
    assertTrue("Version for id 2 must be a long", object instanceof Long);
    versions.put("2", (Long) object);
    QueryResponse resp = getRandomClient().query(COLLECTION, new SolrQuery("*:*"));
    assertEquals("There should be one document because overwrite=true", 2, resp.getResults().getNumFound());
    for (SolrDocument doc : resp.getResults()) {
        Long version = versions.get(doc.getFieldValue("id"));
        assertEquals("Version on add must match _version_ field", version, doc.getFieldValue("_version_"));
    }
    // assert that "deletes" are returned
    UpdateRequest deleteRequest = new UpdateRequest().deleteById("1");
    deleteRequest.setParam(UpdateParams.VERSIONS, Boolean.TRUE.toString());
    response = deleteRequest.commit(getRandomClient(), COLLECTION).getResponse();
    Object deletesObject = response.get("deletes");
    assertNotNull("There must be a deletes parameter", deletesObject);
    NamedList deletes = (NamedList) deletesObject;
    assertEquals("There must be 1 version", 1, deletes.size());
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) AbstractUpdateRequest(org.apache.solr.client.solrj.request.AbstractUpdateRequest) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) HashMap(java.util.HashMap) NamedList(org.apache.solr.common.util.NamedList) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 38 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.

the class CloudSolrClientTest method stateVersionParamTest.

@Test
public void stateVersionParamTest() throws Exception {
    DocCollection coll = cluster.getSolrClient().getZkStateReader().getClusterState().getCollection(COLLECTION);
    Replica r = coll.getSlices().iterator().next().getReplicas().iterator().next();
    SolrQuery q = new SolrQuery().setQuery("*:*");
    HttpSolrClient.RemoteSolrException sse = null;
    final String url = r.getStr(ZkStateReader.BASE_URL_PROP) + "/" + COLLECTION;
    try (HttpSolrClient solrClient = getHttpSolrClient(url)) {
        log.info("should work query, result {}", solrClient.query(q));
        //no problem
        q.setParam(CloudSolrClient.STATE_VERSION, COLLECTION + ":" + coll.getZNodeVersion());
        log.info("2nd query , result {}", solrClient.query(q));
        //no error yet good
        //an older version expect error
        q.setParam(CloudSolrClient.STATE_VERSION, COLLECTION + ":" + (coll.getZNodeVersion() - 1));
        QueryResponse rsp = solrClient.query(q);
        Map m = (Map) rsp.getResponse().get(CloudSolrClient.STATE_VERSION, rsp.getResponse().size() - 1);
        assertNotNull("Expected an extra information from server with the list of invalid collection states", m);
        assertNotNull(m.get(COLLECTION));
    }
    //now send the request to another node that does not serve the collection
    Set<String> allNodesOfColl = new HashSet<>();
    for (Slice slice : coll.getSlices()) {
        for (Replica replica : slice.getReplicas()) {
            allNodesOfColl.add(replica.getStr(ZkStateReader.BASE_URL_PROP));
        }
    }
    String theNode = null;
    Set<String> liveNodes = cluster.getSolrClient().getZkStateReader().getClusterState().getLiveNodes();
    for (String s : liveNodes) {
        String n = cluster.getSolrClient().getZkStateReader().getBaseUrlForNodeName(s);
        if (!allNodesOfColl.contains(n)) {
            theNode = n;
            break;
        }
    }
    log.info("the node which does not serve this collection{} ", theNode);
    assertNotNull(theNode);
    final String solrClientUrl = theNode + "/" + COLLECTION;
    try (SolrClient solrClient = getHttpSolrClient(solrClientUrl)) {
        q.setParam(CloudSolrClient.STATE_VERSION, COLLECTION + ":" + (coll.getZNodeVersion() - 1));
        try {
            QueryResponse rsp = solrClient.query(q);
            log.info("error was expected");
        } catch (HttpSolrClient.RemoteSolrException e) {
            sse = e;
        }
        assertNotNull(sse);
        assertEquals(" Error code should be 510", SolrException.ErrorCode.INVALID_STATE.code, sse.code());
    }
}
Also used : SolrClient(org.apache.solr.client.solrj.SolrClient) Slice(org.apache.solr.common.cloud.Slice) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) DocCollection(org.apache.solr.common.cloud.DocCollection) Replica(org.apache.solr.common.cloud.Replica) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) HashMap(java.util.HashMap) SolrQuery(org.apache.solr.client.solrj.SolrQuery) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 39 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.

the class CloudSolrClientTest method queryWithPreferLocalShards.

private void queryWithPreferLocalShards(CloudSolrClient cloudClient, boolean preferLocalShards, String collectionName) throws Exception {
    SolrQuery qRequest = new SolrQuery("*:*");
    ModifiableSolrParams qParams = new ModifiableSolrParams();
    qParams.add(CommonParams.PREFER_LOCAL_SHARDS, Boolean.toString(preferLocalShards));
    qParams.add(ShardParams.SHARDS_INFO, "true");
    qRequest.add(qParams);
    // CloudSolrClient sends the request to some node.
    // And since all the nodes are hosting cores from all shards, the
    // distributed query formed by this node will select cores from the
    // local shards only
    QueryResponse qResponse = cloudClient.query(collectionName, qRequest);
    Object shardsInfo = qResponse.getResponse().get(ShardParams.SHARDS_INFO);
    assertNotNull("Unable to obtain " + ShardParams.SHARDS_INFO, shardsInfo);
    // Iterate over shards-info and check what cores responded
    SimpleOrderedMap<?> shardsInfoMap = (SimpleOrderedMap<?>) shardsInfo;
    Iterator<Map.Entry<String, ?>> itr = shardsInfoMap.asMap(100).entrySet().iterator();
    List<String> shardAddresses = new ArrayList<String>();
    while (itr.hasNext()) {
        Map.Entry<String, ?> e = itr.next();
        assertTrue("Did not find map-type value in " + ShardParams.SHARDS_INFO, e.getValue() instanceof Map);
        String shardAddress = (String) ((Map) e.getValue()).get("shardAddress");
        assertNotNull(ShardParams.SHARDS_INFO + " did not return 'shardAddress' parameter", shardAddress);
        shardAddresses.add(shardAddress);
    }
    log.info("Shards giving the response: " + Arrays.toString(shardAddresses.toArray()));
    // Make sure the distributed queries were directed to a single node only
    if (preferLocalShards) {
        Set<Integer> ports = new HashSet<Integer>();
        for (String shardAddr : shardAddresses) {
            URL url = new URL(shardAddr);
            ports.add(url.getPort());
        }
        // This assertion would hold true as long as every shard has a core on each node
        assertTrue("Response was not received from shards on a single node", shardAddresses.size() > 1 && ports.size() == 1);
    }
}
Also used : ArrayList(java.util.ArrayList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrQuery(org.apache.solr.client.solrj.SolrQuery) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) URL(java.net.URL) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 40 with QueryResponse

use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.

the class SolrExampleJettyTest method testArbitraryJsonIndexing.

@Test
public void testArbitraryJsonIndexing() throws Exception {
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    client.deleteByQuery("*:*");
    client.commit();
    // make sure it got in
    assertNumFound("*:*", 0);
    // two docs, one with uniqueKey, another without it
    String json = "{\"id\":\"abc1\", \"name\": \"name1\"} {\"name\" : \"name2\"}";
    HttpClient httpClient = client.getHttpClient();
    HttpPost post = new HttpPost(getUri(client));
    post.setHeader("Content-Type", "application/json");
    post.setEntity(new InputStreamEntity(new ByteArrayInputStream(json.getBytes("UTF-8")), -1));
    HttpResponse response = httpClient.execute(post, HttpClientUtil.createNewHttpClientRequestContext());
    assertEquals(200, response.getStatusLine().getStatusCode());
    client.commit();
    QueryResponse rsp = getSolrClient().query(new SolrQuery("*:*"));
    assertEquals(2, rsp.getResults().getNumFound());
    SolrDocument doc = rsp.getResults().get(0);
    String src = (String) doc.getFieldValue("_src_");
    Map m = (Map) ObjectBuilder.fromJSON(src);
    assertEquals("abc1", m.get("id"));
    assertEquals("name1", m.get("name"));
    doc = rsp.getResults().get(1);
    src = (String) doc.getFieldValue("_src_");
    m = (Map) ObjectBuilder.fromJSON(src);
    assertEquals("name2", m.get("name"));
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) HttpPost(org.apache.http.client.methods.HttpPost) SolrDocument(org.apache.solr.common.SolrDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.http.client.HttpClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) HttpResponse(org.apache.http.HttpResponse) Map(java.util.Map) SolrQuery(org.apache.solr.client.solrj.SolrQuery) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Test(org.junit.Test)

Aggregations

QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)293 SolrQuery (org.apache.solr.client.solrj.SolrQuery)129 Test (org.junit.Test)111 SolrDocument (org.apache.solr.common.SolrDocument)81 SolrInputDocument (org.apache.solr.common.SolrInputDocument)67 SolrDocumentList (org.apache.solr.common.SolrDocumentList)61 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)58 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)56 SolrServerException (org.apache.solr.client.solrj.SolrServerException)47 ArrayList (java.util.ArrayList)41 IOException (java.io.IOException)39 NamedList (org.apache.solr.common.util.NamedList)32 SolrParams (org.apache.solr.common.params.SolrParams)28 SolrClient (org.apache.solr.client.solrj.SolrClient)27 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)27 ErrorTrackingConcurrentUpdateSolrClient (org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient)25 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)25 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)23 HashMap (java.util.HashMap)22 List (java.util.List)20