Search in sources :

Example 56 with SolrQuery

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

the class TestSolrEntityProcessorUnit method testPagingQuery.

public void testPagingQuery() {
    SolrEntityProcessor processor = new NoNextMockProcessor();
    HashMap<String, String> entityAttrs = new HashMap<String, String>() {

        {
            put(SolrEntityProcessor.SOLR_SERVER, "http://route:66/no");
            if (random().nextBoolean()) {
                //only 'true' not '*'
                List<String> noCursor = Arrays.asList("", "false", CursorMarkParams.CURSOR_MARK_START);
                Collections.shuffle(noCursor, random());
                put(CursorMarkParams.CURSOR_MARK_PARAM, noCursor.get(0));
            }
        }
    };
    processor.init(getContext(null, null, null, null, Collections.emptyList(), entityAttrs));
    try {
        processor.buildIterator();
        SolrQuery query = new SolrQuery();
        ((SolrDocumentListIterator) processor.rowIterator).passNextPage(query);
        assertEquals("0", query.get(CommonParams.START));
        assertNull(query.get(CursorMarkParams.CURSOR_MARK_PARAM));
        assertNotNull(query.get(CommonParams.TIME_ALLOWED));
    } finally {
        processor.destroy();
    }
}
Also used : SolrDocumentListIterator(org.apache.solr.handler.dataimport.SolrEntityProcessor.SolrDocumentListIterator) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 57 with SolrQuery

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

the class DistributedIntervalFacetingTest method testSolrJ.

private void testSolrJ() throws Exception {
    indexr("id", "0", "test_i_dv", "0", "test_s_dv", "AAA");
    indexr("id", "1", "test_i_dv", "1", "test_s_dv", "BBB");
    indexr("id", "2", "test_i_dv", "2", "test_s_dv", "AAA");
    indexr("id", "3", "test_i_dv", "3", "test_s_dv", "CCC");
    commit();
    QueryResponse response = controlClient.query(new SolrQuery("*:*"));
    assertEquals(4, response.getResults().getNumFound());
    SolrQuery q = new SolrQuery("*:*");
    String[] intervals = new String[] { "[0,1)", "[1,2)", "[2,3)", "[3,*)" };
    q.addIntervalFacets("test_i_dv", intervals);
    response = controlClient.query(q);
    assertEquals(1, response.getIntervalFacets().size());
    assertEquals("test_i_dv", response.getIntervalFacets().get(0).getField());
    assertEquals(4, response.getIntervalFacets().get(0).getIntervals().size());
    for (int i = 0; i < response.getIntervalFacets().get(0).getIntervals().size(); i++) {
        Count count = response.getIntervalFacets().get(0).getIntervals().get(i);
        assertEquals(intervals[i], count.getKey());
        assertEquals(1, count.getCount());
    }
    q = new SolrQuery("*:*");
    q.addIntervalFacets("test_i_dv", intervals);
    q.addIntervalFacets("test_s_dv", new String[] { "{!key='AAA'}[AAA,AAA]", "{!key='BBB'}[BBB,BBB]", "{!key='CCC'}[CCC,CCC]" });
    response = controlClient.query(q);
    assertEquals(2, response.getIntervalFacets().size());
    int stringIntervalIndex = "test_s_dv".equals(response.getIntervalFacets().get(0).getField()) ? 0 : 1;
    assertEquals("test_i_dv", response.getIntervalFacets().get(1 - stringIntervalIndex).getField());
    assertEquals("test_s_dv", response.getIntervalFacets().get(stringIntervalIndex).getField());
    for (int i = 0; i < response.getIntervalFacets().get(1 - stringIntervalIndex).getIntervals().size(); i++) {
        Count count = response.getIntervalFacets().get(1 - stringIntervalIndex).getIntervals().get(i);
        assertEquals(intervals[i], count.getKey());
        assertEquals(1, count.getCount());
    }
    List<Count> stringIntervals = response.getIntervalFacets().get(stringIntervalIndex).getIntervals();
    assertEquals(3, stringIntervals.size());
    assertEquals("AAA", stringIntervals.get(0).getKey());
    assertEquals(2, stringIntervals.get(0).getCount());
    assertEquals("BBB", stringIntervals.get(1).getKey());
    assertEquals(1, stringIntervals.get(1).getCount());
    assertEquals("CCC", stringIntervals.get(2).getKey());
    assertEquals(1, stringIntervals.get(2).getCount());
}
Also used : QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Count(org.apache.solr.client.solrj.response.IntervalFacet.Count) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 58 with SolrQuery

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

