Example usage for org.apache.solr.client.solrj.impl CloudSolrClient getDefaultCollection

List of usage examples for org.apache.solr.client.solrj.impl CloudSolrClient getDefaultCollection

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.impl CloudSolrClient getDefaultCollection.

Prototype

public String getDefaultCollection() 

Source Link

Document

Gets the default collection for request

Usage

From source file:io.logspace.hq.core.solr.event.SolrEventService.java

License:Open Source License

private String getTargetShard(Date timestamp) {
    if (!this.isCloud) {
        return null;
    }/*from   w  w w. j a  v a  2  s.  c  o  m*/

    CloudSolrClient cloudSolrClient = (CloudSolrClient) this.solrClient;

    if (System.currentTimeMillis() > this.nextSliceUpdate) {
        this.nextSliceUpdate = System.currentTimeMillis() + SLICE_UPDATE_INTERVAL;
        this.activeSlicesMap = cloudSolrClient.getZkStateReader().getClusterState()
                .getCollection(cloudSolrClient.getDefaultCollection()).getActiveSlicesMap();
    }

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(timestamp);
    String sliceName = MessageFormat.format("{0,number,0000}-{1,number,00}", calendar.get(YEAR),
            calendar.get(MONTH) + 1);

    if (this.activeSlicesMap.containsKey(sliceName)) {
        return sliceName;
    }

    return this.fallbackShard;
}

From source file:io.logspace.hq.solr.SolrEventService.java

License:Open Source License

private String getTargetShard(Date timestamp) {
    if (!this.isCloud) {
        return null;
    }// w ww.j av a  2s  . c  om

    CloudSolrClient cloudSolrClient = (CloudSolrClient) this.solrClient;

    if (System.currentTimeMillis() > this.nextSliceUpdate) {
        this.nextSliceUpdate = System.currentTimeMillis() + SLICE_UPDATE_INTERVAL;
        this.activeSlicesMap = cloudSolrClient.getZkStateReader().getClusterState()
                .getActiveSlicesMap(cloudSolrClient.getDefaultCollection());
    }

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(timestamp);
    String sliceName = MessageFormat.format("{0,number,0000}-{1,number,00}", calendar.get(YEAR),
            calendar.get(MONTH) + 1);

    if (this.activeSlicesMap.containsKey(sliceName)) {
        return sliceName;
    }

    return this.fallbackShard;
}

From source file:io.redlink.solrlib.cloud.SolrCloudConnectorTest.java

License:Apache License

@Test
public void testCreateClient() throws Exception {
    final String zkConnection = UUID.randomUUID().toString();
    SolrCloudConnectorConfiguration config = new SolrCloudConnectorConfiguration();
    config.setZkConnection(zkConnection);
    SolrCloudConnector connector = new SolrCloudConnector(Collections.<SolrCoreDescriptor>emptySet(), config,
            null);// w ww.ja  va  2 s. c  om

    CloudSolrClient c1 = connector.createSolrClient();
    assertThat(c1.getZkHost(), Matchers.equalTo(zkConnection));

    for (int i = 0; i < 10; i++) {
        final String coreName = UUID.randomUUID().toString();
        SolrClient cI = connector.createSolrClient(coreName);
        assertThat(cI, Matchers.instanceOf(CloudSolrClient.class));
        CloudSolrClient c2 = (CloudSolrClient) cI;
        assertThat(c2.getZkHost(), Matchers.equalTo(zkConnection));
        assertThat(c2.getDefaultCollection(), Matchers.equalTo(coreName));
    }
}

From source file:org.broadleafcommerce.core.search.service.solr.SolrConfiguration.java

License:Open Source License

/**
 * Sets the primary SolrClient instance to communicate with Solr.  This is typically one of the following:
 * <code>org.apache.solr.client.solrj.embedded.EmbeddedSolrClient</code>,
 * <code>org.apache.solr.client.solrj.impl.HttpSolrClient</code>,
 * <code>org.apache.solr.client.solrj.impl.LBHttpSolrClient</code>,
 * or <code>org.apache.solr.client.solrj.impl.CloudSolrClient</code>
 * //w ww.  j  a v  a  2 s . c o m
 * @param server
 * @throws IllegalStateException 
 */
public void setServer(SolrClient server) throws IllegalStateException {
    if (server != null && CloudSolrClient.class.isAssignableFrom(server.getClass())) {
        CloudSolrClient cs = (CloudSolrClient) server;
        if (StringUtils.isBlank(cs.getDefaultCollection())) {
            cs.setDefaultCollection(getPrimaryName());
        }

        if (reindexServer != null) {
            //If we already have a reindex server set, make sure it's not the same instance as the primary
            if (server == reindexServer) {
                throw new IllegalArgumentException(
                        "The primary and reindex CloudSolrServers are the same instances. "
                                + "They must be different instances. Each instance must have a different defaultCollection or "
                                + "the defaultCollection must be unspecified and Broadleaf will set it.");
            }

            if (CloudSolrClient.class.isAssignableFrom(reindexServer.getClass())) {
                //Make sure that the primary and reindex servers are not using the same default collection name
                if (cs.getDefaultCollection()
                        .equals(((CloudSolrClient) reindexServer).getDefaultCollection())) {
                    throw new IllegalStateException(
                            "Primary and Reindex servers cannot have the same defaultCollection: "
                                    + cs.getDefaultCollection());
                }
            }
        }
    }

    primaryServer = server;
}

