Search in sources :

Example 6 with FieldQuery

use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery 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 7 with FieldQuery

use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery in project stanbol by apache.

the class BaseGoogleRefineReconcileResource method reconcile.

private JSONObject reconcile(String siteId, ReconcileQuery rQuery) throws JSONException, EntityhubException {
    FieldQuery query = createFieldQuery(siteId);
    query.addSelectedFields(SELECTED_FIELDS);
    addNameConstraint(rQuery, query);
    addTypeConstraint(rQuery, query);
    addPropertyConstraints(rQuery, query);
    query.setLimit(query.getLimit());
    QueryResultList<Representation> results = performQuery(siteId, query);
    List<JSONObject> jResultList = new ArrayList<JSONObject>(results.size());
    //we need to know the highest score to normalise between [0..1]
    double maxQueryScore = -1;
    if (!results.isEmpty()) {
        for (Representation r : results) {
            if (maxQueryScore < 0) {
                maxQueryScore = r.getFirst(resultScore.getUri(), Number.class).doubleValue();
            }
            JSONObject jResult = new JSONObject();
            jResult.put("id", r.getId());
            double similarity = 0.0;
            //the name returned for the entity
            String name = null;
            for (Iterator<Text> labels = r.getText(NAME_FIELD); labels.hasNext(); ) {
                Text label = labels.next();
                if (label.getText().equalsIgnoreCase(rQuery.getQuery())) {
                    name = label.getText();
                    similarity = 1.0;
                    break;
                }
                double curSimilarity = Utils.levenshtein(rQuery.getQuery(), label.getText());
                if (similarity < curSimilarity) {
                    name = label.getText();
                    similarity = curSimilarity;
                }
            }
            //set the selected name
            jResult.put("name", name);
            Iterator<Reference> types = r.getReferences(TYPE_FIELD);
            if (types != null && types.hasNext()) {
                jResult.put("type", new JSONArray(ModelUtils.asCollection(types)));
            }
            double normalisedScore = r.getFirst(resultScore.getUri(), Number.class).doubleValue();
            normalisedScore = normalisedScore * similarity / maxQueryScore;
            jResult.put("score", normalisedScore);
            jResult.put("match", similarity >= 0);
            jResultList.add(jResult);
        }
    }
    //else no results ... nothing todo
    //sort results based on score
    Collections.sort(jResultList, resultScoreComparator);
    JSONObject jResultContainer = new JSONObject();
    jResultContainer.put("result", new JSONArray(jResultList));
    return jResultContainer;
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) ArrayList(java.util.ArrayList) JSONArray(org.codehaus.jettison.json.JSONArray) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) JSONObject(org.codehaus.jettison.json.JSONObject)

Example 8 with FieldQuery

use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery in project stanbol by apache.

the class JerseyUtils method createFieldQueryForFindRequest.

