Search in sources :

Example 36 with SolrServerException

use of org.apache.solr.client.solrj.SolrServerException in project nifi by apache.

the class TestPutSolrContentStream method testUpdateWithXml.

@Test
public void testUpdateWithXml() throws IOException, SolrServerException {
    final SolrClient solrClient = createEmbeddedSolrClient(DEFAULT_SOLR_CORE);
    final TestableProcessor proc = new TestableProcessor(solrClient);
    final TestRunner runner = createDefaultTestRunner(proc);
    runner.setProperty(PutSolrContentStream.CONTENT_STREAM_PATH, "/update");
    runner.setProperty(PutSolrContentStream.CONTENT_TYPE, "application/xml");
    try (FileInputStream fileIn = new FileInputStream(XML_MULTIPLE_DOCS_FILE)) {
        runner.enqueue(fileIn);
        runner.run(1, false);
        runner.assertTransferCount(PutSolrContentStream.REL_FAILURE, 0);
        runner.assertTransferCount(PutSolrContentStream.REL_CONNECTION_FAILURE, 0);
        runner.assertTransferCount(PutSolrContentStream.REL_SUCCESS, 1);
        verifySolrDocuments(proc.getSolrClient(), Arrays.asList(expectedDoc1, expectedDoc2));
    } finally {
        try {
            proc.getSolrClient().close();
        } catch (Exception e) {
        }
    }
}
Also used : SolrClient(org.apache.solr.client.solrj.SolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) TestRunner(org.apache.nifi.util.TestRunner) FileInputStream(java.io.FileInputStream) InitializationException(org.apache.nifi.reporting.InitializationException) ProcessException(org.apache.nifi.processor.exception.ProcessException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) Test(org.junit.Test)

Example 37 with SolrServerException

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

the class SolrCatalogProvider method create.

@Override
public CreateResponse create(CreateRequest request) throws IngestException {
    if (request == null) {
        throw new IngestException(REQUEST_MUST_NOT_BE_NULL_MESSAGE);
    }
    List<Metacard> metacards = request.getMetacards();
    List<Metacard> output = new ArrayList<>();
    if (metacards == null) {
        return new CreateResponseImpl(request, null, output);
    }
    for (Metacard metacard : metacards) {
        boolean isSourceIdSet = (metacard.getSourceId() != null && !"".equals(metacard.getSourceId()));
        /*
             * If an ID is not provided, then one is generated so that documents are unique. Solr
             * will not accept documents unless the id is unique.
             */
        if (metacard.getId() == null || metacard.getId().equals("")) {
            if (isSourceIdSet) {
                throw new IngestException("Metacard from a separate distribution must have ID");
            }
            metacard.setAttribute(new AttributeImpl(Metacard.ID, generatePrimaryKey()));
        }
        if (!isSourceIdSet) {
            metacard.setSourceId(getId());
        }
        output.add(metacard);
    }
    try {
        client.add(output, isForcedAutoCommit());
    } catch (SolrServerException | SolrException | IOException | MetacardCreationException e) {
        LOGGER.info("Solr could not ingest metacard(s) during create.", e);
        throw new IngestException("Could not ingest metacard(s).");
    }
    pendingNrtIndex.putAll(output.stream().collect(Collectors.toMap(Metacard::getId, m -> copyMetacard(m))));
    return new CreateResponseImpl(request, request.getProperties(), output);
}
Also used : Metacard(ddf.catalog.data.Metacard) MetacardCreationException(ddf.catalog.data.MetacardCreationException) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException) CreateResponseImpl(ddf.catalog.operation.impl.CreateResponseImpl)

Example 38 with SolrServerException

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

the class SolrCatalogProvider method update.

