use of org.apache.solr.client.solrj.SolrServerException in project solr-cmd-utils by tblsoft.
the class SolrFeeder method document.
@Override
public void document(Document document) {
SolrInputDocument inputDoc = new SolrInputDocument();
for (Field field : document.getFields()) {
if (!isFieldIgnored(field.getName())) {
inputDoc.addField(field.getName(), field.getValues());
}
}
try {
server.add(inputDoc);
} catch (SolrServerException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
super.document(document);
}
use of org.apache.solr.client.solrj.SolrServerException in project BroadleafCommerce by BroadleafCommerce.
the class SolrSearchServiceImpl method findSearchResults.
/**
* Given a qualified solr query string (such as "category:2002"), actually performs a solr search. It will
* take into considering the search criteria to build out facets / pagination / sorting.
*
* @param searchCriteria
* @param facets
* @return the ProductSearchResult of the search
* @throws ServiceException
*/
protected SearchResult findSearchResults(String qualifiedSolrQuery, List<SearchFacetDTO> facets, SearchCriteria searchCriteria, String defaultSort, String... filterQueries) throws ServiceException {
Map<String, SearchFacetDTO> namedFacetMap = getNamedFacetMap(facets, searchCriteria);
// Left here for backwards compatibility for this method signature
if (searchCriteria.getQuery() == null && qualifiedSolrQuery != null) {
searchCriteria.setQuery(qualifiedSolrQuery);
}
// Build the basic query
// Solr queries with a 'start' parameter cannot be a negative number
int start = (searchCriteria.getPage() <= 0) ? 0 : (searchCriteria.getPage() - 1);
SolrQuery solrQuery = new SolrQuery().setQuery(searchCriteria.getQuery()).setRows(searchCriteria.getPageSize()).setStart((start) * searchCriteria.getPageSize()).setRequestHandler(searchCriteria.getRequestHandler());
// This is for SolrCloud. We assume that we are always searching against a collection aliased as "PRIMARY"
if (solrConfiguration.isSiteCollections()) {
solrQuery.setParam("collection", solrConfiguration.getSiteAliasName(BroadleafRequestContext.getBroadleafRequestContext().getNonPersistentSite()));
} else {
// This should be ignored if not using SolrCloud
solrQuery.setParam("collection", solrConfiguration.getPrimaryName());
}
solrQuery.setFields(shs.getIndexableIdFieldName());
if (filterQueries != null) {
solrQuery.setFilterQueries(filterQueries);
}
// add category filter if applicable
if (searchCriteria.getCategory() != null) {
solrQuery.addFilterQuery(getCategoryFilter(searchCriteria));
}
solrQuery.addFilterQuery(shs.getNamespaceFieldName() + ":(\"" + solrConfiguration.getNamespace() + "\")");
solrQuery.set("defType", "edismax");
solrQuery.set("qf", buildQueryFieldsString(solrQuery, searchCriteria));
// Attach additional restrictions
attachActiveFacetFilters(solrQuery, namedFacetMap, searchCriteria);
attachFacets(solrQuery, namedFacetMap, searchCriteria);
modifySolrQuery(solrQuery, searchCriteria.getQuery(), facets, searchCriteria, defaultSort);
// on child documents.
if (StringUtils.isNotBlank(defaultSort) || StringUtils.isNotBlank(searchCriteria.getSortQuery())) {
solrQuery.remove("bq");
solrQuery.remove("bf");
solrQuery.remove("boost");
}
attachSortClause(solrQuery, searchCriteria, defaultSort);
solrQuery.setShowDebugInfo(shouldShowDebugQuery());
if (LOG.isTraceEnabled()) {
try {
LOG.trace(URLDecoder.decode(solrQuery.toString(), "UTF-8"));
} catch (Exception e) {
LOG.trace("Couldn't UTF-8 URL Decode: " + solrQuery.toString());
}
}
// Query solr
QueryResponse response;
List<SolrDocument> responseDocuments;
int numResults = 0;
try {
response = solrConfiguration.getServer().query(solrQuery, getSolrQueryMethod());
responseDocuments = getResponseDocuments(response);
numResults = (int) response.getResults().getNumFound();
if (LOG.isTraceEnabled()) {
LOG.trace(response.toString());
for (SolrDocument doc : responseDocuments) {
LOG.trace(doc);
}
}
} catch (SolrServerException e) {
throw new ServiceException("Could not perform search", e);
} catch (IOException e) {
throw new ServiceException("Could not perform search", e);
}
// Get the facets
setFacetResults(namedFacetMap, response);
sortFacetResults(namedFacetMap);
filterEmptyFacets(facets);
SearchResult result = new SearchResult();
result.setFacets(facets);
result.setQueryResponse(response);
setPagingAttributes(result, numResults, searchCriteria);
if (useSku) {
List<Sku> skus = getSkus(responseDocuments);
result.setSkus(skus);
} else {
// Get the products
List<Product> products = getProducts(responseDocuments);
result.setProducts(products);
}
return result;
}
use of org.apache.solr.client.solrj.SolrServerException in project dataverse by IQSS.
the class IndexServiceBean method indexDataverse.
public Future<String> indexDataverse(Dataverse dataverse) {
logger.fine("indexDataverse called on dataverse id " + dataverse.getId() + "(" + dataverse.getAlias() + ")");
if (dataverse.getId() == null) {
String msg = "unable to index dataverse. id was null (alias: " + dataverse.getAlias() + ")";
logger.info(msg);
return new AsyncResult<>(msg);
}
Dataverse rootDataverse = findRootDataverseCached();
if (rootDataverse == null) {
String msg = "Could not find root dataverse and the root dataverse should not be indexed. Returning.";
return new AsyncResult<>(msg);
} else if (dataverse.getId() == rootDataverse.getId()) {
String msg = "The root dataverse should not be indexed. Returning.";
return new AsyncResult<>(msg);
}
Collection<SolrInputDocument> docs = new ArrayList<>();
SolrInputDocument solrInputDocument = new SolrInputDocument();
solrInputDocument.addField(SearchFields.ID, solrDocIdentifierDataverse + dataverse.getId());
solrInputDocument.addField(SearchFields.ENTITY_ID, dataverse.getId());
solrInputDocument.addField(SearchFields.DATAVERSE_VERSION_INDEXED_BY, systemConfig.getVersion());
solrInputDocument.addField(SearchFields.IDENTIFIER, dataverse.getAlias());
solrInputDocument.addField(SearchFields.TYPE, "dataverses");
solrInputDocument.addField(SearchFields.NAME, dataverse.getName());
solrInputDocument.addField(SearchFields.NAME_SORT, dataverse.getName());
solrInputDocument.addField(SearchFields.DATAVERSE_NAME, dataverse.getName());
solrInputDocument.addField(SearchFields.DATAVERSE_CATEGORY, dataverse.getIndexableCategoryName());
if (dataverse.isReleased()) {
solrInputDocument.addField(SearchFields.PUBLICATION_STATUS, PUBLISHED_STRING);
solrInputDocument.addField(SearchFields.RELEASE_OR_CREATE_DATE, dataverse.getPublicationDate());
solrInputDocument.addField(SearchFields.RELEASE_OR_CREATE_DATE_SEARCHABLE_TEXT, convertToFriendlyDate(dataverse.getPublicationDate()));
} else {
solrInputDocument.addField(SearchFields.PUBLICATION_STATUS, UNPUBLISHED_STRING);
solrInputDocument.addField(SearchFields.RELEASE_OR_CREATE_DATE, dataverse.getCreateDate());
solrInputDocument.addField(SearchFields.RELEASE_OR_CREATE_DATE_SEARCHABLE_TEXT, convertToFriendlyDate(dataverse.getCreateDate()));
}
/* We don't really have harvested dataverses yet;
(I have in fact just removed the isHarvested() method from the Dataverse object) -- L.A.
if (dataverse.isHarvested()) {
solrInputDocument.addField(SearchFields.IS_HARVESTED, true);
solrInputDocument.addField(SearchFields.SOURCE, HARVESTED);
} else { (this means that all dataverses are "local" - should this be removed? */
solrInputDocument.addField(SearchFields.IS_HARVESTED, false);
// rootDataverseName);
solrInputDocument.addField(SearchFields.METADATA_SOURCE, findRootDataverseCached().getName());
/*}*/
addDataverseReleaseDateToSolrDoc(solrInputDocument, dataverse);
// if (dataverse.getOwner() != null) {
// solrInputDocument.addField(SearchFields.HOST_DATAVERSE, dataverse.getOwner().getName());
// }
solrInputDocument.addField(SearchFields.DESCRIPTION, StringUtil.html2text(dataverse.getDescription()));
solrInputDocument.addField(SearchFields.DATAVERSE_DESCRIPTION, StringUtil.html2text(dataverse.getDescription()));
// logger.info("dataverse affiliation: " + dataverse.getAffiliation());
if (dataverse.getAffiliation() != null && !dataverse.getAffiliation().isEmpty()) {
/**
* @todo: stop using affiliation as category
*/
// solrInputDocument.addField(SearchFields.CATEGORY, dataverse.getAffiliation());
solrInputDocument.addField(SearchFields.AFFILIATION, dataverse.getAffiliation());
solrInputDocument.addField(SearchFields.DATAVERSE_AFFILIATION, dataverse.getAffiliation());
}
for (ControlledVocabularyValue dataverseSubject : dataverse.getDataverseSubjects()) {
String subject = dataverseSubject.getStrValue();
if (!subject.equals(DatasetField.NA_VALUE)) {
solrInputDocument.addField(SearchFields.DATAVERSE_SUBJECT, subject);
// collapse into shared "subject" field used as a facet
solrInputDocument.addField(SearchFields.SUBJECT, subject);
}
}
// checking for NPE is important so we can create the root dataverse
if (rootDataverse != null && !dataverse.equals(rootDataverse)) {
// important when creating root dataverse
if (dataverse.getOwner() != null) {
solrInputDocument.addField(SearchFields.PARENT_ID, dataverse.getOwner().getId());
solrInputDocument.addField(SearchFields.PARENT_NAME, dataverse.getOwner().getName());
}
}
List<String> dataversePathSegmentsAccumulator = new ArrayList<>();
List<String> dataverseSegments = findPathSegments(dataverse, dataversePathSegmentsAccumulator);
List<String> dataversePaths = getDataversePathsFromSegments(dataverseSegments);
if (dataversePaths.size() > 0) {
// don't show yourself while indexing or in search results: https://redmine.hmdc.harvard.edu/issues/3613
// logger.info(dataverse.getName() + " size " + dataversePaths.size());
dataversePaths.remove(dataversePaths.size() - 1);
}
// Add paths for linking dataverses
for (Dataverse linkingDataverse : dvLinkingService.findLinkingDataverses(dataverse.getId())) {
List<String> linkingDataversePathSegmentsAccumulator = new ArrayList<>();
List<String> linkingdataverseSegments = findPathSegments(linkingDataverse, linkingDataversePathSegmentsAccumulator);
List<String> linkingDataversePaths = getDataversePathsFromSegments(linkingdataverseSegments);
for (String dvPath : linkingDataversePaths) {
dataversePaths.add(dvPath);
}
}
solrInputDocument.addField(SearchFields.SUBTREE, dataversePaths);
docs.add(solrInputDocument);
String status;
try {
if (dataverse.getId() != null) {
solrServer.add(docs);
} else {
logger.info("WARNING: indexing of a dataverse with no id attempted");
}
} catch (SolrServerException | IOException ex) {
status = ex.toString();
logger.info(status);
return new AsyncResult<>(status);
}
try {
solrServer.commit();
} catch (SolrServerException | IOException ex) {
status = ex.toString();
logger.info(status);
return new AsyncResult<>(status);
}
dvObjectService.updateContentIndexTime(dataverse);
IndexResponse indexResponse = solrIndexService.indexPermissionsForOneDvObject(dataverse);
String msg = "indexed dataverse " + dataverse.getId() + ":" + dataverse.getAlias() + ". Response from permission indexing: " + indexResponse.getMessage();
return new AsyncResult<>(msg);
}
use of org.apache.solr.client.solrj.SolrServerException in project dataverse by IQSS.
the class IndexServiceBean method findFilesOfParentDataset.
private List<String> findFilesOfParentDataset(long parentDatasetId) throws SearchException {
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery("*");
solrQuery.setRows(Integer.MAX_VALUE);
solrQuery.addFilterQuery(SearchFields.PARENT_ID + ":" + parentDatasetId);
/**
* @todo "files" should be a constant
*/
solrQuery.addFilterQuery(SearchFields.TYPE + ":" + "files");
List<String> dvObjectInSolrOnly = new ArrayList<>();
QueryResponse queryResponse = null;
try {
queryResponse = solrServer.query(solrQuery);
} catch (SolrServerException | IOException ex) {
throw new SearchException("Error searching Solr for dataset parent id " + parentDatasetId, ex);
}
SolrDocumentList results = queryResponse.getResults();
for (SolrDocument solrDocument : results) {
Object idObject = solrDocument.getFieldValue(SearchFields.ID);
if (idObject != null) {
String id = (String) idObject;
dvObjectInSolrOnly.add(id);
}
}
return dvObjectInSolrOnly;
}
use of org.apache.solr.client.solrj.SolrServerException in project dataverse by IQSS.
the class SolrIndexServiceBean method indexPermissionsForOneDvObject.
public IndexResponse indexPermissionsForOneDvObject(DvObject dvObject) {
if (dvObject == null) {
return new IndexResponse("problem indexing... null DvObject passed in");
}
long dvObjectId = dvObject.getId();
Collection<SolrInputDocument> docs = new ArrayList<>();
List<DvObjectSolrDoc> definitionPoints = determineSolrDocs(dvObject);
for (DvObjectSolrDoc dvObjectSolrDoc : definitionPoints) {
SolrInputDocument solrInputDocument = SearchUtil.createSolrDoc(dvObjectSolrDoc);
docs.add(solrInputDocument);
}
try {
persistToSolr(docs);
boolean updatePermissionTimeSuccessful = false;
if (dvObject != null) {
DvObject savedDvObject = dvObjectService.updatePermissionIndexTime(dvObject);
if (savedDvObject != null) {
updatePermissionTimeSuccessful = true;
}
}
return new IndexResponse("attempted to index permissions for DvObject " + dvObjectId + " and updatePermissionTimeSuccessful was " + updatePermissionTimeSuccessful);
} catch (SolrServerException | IOException ex) {
return new IndexResponse("problem indexing");
}
}
Aggregations