Search in sources :

Example 1 with QueryResultListImpl

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

the class SiteManagerImpl method findIds.

@Override
public QueryResultList<String> findIds(FieldQuery query) {
    log.debug("findIds for query{}", query);
    // We need to search all referenced Sites
    Set<String> entityIds = new HashSet<String>();
    //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<String> results = site.findReferences(query);
                if (processedQuery == null) {
                    processedQuery = results.getQuery();
                }
                if (!results.isEmpty() && queryWithResults == null) {
                    processedQuery = results.getQuery();
                }
                for (String entityId : results) {
                    entityIds.add(entityId);
                }
            } 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<String>(//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, entityIds.iterator(), String.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) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) HashSet(java.util.HashSet)

Example 2 with QueryResultListImpl

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

the class SiteManagerImpl method findEntities.

@Override
public QueryResultList<Entity> findEntities(FieldQuery query) {
    log.debug("findEntities for query{}", query);
    //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;
    Set<Entity> entities = new HashSet<Entity>();
    for (Site site : referencedSites) {
        if (site.supportsSearch()) {
            //do not search on sites that do not support it
            log.debug(" > query site {}", site.getId());
            try {
                QueryResultList<Entity> results = site.findEntities(query);
                if (processedQuery == null) {
                    processedQuery = results.getQuery();
                }
                if (!results.isEmpty() && queryWithResults == null) {
                    processedQuery = results.getQuery();
                }
                for (Entity rep : results) {
                    if (!entities.contains(rep)) {
                        //do not override
                        entities.add(rep);
                    } else {
                        //TODO: find a solution for this problem
                        //      e.g. allow to add the site for entities
                        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<Entity>(//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, entities, Entity.class);
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Site(org.apache.stanbol.entityhub.servicesapi.site.Site) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) HashSet(java.util.HashSet)

Example 3 with QueryResultListImpl

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

the class VirtuosoSearcher method findEntities.

@Override
public final QueryResultList<String> findEntities(FieldQuery parsedQuery) throws IOException {
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    query.setSparqlEndpointType(SparqlEndpointTypeEnum.Virtuoso);
    String sparqlQuery = query.toSparqlSelect(false);
    log.trace("Sending Sparql request [{}].", sparqlQuery);
    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 4 with QueryResultListImpl

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

the class EntityhubRootResource method getSymbolMappings.

@GET
@Path("mapping/symbol")
@Produces({ APPLICATION_JSON, RDF_XML, N3, TURTLE, X_TURTLE, RDF_JSON, N_TRIPLE, TEXT_HTML })
public Response getSymbolMappings(@QueryParam("id") String symbol, @Context HttpHeaders headers) throws WebApplicationException {
    log.debug("getSymbolMappings() POST Request > symbol: {} > accept: {}", symbol, headers.getAcceptableMediaTypes());
    Set<String> supported = new HashSet<String>(JerseyUtils.REPRESENTATION_SUPPORTED_MEDIA_TYPES);
    supported.add(TEXT_HTML);
    MediaType acceptedMediaType = getAcceptableMediaType(headers, supported, APPLICATION_JSON_TYPE);
    if (symbol == null || symbol.isEmpty()) {
        //if HTML -> print the docu of the restfull service
        if (TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
            ResponseBuilder rb = Response.ok(new Viewable("mapping_symbol", this));
            rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
            //addCORSOrigin(servletContext, rb, headers);
            return rb.build();
        } else {
            return Response.status(Status.BAD_REQUEST).entity("No symbol given. Missing parameter id.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
        }
    }
    //Entityhub entityhub = ContextHelper.getServiceFromContext(Entityhub.class, servletContext);
    Collection<Entity> mappings;
    try {
        mappings = entityhub.getMappingsByTarget(symbol);
    } catch (EntityhubException e) {
        throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
    }
    if (mappings == null || mappings.isEmpty()) {
        return Response.status(Status.NOT_FOUND).entity("No mapping found for symbol '" + symbol + "'.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
    } else {
        // TODO: Implement Support for list of Signs, Representations and Strings
        // For now use a pseudo QueryResultList
        QueryResultList<Entity> mappingResultList = new QueryResultListImpl<Entity>(null, mappings, Entity.class);
        ResponseBuilder rb = Response.ok(mappingResultList);
        rb.header(HttpHeaders.CONTENT_TYPE, acceptedMediaType + "; charset=utf-8");
        //addCORSOrigin(servletContext, rb, headers);
        return rb.build();
    }
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) WebApplicationException(javax.ws.rs.WebApplicationException) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) MediaType(javax.ws.rs.core.MediaType) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with QueryResultListImpl

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

the class MockEntityhub method findEntities.

@Override
public QueryResultList<Entity> findEntities(FieldQuery query) throws EntityhubException {
    log.info("Performing Query: {}", query);
    QueryResultList<Representation> results = yard.findRepresentation(query);
    log.info("  ... {} results", results.size());
    Collection<Entity> entities = new ArrayList<Entity>(results.size());
    for (Representation r : results) {
        log.info("    > {}", r.getId());
        entities.add(new EntityImpl("dbpedia", r, null));
    }
    return new QueryResultListImpl<Entity>(results.getQuery(), 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) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation)

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