the class SolrCmdDistributorTest method testOneRetry.

private void testOneRetry() throws Exception {
    final HttpSolrClient solrclient = (HttpSolrClient) clients.get(0);
    long numFoundBefore = solrclient.query(new SolrQuery("*:*")).getResults().getNumFound();
    final MockStreamingSolrClients streamingClients = new MockStreamingSolrClients(updateShardHandler);
    SolrCmdDistributor cmdDistrib = new SolrCmdDistributor(streamingClients, 5, 0);
    streamingClients.setExp(Exp.CONNECT_EXCEPTION);
    ArrayList<Node> nodes = new ArrayList<>();
    ZkNodeProps nodeProps = new ZkNodeProps(ZkStateReader.BASE_URL_PROP, solrclient.getBaseURL(), ZkStateReader.CORE_NAME_PROP, "");
    final AtomicInteger retries = new AtomicInteger();
    nodeProps = new ZkNodeProps(ZkStateReader.BASE_URL_PROP, solrclient.getBaseURL(), ZkStateReader.CORE_NAME_PROP, "");
    RetryNode retryNode = new RetryNode(new ZkCoreNodeProps(nodeProps), null, "collection1", "shard1") {

        @Override
        public boolean checkRetry() {
            streamingClients.setExp(null);
            retries.incrementAndGet();
            return true;
        }
    };
    nodes.add(retryNode);
    AddUpdateCommand cmd = new AddUpdateCommand(null);
    cmd.solrDoc = sdoc("id", id.incrementAndGet());
    ModifiableSolrParams params = new ModifiableSolrParams();
    CommitUpdateCommand ccmd = new CommitUpdateCommand(null, false);
    cmdDistrib.distribAdd(cmd, nodes, params);
    cmdDistrib.distribCommit(ccmd, nodes, params);
    cmdDistrib.finish();
    assertEquals(1, retries.get());
    long numFoundAfter = solrclient.query(new SolrQuery("*:*")).getResults().getNumFound();
    // we will get java.net.ConnectException which we retry on
    assertEquals(numFoundBefore + 1, numFoundAfter);
    assertEquals(0, cmdDistrib.getErrors().size());
}
Also used : RetryNode(org.apache.solr.update.SolrCmdDistributor.RetryNode) ZkCoreNodeProps(org.apache.solr.common.cloud.ZkCoreNodeProps) RetryNode(org.apache.solr.update.SolrCmdDistributor.RetryNode) StdNode(org.apache.solr.update.SolrCmdDistributor.StdNode) Node(org.apache.solr.update.SolrCmdDistributor.Node) ZkNodeProps(org.apache.solr.common.cloud.ZkNodeProps) ArrayList(java.util.ArrayList) SolrQuery(org.apache.solr.client.solrj.SolrQuery) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 59 with SolrQuery

use of org.apache.solr.client.solrj.SolrQuery 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 60 with SolrQuery

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

Aggregations

SolrQuery (org.apache.solr.client.solrj.SolrQuery)327 Test (org.junit.Test)174 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)129 SolrServerException (org.apache.solr.client.solrj.SolrServerException)53 SolrInputDocument (org.apache.solr.common.SolrInputDocument)52 SolrDocument (org.apache.solr.common.SolrDocument)47 ArrayList (java.util.ArrayList)46 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)45 IOException (java.io.IOException)36 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)33 SolrDocumentList (org.apache.solr.common.SolrDocumentList)33 SolrClient (org.apache.solr.client.solrj.SolrClient)27 LinearModel (org.apache.solr.ltr.model.LinearModel)24 Map (java.util.Map)23 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)23 Replica (org.apache.solr.common.cloud.Replica)23 Slice (org.apache.solr.common.cloud.Slice)23 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)21 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)19 SolrException (org.apache.solr.common.SolrException)18