Search in sources :

Example 6 with SolrClient

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

the class ReindexCommandTest method testReindex.

@Test
public void testReindex() throws Exception {
    ThreadContext.bind(mock(Subject.class));
    SolrClient cloudClient = mock(SolrClient.class);
    NamedList<Object> pingStatus = new NamedList<>();
    pingStatus.add("status", "OK");
    when(cloudClient.isAvailable()).thenReturn(true);
    QueryResponse hitCountResponse = mock(QueryResponse.class);
    SolrDocumentList hitCountResults = mock(SolrDocumentList.class);
    when(hitCountResults.getNumFound()).thenReturn(1L);
    when(hitCountResponse.getResults()).thenReturn(hitCountResults);
    SolrDocument doc = new SolrDocument();
    doc.put("id_txt", "1234");
    SolrDocumentList dataDocumentList = new SolrDocumentList();
    dataDocumentList.add(doc);
    dataDocumentList.setNumFound(1L);
    QueryResponse dataResponse = mock(QueryResponse.class);
    when(dataResponse.getResults()).thenReturn(dataDocumentList);
    when(dataResponse.getNextCursorMark()).thenReturn("cursor1234");
    SolrDocumentList emptyDocList = new SolrDocumentList();
    dataDocumentList.add(doc);
    QueryResponse emptyResponse = mock(QueryResponse.class);
    when(emptyResponse.getResults()).thenReturn(emptyDocList);
    when(cloudClient.query(any(SolrQuery.class))).thenReturn(hitCountResponse, dataResponse, emptyResponse);
    SolrMetacardClientImpl solrMetacardClient = mock(SolrMetacardClientImpl.class);
    when(solrMetacardClient.createMetacard(any())).thenReturn(getTestMetacard());
    CreateResponse createResponse = mock(CreateResponse.class);
    CatalogFramework catalogFramework = mock(CatalogFramework.class);
    when(catalogFramework.create(any(CreateRequest.class))).thenReturn(createResponse);
    Security security = mock(Security.class);
    Subject subject = mock(Subject.class);
    when(security.runAsAdmin(any())).thenReturn(subject);
    when(subject.execute(any(Callable.class))).thenAnswer(c -> ((Callable) c.getArguments()[0]).call());
    ReindexCommand command = new ReindexCommand();
    command.setSolrjClient(cloudClient);
    command.setMetacardClient(solrMetacardClient);
    command.setNumThread(1);
    command.setCollection("catalog");
    command.setSolrHost("http://localhost:8994/solr");
    command.setCatalogFramework(catalogFramework);
    command.security = security;
    command.execute();
    verify(catalogFramework, times(1)).create(any(CreateRequest.class));
}
Also used : NamedList(org.apache.solr.common.util.NamedList) CreateResponse(ddf.catalog.operation.CreateResponse) CreateRequest(ddf.catalog.operation.CreateRequest) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Security(org.codice.ddf.security.Security) Subject(ddf.security.Subject) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Callable(java.util.concurrent.Callable) SolrDocument(org.apache.solr.common.SolrDocument) SolrClient(org.codice.solr.client.solrj.SolrClient) SolrMetacardClientImpl(ddf.catalog.source.solr.SolrMetacardClientImpl) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) CatalogFramework(ddf.catalog.CatalogFramework) Test(org.junit.Test)

Example 7 with SolrClient

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

the class AbstractSolrClientCommand method executeWithSubject.

@SuppressWarnings({ "java:S2139" /* Logging and rethrowing failure exception intentionally */
})
@Override
protected Object executeWithSubject() throws Exception {
    if (!force) {
        String answer = session.readLine("Are you sure you want to continue? (y/n): ", null).toLowerCase();
        if (!("y".equalsIgnoreCase(answer) || "yes".equalsIgnoreCase(answer))) {
            console.println("Aborting.");
            return null;
        }
    }
    try (SolrClient solrClient = clientFactory.newClient(COLLECTION_NAME)) {
        Callable<Boolean> booleanCallable = solrClient::isAvailable;
        boolean response = Failsafe.with(new RetryPolicy().retryWhen(false).withMaxDuration(5, TimeUnit.SECONDS).withBackoff(25, 1_000, TimeUnit.MILLISECONDS)).get(booleanCallable);
        if (!response) {
            LOGGER.error("Could not contact solr");
            printErrorMessage("Could not contact solr, exiting.");
            return null;
        }
        executeWithSolrClient(solrClient);
    } catch (SolrServerException | IOException e) {
        // Note that this will also catch failures closing the SolrClient
        LOGGER.info("Error while executing", e);
        printErrorMessage("Error while executing.");
        throw e;
    }
    printSuccessMessage("Success");
    return null;
}
Also used : SolrClient(org.codice.solr.client.solrj.SolrClient) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 8 with SolrClient

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

the class PersistentStoreImpl method delete.