From source file:org.broadleafcommerce.core.search.service.solr.SolrConfiguration.java

License:Open Source License

/**
 * Sets the SolrClient instance that points to the reindex core for the purpose of doing a full reindex, while the
 * primary core is still serving serving requests.  This is typically one of the following: 
 * <code>org.apache.solr.client.solrj.embedded.EmbeddedSolrServer</code>, 
 * <code>org.apache.solr.client.solrj.impl.HttpSolrServer</code>, 
 * <code>org.apache.solr.client.solrj.impl.LBHttpSolrServer</code>, 
 * or <code>org.apache.solr.client.solrj.impl.CloudSolrClient</code>
 * //from w  w  w . j  a v  a 2 s  .c  o  m
 * @param server
 * @throws IllegalStateException 
 */
public void setReindexServer(SolrClient server) throws IllegalStateException {
    if (server != null && CloudSolrClient.class.isAssignableFrom(server.getClass())) {
        CloudSolrClient cs = (CloudSolrClient) server;
        if (StringUtils.isBlank(cs.getDefaultCollection())) {
            cs.setDefaultCollection(getReindexName());
        }

        if (primaryServer != null) {
            //If we already have a reindex server set, make sure it's not the same instance as the primary
            if (server == primaryServer) {
                throw new IllegalArgumentException(
                        "The primary and reindex CloudSolrServers are the same instances. "
                                + "They must be different instances. Each instance must have a different defaultCollection or "
                                + "the defaultCollection must be unspecified and Broadleaf will set it.");
            }

            if (CloudSolrClient.class.isAssignableFrom(primaryServer.getClass())) {
                //Make sure that the primary and reindex servers are not using the same default collection name
                if (cs.getDefaultCollection()
                        .equals(((CloudSolrClient) primaryServer).getDefaultCollection())) {
                    throw new IllegalStateException(
                            "Primary and Reindex servers cannot have the same defaultCollection: "
                                    + cs.getDefaultCollection());
                }
            }
        }
    }
    reindexServer = server;
}

From source file:org.broadleafcommerce.core.search.service.solr.SolrConfiguration.java

License:Open Source License