//    /**
//     * Returns the {@link FieldQuery} based on the JSON formatted String (in case
//     * of "application/x-www-form-urlencoded" requests) or file (in case of
//     * "multipart/form-data" requests).<p>
//     * @param query the string containing the JSON serialised FieldQuery or
//     * <code>null</code> in case of a "multipart/form-data" request
//     * @param file the temporary file holding the data parsed by the request to
//     * the web server in case of a "multipart/form-data" request or <code>null</code>
//     * in case of the "application/x-www-form-urlencoded" request.
//     * @return the FieldQuery parsed from the string provided by one of the two
//     * parameters
//     * @throws WebApplicationException if both parameter are <code>null</code> or
//     * if the string provided by both parameters could not be used to parse a
//     * {@link FieldQuery} instance.
//     */
//    public static FieldQuery parseFieldQuery(String query, File file) throws WebApplicationException {
//        if(query == null && file == null) {
//            throw new WebApplicationException(new IllegalArgumentException("Query Requests MUST define the \"query\" parameter"), Response.Status.BAD_REQUEST);
//        }
//        FieldQuery fieldQuery = null;
//        JSONException exception = null;
//        if(query != null){
//            try {
//                fieldQuery = JSONToFieldQuery.fromJSON(queryFactory,query);
//            } catch (JSONException e) {
//                log.warn("unable to parse FieldQuery from \"application/x-www-form-urlencoded\" encoded query string "+query,e);
//                fieldQuery = null;
//                exception = e;
//            }
//        } //else no query via application/x-www-form-urlencoded parsed
//        if(fieldQuery == null && file != null){
//            try {
//                query = FileUtils.readFileToString(file);
//                fieldQuery = JSONToFieldQuery.fromJSON(queryFactory,query);
//            } catch (IOException e) {
//                throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
//            } catch (JSONException e) {
//                log.warn("unable to parse FieldQuery from \"multipart/form-data\" encoded query string "+query,e);
//                exception = e;
//            }
//        }//fieldquery already initialised or no query via multipart/form-data parsed
//        if(fieldQuery == null){
//            throw new WebApplicationException(new IllegalArgumentException("Unable to parse FieldQuery form the parsed query String:"+query, exception),Response.Status.BAD_REQUEST);
//        }
//        return fieldQuery;
//    }
/**
     * Creates an {@link FieldQuery} for parameters parsed by the /find requests
     * supported by the /symbol, /sites and {siteId} RESTful endpoints.
     * TODO: This has to be refactored to use "EntityQuery" as soon as Multiple
     *       query types are implemented for the Entityhub.
     * @param name the name pattern to search entities for (required)
     * @param field the field used to search for entities (required)
     * @param language the language of the parsed name pattern (optional)
     * @param limit the maximum number of result (optional)
     * @param offset the offset of the first result (optional)
     * @return the {@link FieldQuery} representing the parsed parameter
     * @throws WebApplicationException in case the parsed name pattern is invalid.
     * The validation of this required parameter provided by the Request is done
     * by this method.
     * @throws IllegalArgumentException in case the parsed field is invalid. Callers
     * of this method need to ensure that this parameter is set to an valid value.
     */
public static FieldQuery createFieldQueryForFindRequest(String name, String field, String language, Integer limit, Integer offset, String ldpath) throws WebApplicationException, IllegalArgumentException {
    if (name == null || name.trim().isEmpty()) {
        // as an bad Requested sent by the user
        throw new WebApplicationException(new IllegalArgumentException("The parsed \"name\" pattern to search entities for MUST NOT be NULL nor EMPTY"), Response.Status.BAD_REQUEST);
    } else {
        name = name.trim();
    }
    if (field == null || field.trim().isEmpty()) {
        // valid data. 
        throw new IllegalArgumentException("The parsed search \"field\" MUST NOT be NULL nor EMPTY");
    } else {
        field = field.trim();
    }
    log.debug("Process Find Request:");
    log.debug("  > name  : " + name);
    log.debug("  > field  : " + field);
    log.debug("  > lang  : " + language);
    log.debug("  > limit : " + limit);
    log.debug("  > offset: " + offset);
    log.debug("  > ldpath: " + ldpath);
    FieldQuery query;
    if (ldpath != null && !ldpath.isEmpty()) {
        //STANBOL-417 
        query = new LDPathFieldQueryImpl();
        ((LDPathFieldQueryImpl) query).setLDPathSelect(ldpath);
    } else {
        //if no LDPath is parsed select the default field
        query = queryFactory.createFieldQuery();
        Collection<String> selectedFields = new ArrayList<String>();
        //select also the field used to find entities
        selectedFields.add(field);
        query.addSelectedFields(selectedFields);
    }
    if (language == null || language.trim().isEmpty()) {
        query.setConstraint(field, new TextConstraint(name, PatternType.wildcard, false));
    } else {
        query.setConstraint(field, new TextConstraint(name, PatternType.wildcard, false, language));
    }
    if (limit != null && limit > 0) {
        query.setLimit(limit);
    }
    if (offset != null && offset > 0) {
        query.setOffset(offset);
    }
    return query;
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) WebApplicationException(javax.ws.rs.WebApplicationException) LDPathFieldQueryImpl(org.apache.stanbol.entityhub.ldpath.query.LDPathFieldQueryImpl) ArrayList(java.util.ArrayList) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)

