Search in sources :

Example 91 with SolrServerException

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

the class SolrYard method getRepresentation.

@Override
public final Representation getRepresentation(String id) throws YardException {
    if (id == null) {
        throw new IllegalArgumentException("The parsed Representation id MUST NOT be NULL!");
    }
    if (id.isEmpty()) {
        throw new IllegalArgumentException("The parsed Representation id MUST NOT be empty!");
    }
    if (closed) {
        log.warn("The SolrYard '{}' was already closed!", config.getName());
    }
    SolrDocument doc;
    long start = System.currentTimeMillis();
    try {
        doc = getSolrDocument(id);
    } catch (SolrServerException e) {
        throw new YardException("Error while getting SolrDocument for id" + id, e);
    } catch (IOException e) {
        throw new YardException("Unable to access SolrServer", e);
    }
    long retrieve = System.currentTimeMillis();
    Representation rep;
    if (doc != null) {
        // create an Representation for the Doc! retrieve
        log.debug(String.format("Create Representation %s from SolrDocument", doc.getFirstValue(fieldMapper.getDocumentIdField())));
        rep = createRepresentation(doc, null);
    } else {
        rep = null;
    }
    long create = System.currentTimeMillis();
    log.debug(String.format("  ... %s [retrieve=%dms|create=%dms|sum=%dms]", rep == null ? "not found" : "done", (retrieve - start), (create - retrieve), (create - start)));
    return rep;
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) IOException(java.io.IOException)

Example 92 with SolrServerException

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

the class SolrYard method findReferences.

@Override
public final QueryResultList<String> findReferences(FieldQuery parsedQuery) throws YardException {
    // create a clone of the query, because we need to refine it because the
    // query (as executed) needs to be included in the result set
    FieldQuery fieldQuery = parsedQuery.clone();
    final SolrQuery query = solrQueryFactoy.parseFieldQuery(fieldQuery, SELECT.ID);
    if (closed) {
        log.warn("The SolrYard '{}' was already closed!", config.getName());
    }
    QueryResponse response;
    try {
        response = AccessController.doPrivileged(new PrivilegedExceptionAction<QueryResponse>() {

            public QueryResponse run() throws IOException, SolrServerException {
                return server.query(query, METHOD.POST);
            }
        });
    } catch (PrivilegedActionException pae) {
        Exception e = pae.getException();
        if (e instanceof SolrServerException) {
            throw new YardException("Error while performing query on the SolrServer (query: " + query.getQuery() + ")!", e);
        } else if (e instanceof IOException) {
            throw new YardException("Unable to access SolrServer", e);
        } else {
            throw RuntimeException.class.cast(e);
        }
    }
    // return a queryResultList
    return new QueryResultListImpl<String>(fieldQuery, // by adapting SolrDocuments to Representations
    new AdaptingIterator<SolrDocument, String>(response.getResults().iterator(), // inline Adapter Implementation
    new AdaptingIterator.Adapter<SolrDocument, String>() {

        @Override
        public String adapt(SolrDocument doc, Class<String> type) {
            // use this method for the conversion!
            return doc.getFirstValue(fieldMapper.getDocumentIdField()).toString();
        }
    }, String.class), String.class);
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) PrivilegedActionException(java.security.PrivilegedActionException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrServerException(org.apache.solr.client.solrj.SolrServerException) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) NoConverterException(org.apache.stanbol.entityhub.yard.solr.model.NoConverterException) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl)

Example 93 with SolrServerException

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

the class SolrYard method store.

@Override
public final Iterable<Representation> store(Iterable<Representation> representations) throws IllegalArgumentException, YardException {
    if (representations == null) {
        throw new IllegalArgumentException("The parsed Representations MUST NOT be NULL!");
    }
    Collection<Representation> added = new HashSet<Representation>();
    long start = System.currentTimeMillis();
    Collection<SolrInputDocument> inputDocs = new HashSet<SolrInputDocument>();
    for (Representation representation : representations) {
        if (representation != null) {
            inputDocs.add(createSolrInputDocument(representation));
            added.add(representation);
        }
    }
    if (inputDocs.isEmpty()) {
        // empty data sent ... nothing to do
        log.debug("strore called with empty collection of Representations");
        return representations;
    }
    long created = System.currentTimeMillis();
    if (closed) {
        log.warn("The SolrYard '{}' was already closed!", config.getName());
    }
    final UpdateRequest update = new UpdateRequest();
    if (!immediateCommit) {
        update.setCommitWithin(commitWithin);
    }
    update.add(inputDocs);
    try {
        AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

            public Object run() throws IOException, SolrServerException {
                update.process(server);
                if (immediateCommit) {
                    server.commit();
                }
                return null;
            }
        });
        long ready = System.currentTimeMillis();
        log.debug(String.format("Processed store request for %d documents in %dms (created %dms| stored%dms)", inputDocs.size(), ready - start, created - start, ready - created));
    } catch (PrivilegedActionException pae) {
        if (pae.getException() instanceof SolrServerException) {
            throw new YardException("Exception while adding Documents to the Solr Server!", pae.getException());
        } else if (pae.getException() instanceof IOException) {
            throw new YardException("Unable to access SolrServer", pae.getException());
        } else {
            throw RuntimeException.class.cast(pae.getException());
        }
    }
    return added;
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) PrivilegedActionException(java.security.PrivilegedActionException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) IOException(java.io.IOException) SolrInputDocument(org.apache.solr.common.SolrInputDocument) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) HashSet(java.util.HashSet)