@Override
public void afterPropertiesSet() throws SolrServerException, IOException, IllegalStateException {
    if (CloudSolrClient.class.isAssignableFrom(getServer().getClass())) {
        //We want to use the Solr APIs to make sure the correct collections are set up.
        CloudSolrClient primary = (CloudSolrClient) primaryServer;
        CloudSolrClient reindex = (CloudSolrClient) reindexServer;
        if (primary == null || reindex == null) {
            throw new IllegalStateException("The primary and reindex CloudSolrServers must not be null. Check "
                    + "your configuration and ensure that you are passing a different instance for each to the "
                    + "constructor of " + this.getClass().getName() + " and ensure that each has a null (empty)"
                    + " defaultCollection property, or ensure that defaultCollection is unique between"
                    + " the two instances. All other things, like Zookeeper addresses should be the same.");
        }//ww  w  . j ava2 s  .co  m

        if (primary == reindex) {
            //These are the same object instances.  They should be separate instances, with generally 
            //the same configuration, except for the defaultCollection name.
            throw new IllegalStateException(
                    "The primary and reindex CloudSolrServers must be different instances "
                            + "and their defaultCollection property must be unique or null.  All other things like the "
                            + "Zookeeper addresses should be the same.");
        }

        //check if the default collection is null
        if (StringUtils.isEmpty(primary.getDefaultCollection())) {
            throw new IllegalStateException(
                    "The primary CloudSolrServer must have a defaultCollection property set.");
        } else {
            this.setPrimaryName(primary.getDefaultCollection());
        }

        //check if the default collection is null
        if (StringUtils.isEmpty(reindex.getDefaultCollection())) {
            throw new IllegalStateException(
                    "The reindex CloudSolrServer must have a defaultCollection property set.");
        } else {
            this.setReindexName(reindex.getDefaultCollection());
        }

        if (Objects.equals(primary.getDefaultCollection(), reindex.getDefaultCollection())) {
            throw new IllegalStateException("The primary and reindex CloudSolrServers must have "
                    + "unique defaultCollection properties.  All other things like the "
                    + "Zookeeper addresses should be the same.");
        }

        primary.connect(); //This is required to ensure no NPE!

        //Get a list of existing collections so we don't overwrite one
        NamedList<Object> listResponse = new CollectionAdminRequest.List().process(primary).getResponse();
        List<String> collectionNames = listResponse.get("collections") == null
                ? collectionNames = new ArrayList<String>()
                : (List<String>) listResponse.get("collections");

        Aliases aliases = primary.getZkStateReader().getAliases();
        Map<String, String> aliasCollectionMap = aliases.getCollectionAliasMap();

        if (aliasCollectionMap == null || !aliasCollectionMap.containsKey(primary.getDefaultCollection())) {
            //Create a completely new collection
            String collectionName = null;
            for (int i = 0; i < 1000; i++) {
                collectionName = "blcCollection" + i;
                if (collectionNames.contains(collectionName)) {
                    collectionName = null;
                } else {
                    break;
                }
            }

            new CollectionAdminRequest.Create().setCollectionName(collectionName)
                    .setNumShards(solrCloudNumShards).setConfigName(solrCloudConfigName).process(primary);

            new CollectionAdminRequest.CreateAlias().setAliasName(primary.getDefaultCollection())
                    .setAliasedCollections(collectionName).process(primary);
        } else {
            //Aliases can be mapped to collections that don't exist.... Make sure the collection exists
            String collectionName = aliasCollectionMap.get(primary.getDefaultCollection());
            collectionName = collectionName.split(",")[0];
            if (!collectionNames.contains(collectionName)) {
                new CollectionAdminRequest.Create().setCollectionName(collectionName)
                        .setNumShards(solrCloudNumShards).setConfigName(solrCloudConfigName).process(primary);
            }
        }

        //Reload the collection names
        listResponse = new CollectionAdminRequest.List().process(primary).getResponse();
        collectionNames = listResponse.get("collections") == null ? collectionNames = new ArrayList<String>()
                : (List<String>) listResponse.get("collections");

        //Reload these maps for the next collection.
        aliases = primary.getZkStateReader().getAliases();
        aliasCollectionMap = aliases.getCollectionAliasMap();

        if (aliasCollectionMap == null || !aliasCollectionMap.containsKey(reindex.getDefaultCollection())) {
            //Create a completely new collection
            String collectionName = null;
            for (int i = 0; i < 1000; i++) {
                collectionName = "blcCollection" + i;
                if (collectionNames.contains(collectionName)) {
                    collectionName = null;
                } else {
                    break;
                }
            }

            new CollectionAdminRequest.Create().setCollectionName(collectionName)
                    .setNumShards(solrCloudNumShards).setConfigName(solrCloudConfigName).process(primary);

            new CollectionAdminRequest.CreateAlias().setAliasName(reindex.getDefaultCollection())
                    .setAliasedCollections(collectionName).process(primary);
        } else {
            //Aliases can be mapped to collections that don't exist.... Make sure the collection exists
            String collectionName = aliasCollectionMap.get(reindex.getDefaultCollection());
            collectionName = collectionName.split(",")[0];
            if (!collectionNames.contains(collectionName)) {
                new CollectionAdminRequest.Create().setCollectionName(collectionName)
                        .setNumShards(solrCloudNumShards).setConfigName(solrCloudConfigName).process(primary);
            }
        }
    }
}

From source file:org.springframework.data.solr.server.support.SolrClientUtilTests.java

License:Apache License

@Test
@SuppressWarnings("unchecked")
public void testClonesCloudSolrClientForCoreCorrectlyWhenCoreNameIsNotEmpty() throws MalformedURLException {
    LBHttpSolrClient lbSolrClient = new LBHttpSolrClient(BASE_URL, ALTERNATE_BASE_URL);
    CloudSolrClient cloudClient = new CloudSolrClient(ZOO_KEEPER_URL, lbSolrClient);

    CloudSolrClient clone = SolrClientUtils.clone(cloudClient, CORE_NAME);
    Assert.assertEquals(ZOO_KEEPER_URL, ReflectionTestUtils.getField(clone, FIELD_ZOO_KEEPER));

    LBHttpSolrClient lbClone = clone.getLbClient();
    Map<String, ?> aliveServers = (Map<String, ?>) ReflectionTestUtils.getField(lbClone, FIELD_ALIVE_SERVERS);
    Assert.assertThat(aliveServers.keySet(), hasItems(CORE_URL, ALTERNATE_CORE_URL));

    assertLBHttpSolrClientProperties(lbSolrClient, lbClone);
    Assert.assertThat(clone.getDefaultCollection(), equalTo(CORE_NAME));
}

From source file:org.springframework.data.solr.server.support.SolrClientUtilTests.java

License:Apache License

@Test
public void testClonesCloudSolrClientForCoreCorrectlyWhenNoLBHttpServerPresent() throws MalformedURLException {
    CloudSolrClient cloudClient = new CloudSolrClient(ZOO_KEEPER_URL);

    CloudSolrClient clone = SolrClientUtils.clone(cloudClient, CORE_NAME);
    Assert.assertEquals(ZOO_KEEPER_URL, ReflectionTestUtils.getField(clone, FIELD_ZOO_KEEPER));

    LBHttpSolrClient lbClone = clone.getLbClient();

    assertLBHttpSolrClientProperties(cloudClient.getLbClient(), lbClone);
    Assert.assertThat(clone.getDefaultCollection(), equalTo(CORE_NAME));
}