Search in sources :

Example 61 with SolrServerException

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);
}
Also used : Field(de.tblsoft.solr.pipeline.bean.Field) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException)

Example 62 with SolrServerException

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;
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) SearchFacetDTO(org.broadleafcommerce.core.search.domain.SearchFacetDTO) Product(org.broadleafcommerce.core.catalog.domain.Product) SearchResult(org.broadleafcommerce.core.search.domain.SearchResult) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ServiceException(org.broadleafcommerce.common.exception.ServiceException) IOException(java.io.IOException) SolrDocument(org.apache.solr.common.SolrDocument) ServiceException(org.broadleafcommerce.common.exception.ServiceException) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Example 63 with SolrServerException

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);
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Dataverse(edu.harvard.iq.dataverse.Dataverse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) AsyncResult(javax.ejb.AsyncResult) ControlledVocabularyValue(edu.harvard.iq.dataverse.ControlledVocabularyValue)

Example 64 with SolrServerException

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;
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) DvObject(edu.harvard.iq.dataverse.DvObject) IOException(java.io.IOException) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 65 with SolrServerException

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");
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) DvObject(edu.harvard.iq.dataverse.DvObject) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Aggregations

SolrServerException (org.apache.solr.client.solrj.SolrServerException)281 IOException (java.io.IOException)210 SolrQuery (org.apache.solr.client.solrj.SolrQuery)101 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)97 ArrayList (java.util.ArrayList)58 SolrException (org.apache.solr.common.SolrException)57 SolrDocument (org.apache.solr.common.SolrDocument)55 SolrInputDocument (org.apache.solr.common.SolrInputDocument)50 SolrDocumentList (org.apache.solr.common.SolrDocumentList)44 HashMap (java.util.HashMap)30 Map (java.util.Map)29 List (java.util.List)27 UpdateResponse (org.apache.solr.client.solrj.response.UpdateResponse)26 SolrClient (org.apache.solr.client.solrj.SolrClient)23 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)23 NamedList (org.apache.solr.common.util.NamedList)22 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)19 Date (java.util.Date)18 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)17 SolrParams (org.apache.solr.common.params.SolrParams)13