Search in sources :

Example 76 with SolrClient

use of org.apache.solr.client.solrj.SolrClient in project ddf by codice.

the class RemoteSolrCatalogProviderTest method testShutdown.

@Test
public void testShutdown() throws SolrServerException, IOException {
    boolean clientStatus = true;
    SolrClient givenClient = givenSolrClient(clientStatus);
    RemoteSolrCatalogProvider provider = new MockedRemoteSolrCatalogProvider(givenClient);
    provider.shutdown();
    verify(givenClient, times(1)).close();
}
Also used : SolrClient(org.apache.solr.client.solrj.SolrClient) Test(org.junit.Test)

Example 77 with SolrClient

use of org.apache.solr.client.solrj.SolrClient in project ddf by codice.

the class BackupCommand method performSolrCloudBackup.

private void performSolrCloudBackup() throws Exception {
    SolrClient client = null;
    try {
        client = getCloudSolrClient();
        if (asyncBackupStatus) {
            verifyCloudBackupStatusInput();
            getBackupStatus(client, asyncBackupReqId);
        } else {
            verifyCloudBackupInput();
            performSolrCloudBackup(client);
        }
    } finally {
        shutdown(client);
    }
}
Also used : SolrClient(org.apache.solr.client.solrj.SolrClient)

Example 78 with SolrClient

use of org.apache.solr.client.solrj.SolrClient in project ddf by codice.

the class SolrCommands method getCloudSolrClient.

protected SolrClient getCloudSolrClient() {
    String zkHosts = System.getProperty(ZOOKEEPER_HOSTS_PROP);
    LOGGER.debug("Zookeeper hosts: {}", zkHosts);
    if (zkHosts != null) {
        SolrClient client = new CloudSolrClient.Builder().withZkHost(zkHosts).build();
        LOGGER.debug("Created solr client: {}", client);
        return client;
    } else {
        throw new IllegalArgumentException(String.format("Could not determine Zookeeper Hosts. Please verify that the system property %s is configured in %s.", ZOOKEEPER_HOSTS_PROP, SYSTEM_PROPERTIES_PATH));
    }
}
Also used : SolrClient(org.apache.solr.client.solrj.SolrClient) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient)

Example 79 with SolrClient

use of org.apache.solr.client.solrj.SolrClient in project ddf by codice.

the class PersistentStoreImpl method getSolrClient.