Example 9 with FieldQuery

use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery in project stanbol by apache.

the class EntityhubRootResource method findEntity.

@POST
@Path("/find")
@Produces({ APPLICATION_JSON, RDF_XML, N3, TURTLE, X_TURTLE, RDF_JSON, N_TRIPLE, TEXT_HTML })
public Response findEntity(@FormParam(value = "name") String name, @FormParam(value = "field") String parsedField, @FormParam(value = "lang") String language, @FormParam(value = "limit") Integer limit, @FormParam(value = "offset") Integer offset, // solution!
@FormParam(value = "select") String select, @FormParam(value = "ldpath") String ldpath, @Context HttpHeaders headers) {
    log.debug("/find Request");
    final MediaType acceptedMediaType = getAcceptableMediaType(headers, ENTITY_SUPPORTED_MEDIA_TYPE_INCL_HTML, MediaType.APPLICATION_JSON_TYPE);
    if (name == null || name.isEmpty()) {
        if (acceptedMediaType.isCompatible(TEXT_HTML_TYPE)) {
            //return HTML docu
            ResponseBuilder rb = Response.ok(new Viewable("find", 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("The name must not be null nor empty for find requests. Missing parameter name.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
        }
    } else {
        final String property;
        if (parsedField == null) {
            property = DEFAULT_FIND_FIELD;
        } else {
            parsedField = parsedField.trim();
            if (parsedField.isEmpty()) {
                property = DEFAULT_FIND_FIELD;
            } else {
                property = nsPrefixService.getFullName(parsedField);
                if (property == null) {
                    String messsage = String.format("The prefix '%s' of the parsed field '%' is not " + "mapped to any namespace. Please parse the full URI instead!\n", NamespaceMappingUtils.getPrefix(parsedField), parsedField);
                    return Response.status(Status.BAD_REQUEST).entity(messsage).header(HttpHeaders.ACCEPT, acceptedMediaType).build();
                }
            }
        }
        FieldQuery query = JerseyUtils.createFieldQueryForFindRequest(name, property, language, limit == null || limit < 1 ? DEFAULT_FIND_RESULT_LIMIT : limit, offset, ldpath);
        // For the Entityhub we support to select additional fields for results
        // of find requests. For the Sites and {site} endpoint this is currently
        // deactivated because of very bad performance with OPTIONAL graph patterns
        // in SPARQL queries.
        Collection<String> additionalSelectedFields = new ArrayList<String>();
        if (select != null && !select.isEmpty()) {
            for (String selected : select.trim().split(" ")) {
                if (selected != null && !selected.isEmpty()) {
                    additionalSelectedFields.add(selected);
                }
            }
        }
        query.addSelectedFields(additionalSelectedFields);
        return executeQuery(query, headers, acceptedMediaType);
    }
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 10 with FieldQuery

use of org.apache.stanbol.entityhub.servicesapi.query.FieldQuery 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)

Aggregations

FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)41 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)18 Test (org.junit.Test)13 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)12 ReferenceConstraint (org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)10 HashSet (java.util.HashSet)8 ArrayList (java.util.ArrayList)7 Site (org.apache.stanbol.entityhub.servicesapi.site.Site)6 QueryResultListImpl (org.apache.stanbol.entityhub.core.query.QueryResultListImpl)5 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)5 SiteException (org.apache.stanbol.entityhub.servicesapi.site.SiteException)5 Constraint (org.apache.stanbol.entityhub.servicesapi.query.Constraint)4 FieldQueryFactory (org.apache.stanbol.entityhub.servicesapi.query.FieldQueryFactory)4 ValueConstraint (org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint)4 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)3 SimilarityConstraint (org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint)3 JSONException (org.codehaus.jettison.json.JSONException)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 IOException (java.io.IOException)2 PrivilegedActionException (java.security.PrivilegedActionException)2