@Override
public int delete(String type, String cql, int startIndex, int pageSize) throws PersistenceException {
    List<Map<String, Object>> itemsToDelete = this.get(type, cql, startIndex, pageSize);
    SolrClient solrClient = getSolrClient(type);
    List<String> idsToDelete = new ArrayList<>();
    for (Map<String, Object> item : itemsToDelete) {
        String uuid = (String) item.get(PersistentItem.ID);
        if (StringUtils.isNotBlank(uuid)) {
            idsToDelete.add(uuid);
        }
    }
    if (!idsToDelete.isEmpty()) {
        try {
            LOGGER.debug("Deleting {} items by ID", idsToDelete.size());
            solrClient.deleteById(idsToDelete);
        } catch (SolrServerException | SolrException | IOException e) {
            LOGGER.info("Exception while trying to delete items by ID for persistent type {}", type, e);
            doRollback(solrClient, type);
            throw new PersistenceException("Exception while trying to delete items by ID for persistent type " + type, e);
        } catch (RuntimeException e) {
            LOGGER.info("RuntimeException while trying to delete items by ID for persistent type {}", type, e);
            doRollback(solrClient, type);
            throw new PersistenceException("RuntimeException while trying to delete items by ID for persistent type " + type, e);
        }
    }
    return idsToDelete.size();
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SolrClient(org.codice.solr.client.solrj.SolrClient) PersistenceException(org.codice.ddf.persistence.PersistenceException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SolrException(org.apache.solr.common.SolrException)

Example 9 with SolrClient

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

the class PersistentStoreImpl method add.

@Override
public void add(String type, Collection<Map<String, Object>> items) throws PersistenceException {
    LOGGER.debug("type = {}", type);
    if (StringUtils.isEmpty(type)) {
        throw new PersistenceException("The type of object(s) to be added must be non-null and not blank, e.g., notification, metacard, etc.");
    }
    if (CollectionUtils.isEmpty(items)) {
        return;
    }
    // Set Solr Core name to type and create solr client
    SolrClient solrClient = getSolrClient(type);
    List<SolrInputDocument> inputDocuments = new ArrayList<>();
    for (Map<String, Object> properties : items) {
        if (MapUtils.isEmpty(properties)) {
            continue;
        }
        LOGGER.debug("Adding entry of type {}", type);
        SolrInputDocument solrInputDocument = new SolrInputDocument();
        solrInputDocument.addField("createddate_tdt", new Date());
        for (Map.Entry<String, Object> entry : properties.entrySet()) {
            solrInputDocument.addField(entry.getKey(), entry.getValue());
        }
        inputDocuments.add(solrInputDocument);
    }
    if (inputDocuments.isEmpty()) {
        return;
    }
    try {
        UpdateResponse response = solrClient.add(inputDocuments, commitNrtCommitWithinMs);
        LOGGER.debug("UpdateResponse from add of SolrInputDocument:  {}", response);
    } catch (SolrServerException | SolrException | IOException e) {
        LOGGER.info("Exception while adding Solr index for persistent type {}", type, e);
        doRollback(solrClient, type);
        throw new PersistenceException("Exception while adding Solr index for persistent type " + type, e);
    } catch (RuntimeException e) {
        LOGGER.info("RuntimeException while adding Solr index for persistent type {}", type, e);
        doRollback(solrClient, type);
        throw new PersistenceException("RuntimeException while adding Solr index for persistent type " + type, e);
    }
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Date(java.util.Date) UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrClient(org.codice.solr.client.solrj.SolrClient) PersistenceException(org.codice.ddf.persistence.PersistenceException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SolrException(org.apache.solr.common.SolrException)

Example 10 with SolrClient

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

the class PersistentStoreImpl method get.

@Override
public List<Map<String, Object>> get(String type, String cql, int startIndex, int pageSize) 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.");
    }
    if (startIndex < 0) {
        throw new IllegalArgumentException("The start index must be nonnegative.");
    }
    if (pageSize <= 0 || pageSize > MAX_PAGE_SIZE) {
        throw new IllegalArgumentException(String.format("The page size must be greater than 0 and less than or equal to %d.", MAX_PAGE_SIZE));
    }
    // Set Solr Core name to type and create/connect to Solr Core
    SolrClient solrClient = getSolrClient(type);
    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 = ECQL.toFilter(cql);
            solrQuery = (SolrQuery) filter.accept(visitor, null);
        }
        if (solrQuery == null) {
            throw new PersistenceException("Unsupported query " + cql);
        }
        solrQuery.setRows(pageSize);
        solrQuery.setStart(startIndex);
        solrQuery.addSort(PersistentItem.ID, SolrQuery.ORDER.asc);
        QueryResponse solrResponse = solrClient.query(solrQuery, METHOD.POST);
        long numResults = solrResponse.getResults().getNumFound();
        LOGGER.debug("numResults = {}", numResults);
        final SolrDocumentList docs = solrResponse.getResults();
        return documentListToResultList(docs);
    } catch (CQLException e) {
        throw new PersistenceException("CQLException while getting Solr data with cql statement " + cql, e);
    } catch (SolrServerException | SolrException | IOException e) {
        throw new PersistenceException("Exception while getting Solr data with cql statement " + cql, e);
    }
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrQueryFilterVisitor(org.codice.solr.query.SolrQueryFilterVisitor) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrClient(org.codice.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) SolrException(org.apache.solr.common.SolrException)

Aggregations

SolrClient (org.codice.solr.client.solrj.SolrClient)12 Test (org.junit.Test)7 IOException (java.io.IOException)4 SolrServerException (org.apache.solr.client.solrj.SolrServerException)4 SolrException (org.apache.solr.common.SolrException)3 NamedList (org.apache.solr.common.util.NamedList)3 PersistenceException (org.codice.ddf.persistence.PersistenceException)3 CatalogProvider (ddf.catalog.source.CatalogProvider)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 SolrQuery (org.apache.solr.client.solrj.SolrQuery)2 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)2 UpdateResponse (org.apache.solr.client.solrj.response.UpdateResponse)2 SolrDocumentList (org.apache.solr.common.SolrDocumentList)2 CatalogFramework (ddf.catalog.CatalogFramework)1 CreateRequest (ddf.catalog.operation.CreateRequest)1 CreateResponse (ddf.catalog.operation.CreateResponse)1 SourceMonitor (ddf.catalog.source.SourceMonitor)1 SolrMetacardClientImpl (ddf.catalog.source.solr.SolrMetacardClientImpl)1