Example 94 with SolrServerException

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

the class SolrYard method find.

private QueryResultList<Representation> find(final FieldQuery parsedQuery, SELECT select) throws YardException {
    // create a clone of the query, because we need to refine it because the
    // query (as executed) needs to be included in the result set
    FieldQuery fieldQuery = parsedQuery.clone();
    log.debug("find " + fieldQuery);
    long start = System.currentTimeMillis();
    final Set<String> selected;
    if (select == SELECT.QUERY) {
        // if query set the fields to add to the result Representations
        selected = new HashSet<String>(fieldQuery.getSelectedFields());
        // add the score to query results!
        selected.add(RdfResourceEnum.resultScore.getUri());
    } else {
        // otherwise add all fields
        selected = null;
    }
    final SolrQuery query = solrQueryFactoy.parseFieldQuery(fieldQuery, select);
    long queryGeneration = System.currentTimeMillis();
    if (closed) {
        log.warn("The SolrYard '{}' was already closed!", config.getName());
    }
    QueryResponse response;
    try {
        response = AccessController.doPrivileged(new PrivilegedExceptionAction<QueryResponse>() {

            public QueryResponse run() throws IOException, SolrServerException {
                StreamQueryRequest request = new StreamQueryRequest(query);
                return request.process(server);
            }
        });
    } catch (PrivilegedActionException pae) {
        Exception e = pae.getException();
        if (e instanceof SolrServerException) {
            if ("unknown handler: /mlt".equals(e.getCause().getMessage())) {
                throw new YardException("Solr is missing '<requestHandler name=\"/mlt\"" + " class=\"solr.MoreLikeThisHandler\" startup=\"lazy\" />'" + " in 'solrconfig.xml'", e);
            }
            throw new YardException("Error while performing Query on SolrServer: " + query.getQuery(), e);
        } else if (e instanceof IOException) {
            throw new YardException("Unable to access SolrServer", e);
        } else {
            throw RuntimeException.class.cast(e);
        }
    }
    if (SolrQueryFactory.MLT_QUERY_TYPE.equals(query.getRequestHandler())) {
        log.debug("{}", response);
    }
    long queryTime = System.currentTimeMillis();
    // return a queryResultList
    QueryResultListImpl<Representation> resultList = new QueryResultListImpl<Representation>(fieldQuery, // by adapting SolrDocuments to Representations
    new AdaptingIterator<SolrDocument, Representation>(response.getResults().iterator(), // inline Adapter Implementation
    new AdaptingIterator.Adapter<SolrDocument, Representation>() {

        @Override
        public Representation adapt(SolrDocument doc, Class<Representation> type) {
            // use this method for the conversion!
            return createRepresentation(doc, selected);
        }
    }, Representation.class), Representation.class);
    long resultProcessing = System.currentTimeMillis();
    log.debug(String.format("  ... done [queryGeneration=%dms|queryTime=%dms|resultProcessing=%dms|sum=%dms]", (queryGeneration - start), (queryTime - queryGeneration), (resultProcessing - queryTime), (resultProcessing - start)));
    return resultList;
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) PrivilegedActionException(java.security.PrivilegedActionException) StreamQueryRequest(org.apache.stanbol.commons.solr.utils.StreamQueryRequest) SolrServerException(org.apache.solr.client.solrj.SolrServerException) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrServerException(org.apache.solr.client.solrj.SolrServerException) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) NoConverterException(org.apache.stanbol.entityhub.yard.solr.model.NoConverterException) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl)

Example 95 with SolrServerException

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

the class SolrClient method delete.

/**
 * Delete a record from the database.
 *
 * @param table
 *          The name of the table
 * @param key
 *          The record key of the record to delete.
 * @return Zero on success, a non-zero error code on error. See this class's description for a
 *         discussion of error codes.
 */
@Override
public Status delete(String table, String key) {
    try {
        UpdateResponse response;
        if (batchMode) {
            response = client.deleteById(table, key, commitTime);
        } else {
            response = client.deleteById(table, key);
            client.commit(table);
        }
        return checkStatus(response.getStatus());
    } catch (IOException | SolrServerException e) {
        e.printStackTrace();
    }
    return Status.ERROR;
}
Also used : UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrServerException(org.apache.solr.client.solrj.SolrServerException) 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