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));
}
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;
}
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();
}
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);
}
}
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);
}
}
Aggregations