private SolrClient getSolrClient(String storeName) {
    SolrClient solrClient = null;
    Future<SolrClient> clientFuture = solrFutures.computeIfAbsent(storeName, clientFactory::newClient);
    try {
        solrClient = clientFuture.get(5, TimeUnit.SECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        LOGGER.warn("Error getting solr server from future for core: {}", storeName, e);
    }
    return solrClient;
}
Also used : SolrClient(org.apache.solr.client.solrj.SolrClient) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 80 with SolrClient

use of org.apache.solr.client.solrj.SolrClient in project ddf by codice.

the class PersistentStoreImpl method get.

@Override
public // Returned Map will have suffixes in the key names - client is responsible for handling them
List<Map<String, Object>> get(String type, String cql) throws PersistenceException {
    if (StringUtils.isBlank(type)) {
        throw new PersistenceException("The type of object(s) to retrieve must be non-null and not blank, e.g., notification, metacard, etc.");
    }
    List<Map<String, Object>> results = new ArrayList<>();
    // Set Solr Core name to type and create/connect to Solr Core
    SolrClient solrClient = getSolrClient(type);
    if (solrClient == null) {
        throw new PersistenceException("Unable to create Solr client.");
    }
    SolrQueryFilterVisitor visitor = new SolrQueryFilterVisitor(solrClient, type);
    try {
        SolrQuery solrQuery;
        // If not cql specified, then return all items
        if (StringUtils.isBlank(cql)) {
            solrQuery = new SolrQuery("*:*");
        } else {
            Filter filter = CQL.toFilter(cql);
            solrQuery = (SolrQuery) filter.accept(visitor, null);
        }
        QueryResponse solrResponse = solrClient.query(solrQuery, METHOD.POST);
        long numResults = solrResponse.getResults().getNumFound();
        LOGGER.debug("numResults = {}", numResults);
        SolrDocumentList docs = solrResponse.getResults();
        for (SolrDocument doc : docs) {
            PersistentItem result = new PersistentItem();
            Collection<String> fieldNames = doc.getFieldNames();
            for (String name : fieldNames) {
                LOGGER.debug("field name = {} has value = {}", name, doc.getFieldValue(name));
                if (name.endsWith(PersistentItem.TEXT_SUFFIX) && doc.getFieldValues(name).size() > 1) {
                    result.addProperty(name, doc.getFieldValues(name).stream().filter(s -> s instanceof String).map(s -> (String) s).collect(Collectors.toSet()));
                } else if (name.endsWith(PersistentItem.XML_SUFFIX)) {
                    result.addXmlProperty(name, (String) doc.getFirstValue(name));
                } else if (name.endsWith(PersistentItem.TEXT_SUFFIX)) {
                    result.addProperty(name, (String) doc.getFirstValue(name));
                } else if (name.endsWith(PersistentItem.LONG_SUFFIX)) {
                    result.addProperty(name, (Long) doc.getFirstValue(name));
                } else if (name.endsWith(PersistentItem.INT_SUFFIX)) {
                    result.addProperty(name, (Integer) doc.getFirstValue(name));
                } else if (name.endsWith(PersistentItem.DATE_SUFFIX)) {
                    result.addProperty(name, (Date) doc.getFirstValue(name));
                } else if (name.endsWith(PersistentItem.BINARY_SUFFIX)) {
                    result.addProperty(name, (byte[]) doc.getFirstValue(name));
                } else {
                    LOGGER.debug("Not adding field {} because it has invalid suffix", name);
                }
            }
            results.add(result);
        }
    } catch (CQLException e) {
        throw new PersistenceException("CQLException while getting Solr data with cql statement " + cql, e);
    } catch (SolrServerException | IOException e) {
        throw new PersistenceException("SolrServerException while getting Solr data with cql statement " + cql, e);
    }
    return results;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Date(java.util.Date) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrClientFactory(org.codice.solr.factory.SolrClientFactory) SolrClientFactoryImpl(org.codice.solr.factory.impl.SolrClientFactoryImpl) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) ArrayList(java.util.ArrayList) PersistenceException(org.codice.ddf.persistence.PersistenceException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) Future(java.util.concurrent.Future) METHOD(org.apache.solr.client.solrj.SolrRequest.METHOD) CQL(org.geotools.filter.text.cql2.CQL) SolrQueryFilterVisitor(org.codice.solr.query.SolrQueryFilterVisitor) Map(java.util.Map) CQLException(org.geotools.filter.text.cql2.CQLException) Logger(org.slf4j.Logger) PersistentItem(org.codice.ddf.persistence.PersistentItem) Collection(java.util.Collection) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) SolrClient(org.apache.solr.client.solrj.SolrClient) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) SolrDocument(org.apache.solr.common.SolrDocument) List(java.util.List) PersistentStore(org.codice.ddf.persistence.PersistentStore) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Filter(org.opengis.filter.Filter) UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) PersistentItem(org.codice.ddf.persistence.PersistentItem) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) SolrQueryFilterVisitor(org.codice.solr.query.SolrQueryFilterVisitor) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Date(java.util.Date) SolrDocument(org.apache.solr.common.SolrDocument) SolrClient(org.apache.solr.client.solrj.SolrClient) Filter(org.opengis.filter.Filter) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) PersistenceException(org.codice.ddf.persistence.PersistenceException) CQLException(org.geotools.filter.text.cql2.CQLException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

SolrClient (org.apache.solr.client.solrj.SolrClient)172 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)105 Test (org.junit.Test)67 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)38 ArrayList (java.util.ArrayList)36 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)31 SolrQuery (org.apache.solr.client.solrj.SolrQuery)28 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)28 SolrInputDocument (org.apache.solr.common.SolrInputDocument)28 IOException (java.io.IOException)24 NamedList (org.apache.solr.common.util.NamedList)23 SolrServerException (org.apache.solr.client.solrj.SolrServerException)18 SolrException (org.apache.solr.common.SolrException)18 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)17 Map (java.util.Map)16 Replica (org.apache.solr.common.cloud.Replica)16 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)15 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)14 SolrDocument (org.apache.solr.common.SolrDocument)14 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)13