@Override
public UpdateResponse update(UpdateRequest updateRequest) throws IngestException {
    if (updateRequest == null) {
        throw new IngestException(REQUEST_MUST_NOT_BE_NULL_MESSAGE);
    }
    List<Entry<Serializable, Metacard>> updates = updateRequest.getUpdates();
    // the list of updates, both new and old metacards
    ArrayList<Update> updateList = new ArrayList<>();
    String attributeName = updateRequest.getAttributeName();
    // need an attribute name in order to do query
    if (attributeName == null) {
        throw new IngestException("Attribute name cannot be null. " + "Please provide the name of the attribute.");
    }
    List<String> identifiers = new ArrayList<>();
    // if we have nothing to update, send the empty list
    if (updates == null || updates.size() == 0) {
        return new UpdateResponseImpl(updateRequest, null, new ArrayList<>());
    }
    // Loop to get all identifiers
    for (Entry<Serializable, Metacard> updateEntry : updates) {
        identifiers.add(updateEntry.getKey().toString());
    }
    /* 1a. Create the old Metacard Query */
    String attributeQuery = getQuery(attributeName, identifiers);
    SolrQuery query = new SolrQuery(attributeQuery);
    // Set number of rows to the result size + 1.  The default row size in Solr is 10, so this
    // needs to be set in situations where the number of metacards to update is > 10.  Since there
    // could be more results in the query response than the number of metacards in the update request,
    // 1 is added to the row size, so we can still determine whether we found more metacards than
    // updated metacards provided
    query.setRows(updates.size() + 1);
    QueryResponse idResults = null;
    /* 1b. Execute Query */
    try {
        idResults = solr.query(query, METHOD.POST);
    } catch (SolrServerException | IOException e) {
        LOGGER.info("Failed to query for metacard(s) before update.", e);
    }
    // map of old metacards to be populated
    Map<Serializable, Metacard> idToMetacardMap = new HashMap<>();
    /* 1c. Populate list of old metacards */
    if (idResults != null && idResults.getResults() != null && idResults.getResults().size() != 0) {
        LOGGER.debug("Found {} current metacard(s).", idResults.getResults().size());
        // CHECK updates size assertion
        if (idResults.getResults().size() > updates.size()) {
            throw new IngestException("Found more metacards than updated metacards provided. Please ensure your attribute values match unique records.");
        }
        for (SolrDocument doc : idResults.getResults()) {
            Metacard old;
            try {
                old = client.createMetacard(doc);
            } catch (MetacardCreationException e) {
                LOGGER.info("Unable to create metacard(s) from Solr responses during update.", e);
                throw new IngestException("Could not create metacard(s).");
            }
            if (!idToMetacardMap.containsKey(old.getAttribute(attributeName).getValue())) {
                idToMetacardMap.put(old.getAttribute(attributeName).getValue(), old);
            } else {
                throw new IngestException("The attribute value given [" + old.getAttribute(attributeName).getValue() + "] matched multiple records. Attribute values must at most match only one unique Metacard.");
            }
        }
    }
    if (Metacard.ID.equals(attributeName)) {
        idToMetacardMap.putAll(pendingNrtIndex.getAllPresent(identifiers));
    }
    if (idToMetacardMap.size() == 0) {
        LOGGER.debug("No results found for given attribute values.");
        // return an empty list
        return new UpdateResponseImpl(updateRequest, null, new ArrayList<>());
    }
    /* 2. Update the cards */
    List<Metacard> newMetacards = new ArrayList<>();
    for (Entry<Serializable, Metacard> updateEntry : updates) {
        String localKey = updateEntry.getKey().toString();
        /* 2a. Prepare new Metacard */
        MetacardImpl newMetacard = new MetacardImpl(updateEntry.getValue());
        // Find the exact oldMetacard that corresponds with this newMetacard
        Metacard oldMetacard = idToMetacardMap.get(localKey);
        // matched but another did not
        if (oldMetacard != null) {
            // overwrite the id, in case it has not been done properly/already
            newMetacard.setId(oldMetacard.getId());
            newMetacard.setSourceId(getId());
            newMetacards.add(newMetacard);
            updateList.add(new UpdateImpl(newMetacard, oldMetacard));
        }
    }
    try {
        client.add(newMetacards, isForcedAutoCommit());
    } catch (SolrServerException | SolrException | IOException | MetacardCreationException e) {
        LOGGER.info("Failed to update metacard(s) with Solr.", e);
        throw new IngestException("Failed to update metacard(s).");
    }
    pendingNrtIndex.putAll(updateList.stream().collect(Collectors.toMap(u -> u.getNewMetacard().getId(), u -> copyMetacard(u.getNewMetacard()))));
    return new UpdateResponseImpl(updateRequest, updateRequest.getProperties(), updateList);
}
Also used : UpdateImpl(ddf.catalog.operation.impl.UpdateImpl) Serializable(java.io.Serializable) HashMap(java.util.HashMap) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) Update(ddf.catalog.operation.Update) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Entry(java.util.Map.Entry) SolrDocument(org.apache.solr.common.SolrDocument) UpdateResponseImpl(ddf.catalog.operation.impl.UpdateResponseImpl) IngestException(ddf.catalog.source.IngestException) SolrException(org.apache.solr.common.SolrException) MetacardCreationException(ddf.catalog.data.MetacardCreationException) IOException(java.io.IOException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Metacard(ddf.catalog.data.Metacard) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse)

Example 39 with SolrServerException

use of org.apache.solr.client.solrj.SolrServerException 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)

Example 40 with SolrServerException

use of org.apache.solr.client.solrj.SolrServerException in project xwiki-platform by xwiki.

the class EmbeddedSolrInstance method createCoreContainer.

private CoreContainer createCoreContainer(String solrHome) throws SolrServerException {
    CoreContainer coreContainer = new CoreContainer(solrHome);
    coreContainer.load();
    if (coreContainer.getCores().size() == 0) {
        throw new SolrServerException("Failed to initialize the Solr core. " + "Please check the configuration and log messages.");
    } else if (coreContainer.getCores().size() > 1) {
        this.logger.warn("Multiple Solr cores detected: [{}]. Using the first one.", StringUtils.join(coreContainer.getCoreNames(), ", "));
    }
    return coreContainer;
}
Also used : CoreContainer(org.apache.solr.core.CoreContainer) SolrServerException(org.apache.solr.client.solrj.SolrServerException)

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