Search in sources :

Example 11 with QueryResultListImpl

use of org.apache.stanbol.entityhub.core.query.QueryResultListImpl in project stanbol by apache.

the class SiteManagerImpl method find.

@Override
public QueryResultList<Representation> find(FieldQuery query) {
    log.debug("find with query{}", query);
    Set<Representation> representations = new HashSet<Representation>();
    //TODO: The QueryResultList expects that the query as executed is added
    //to the response. However when executing queries on multiple site they
    //might support a different set of features and therefore execute
    //different variants. For now I return simple the query as executed by
    //the first Site that contributes results
    FieldQuery processedQuery = null;
    FieldQuery queryWithResults = null;
    for (Site site : referencedSites) {
        if (site.supportsSearch()) {
            log.debug(" > query site {}", site.getId());
            try {
                QueryResultList<Representation> results = site.find(query);
                if (processedQuery == null) {
                    processedQuery = results.getQuery();
                }
                if (!results.isEmpty() && queryWithResults == null) {
                    processedQuery = results.getQuery();
                }
                for (Representation rep : results) {
                    if (!representations.contains(rep)) {
                        //do not override
                        representations.add(rep);
                    } else {
                        log.info("Entity {} found on more than one Referenced Site" + " -> Representation of Site {} is ignored", rep.getId(), site.getConfiguration().getName());
                    }
                }
            } catch (SiteException e) {
                log.warn("Unable to access Site " + site.getConfiguration().getName() + " (id = " + site.getId() + ")", e);
            }
        } else {
            log.debug(" > Site {} does not support queries", site.getId());
        }
    }
    return new QueryResultListImpl<Representation>(//use the query with results
    queryWithResults != null ? //use the query with results
    queryWithResults : //if not a processed
    processedQuery != null ? //if not a processed
    processedQuery : //else the parsed one
    query, representations, Representation.class);
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Site(org.apache.stanbol.entityhub.servicesapi.site.Site) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) HashSet(java.util.HashSet)

Example 12 with QueryResultListImpl

use of org.apache.stanbol.entityhub.core.query.QueryResultListImpl in project stanbol by apache.

the class SparqlSearcher method findEntities.

@Override
public final QueryResultList<String> findEntities(FieldQuery parsedQuery) throws IOException {
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    String sparqlQuery = query.toSparqlSelect(false);
    InputStream in = SparqlEndpointUtils.sendSparqlRequest(getQueryUri(), sparqlQuery, DEFAULT_SPARQL_RESULT_CONTENT_TYPE);
    //Move to util class!
    final List<String> entities = extractEntitiesFromJsonResult(in, query.getRootVariableName());
    return new QueryResultListImpl<String>(query, entities.iterator(), String.class);
}
Also used : InputStream(java.io.InputStream) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl)

Example 13 with QueryResultListImpl

use of org.apache.stanbol.entityhub.core.query.QueryResultListImpl in project stanbol by apache.

the class YardSite method findEntities.

@Override
public QueryResultList<Entity> findEntities(FieldQuery query) throws ManagedSiteException {
    QueryResultList<Representation> results;
    try {
        results = getYard().findRepresentation(query);
    } catch (YardException e) {
        throw new ManagedSiteException(e.getMessage(), e);
    }
    return new QueryResultListImpl<Entity>(results.getQuery(), new AdaptingIterator<Representation, Entity>(results.iterator(), new AdaptingIterator.Adapter<Representation, Entity>() {

        private final String siteId = config.getId();

        @Override
        public Entity adapt(Representation value, Class<Entity> type) {
            Entity entity = new EntityImpl(siteId, value, null);
            SiteUtils.initEntityMetadata(entity, siteMetadata, null);
            return entity;
        }
    }, Entity.class), Entity.class);
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) EntityImpl(org.apache.stanbol.entityhub.core.model.EntityImpl) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation)

Example 14 with QueryResultListImpl

use of org.apache.stanbol.entityhub.core.query.QueryResultListImpl in project stanbol by apache.

the class LarqSearcher method findEntities.

@Override
public final QueryResultList<String> findEntities(FieldQuery parsedQuery) throws IOException {
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    query.setSparqlEndpointType(SparqlEndpointTypeEnum.LARQ);
    String sparqlQuery = query.toSparqlSelect(false);
    InputStream in = sendSparqlRequest(getQueryUri(), sparqlQuery, SparqlSearcher.DEFAULT_SPARQL_RESULT_CONTENT_TYPE);
    //Move to util class!
    final List<String> entities = extractEntitiesFromJsonResult(in, query.getRootVariableName());
    return new QueryResultListImpl<String>(query, entities.iterator(), String.class);
}
Also used : InputStream(java.io.InputStream) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl)

