Search in sources :

Example 1 with FieldQuery

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

the class EntitySearcherUtils method createFieldQuery.

/**
 * Validated the parsed parameter as parsed to
 * {@link EntitySearcher#lookup(String, Set, List, String...)}
 * and creates a fieldQuery for the parsed parameter
 * @param field
 * @param includeFields
 * @param search
 * @param languages
 * @return
 */
public static final FieldQuery createFieldQuery(FieldQueryFactory factory, String field, Set<String> includeFields, List<String> search, String... languages) {
    if (field == null || field.isEmpty()) {
        throw new IllegalArgumentException("The parsed search field MUST NOT be NULL nor empty");
    }
    if (search == null || search.isEmpty()) {
        throw new IllegalArgumentException("The parsed list of search strings MUST NOT be NULL nor empty");
    }
    // build the query and than return the result
    FieldQuery query = factory.createFieldQuery();
    if (includeFields == null) {
        query.addSelectedField(field);
    } else {
        if (!includeFields.contains(field)) {
            query.addSelectedField(field);
        }
        for (String select : includeFields) {
            query.addSelectedField(select);
        }
    }
    // TODO make configurable
    query.setLimit(20);
    query.setConstraint(field, new TextConstraint(search, languages));
    return query;
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)

Example 2 with FieldQuery

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

the class ReferencedSiteSearcher method lookup.

@Override
public Collection<? extends Representation> lookup(String field, Set<String> includeFields, List<String> search, String... languages) throws IllegalStateException {
    // build the query and than return the result
    Site site = getSearchService();
    if (site == null) {
        throw new IllegalStateException("ReferencedSite " + siteId + " is currently not available");
    }
    FieldQuery query = EntitySearcherUtils.createFieldQuery(site.getQueryFactory(), field, includeFields, search, languages);
    if (limit != null) {
        query.setLimit(limit);
    }
    QueryResultList<Representation> results;
    try {
        results = site.find(query);
    } catch (SiteException e) {
        throw new IllegalStateException("Exception while searchign for " + search + '@' + Arrays.toString(languages) + "in the ReferencedSite " + site.getId(), e);
    }
    return results.results();
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException)

Example 3 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 4 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 5 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)

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