Example 15 with QueryResultListImpl

use of org.apache.stanbol.entityhub.core.query.QueryResultListImpl in project stanbol by apache.

the class ReferencedSiteImpl method findEntities.

@Override
public QueryResultList<Entity> findEntities(FieldQuery query) throws SiteException {
    List<Entity> results;
    if (siteConfiguration.getCacheStrategy() == CacheStrategy.all) {
        try {
            // When using the Cache, directly get the representations!
            QueryResultList<Representation> representations = cache.findRepresentation((query));
            results = new ArrayList<Entity>(representations.size());
            for (Representation result : representations) {
                Entity entity = new EntityImpl(getId(), result, null);
                results.add(entity);
                initEntityMetadata(entity, siteMetadata, singletonMap(RdfResourceEnum.isChached.getUri(), (Object) Boolean.TRUE));
            }
            return new QueryResultListImpl<Entity>(query, results, Entity.class);
        } catch (YardException e) {
            if (entitySearcher == null) {
                throw new SiteException("Unable to execute query on Cache " + siteConfiguration.getCacheId(), e);
            } else {
                log.warn(String.format("Error while performing query on Cache %s! Try to use remote site %s as fallback!", siteConfiguration.getCacheId(), siteConfiguration.getQueryUri()), e);
            }
        }
    }
    QueryResultList<String> entityIds;
    if (entitySearcher == null) {
        throw new SiteException(String.format("The ReferencedSite %s does not support queries!", getId()));
    }
    try {
        entityIds = entitySearcher.findEntities(query);
    } catch (IOException e) {
        throw new SiteException(String.format("Unable to execute query on remote site %s with entitySearcher %s!", siteConfiguration.getQueryUri(), siteConfiguration.getEntitySearcherType()), e);
    }
    int numResults = entityIds.size();
    List<Entity> entities = new ArrayList<Entity>(numResults);
    int errors = 0;
    SiteException lastError = null;
    for (String id : entityIds) {
        Entity entity;
        try {
            entity = getEntity(id);
            if (entity == null) {
                log.warn("Unable to create Entity for ID that was selected by an FieldQuery (id=" + id + ")");
            }
            entities.add(entity);
            // use the position in the list as resultSocre
            entity.getRepresentation().set(RdfResourceEnum.resultScore.getUri(), Float.valueOf((float) numResults));
        } catch (SiteException e) {
            lastError = e;
            errors++;
            log.warn(String.format("Unable to get Representation for Entity " + "%s. -> %d Error%s for %d Entities in QueryResult (Reason:%s)", id, errors, errors > 1 ? "s" : "", entityIds.size(), e.getMessage()));
        }
        // decrease numResults because it is used as resultScore for
        // entities
        numResults--;
    }
    if (lastError != null) {
        if (entities.isEmpty()) {
            throw new SiteException("Unable to get anly Representations for " + "Entities selected by the parsed Query (Root-Cause is the " + "last Exception trown)", lastError);
        } else {
            log.warn(String.format("Unable to get %d/%d Represetnations for selected Entities.", errors, entityIds.size()));
            log.warn("Stack trace of the last Exception:", lastError);
        }
    }
    return new QueryResultListImpl<Entity>(query, entities, Entity.class);
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) EntityImpl(org.apache.stanbol.entityhub.core.model.EntityImpl) ArrayList(java.util.ArrayList) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) IOException(java.io.IOException) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException)

Aggregations

QueryResultListImpl (org.apache.stanbol.entityhub.core.query.QueryResultListImpl)17 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)7 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)6 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)5 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)4 SiteException (org.apache.stanbol.entityhub.servicesapi.site.SiteException)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 EntityImpl (org.apache.stanbol.entityhub.core.model.EntityImpl)3 Site (org.apache.stanbol.entityhub.servicesapi.site.Site)3 PrivilegedActionException (java.security.PrivilegedActionException)2 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)2 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)2 ResultSet (org.apache.clerezza.rdf.core.sparql.ResultSet)2 SolrQuery (org.apache.solr.client.solrj.SolrQuery)2 SolrServerException (org.apache.solr.client.solrj.SolrServerException)2 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)2