Search in sources :

Example 6 with Constraint

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

the class CoreferenceFinder method lookupEntity.

/**
     * Gets an Entity from the configured {@link Site} based on the NER text and type.
     * 
     * @param ner
     * @param language
     * @return
     * @throws EngineException
     */
private Entity lookupEntity(Span ner, String language) throws EngineException {
    Site site = getReferencedSite();
    FieldQueryFactory queryFactory = site == null ? entityHub.getQueryFactory() : site.getQueryFactory();
    FieldQuery query = queryFactory.createFieldQuery();
    Constraint labelConstraint;
    String namedEntityLabel = ner.getSpan();
    labelConstraint = new TextConstraint(namedEntityLabel, false, language, null);
    query.setConstraint(RDFS_LABEL.getUnicodeString(), labelConstraint);
    query.setConstraint(RDF_TYPE.getUnicodeString(), new ReferenceConstraint(ner.getAnnotation(NlpAnnotations.NER_ANNOTATION).value().getType().getUnicodeString()));
    query.setLimit(1);
    QueryResultList<Entity> results = // if site is NULL
    site == null ? entityHub.findEntities(query) : // use the Entityhub
    site.findEntities(// else the referenced site
    query);
    if (results.isEmpty())
        return null;
    // We set the limit to 1 so if it found anything it should contain just 1 entry
    return results.iterator().next();
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) Constraint(org.apache.stanbol.entityhub.servicesapi.query.Constraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) FieldQueryFactory(org.apache.stanbol.entityhub.servicesapi.query.FieldQueryFactory) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)

Example 7 with Constraint

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

the class SolrQueryFactory method parseFieldQuery.

/**
     * Converts the field query to a SolrQuery. In addition changes the parsed
     * FieldQuery (e.g. removing unsupported features, setting defaults for
     * missing parameters)
     * @param fieldQuery the field query (will be modified to reflect the query
     * as executed)
     * @param select the SELECT mode
     * @return the SolrQuery
     */
public SolrQuery parseFieldQuery(FieldQuery fieldQuery, SELECT select) {
    SolrQuery query = initSolrQuery(fieldQuery);
    setSelected(query, fieldQuery, select);
    StringBuilder queryString = new StringBuilder();
    Map<String, Constraint> processedFieldConstraints = new HashMap<String, Constraint>();
    boolean firstConstraint = true;
    boolean similarityConstraintPresent = false;
    for (Entry<String, Constraint> fieldConstraint : fieldQuery) {
        if (fieldConstraint.getValue().getType() == ConstraintType.similarity) {
            // TODO: log make the FieldQuery ensure that there is no more than one instead of similarity
            // constraint per query
            List<String> fields = new ArrayList<String>();
            fields.add(fieldConstraint.getKey());
            SimilarityConstraint simConstraint = (SimilarityConstraint) fieldConstraint.getValue();
            final IndexValue contextValue = indexValueFactory.createIndexValue(simConstraint.getContext());
            fields.addAll(simConstraint.getAdditionalFields());
            if (!similarityConstraintPresent) {
                //similarity constraint present
                similarityConstraintPresent = true;
                //add the constraint to the query
                query.setRequestHandler(MLT_QUERY_TYPE);
                query.set(MATCH_INCLUDE, false);
                query.set(MIN_DOC_FREQ, 1);
                query.set(MIN_TERM_FREQ, 1);
                query.set(INTERESTING_TERMS, "details");
                //testing
                query.set("mlt.boost", true);
                List<String> indexFields = new ArrayList<String>();
                for (String field : fields) {
                    //we need to get the actual fields in the index for the
                    //logical fields parsed with the constraint
                    IndexDataTypeEnum mapedIndexTypeEnum = IndexDataTypeEnum.forDataTyoe(simConstraint.getContextType());
                    IndexField indexField = new IndexField(Collections.singletonList(field), mapedIndexTypeEnum == null ? null : mapedIndexTypeEnum.getIndexType(), simConstraint.getLanguages());
                    indexFields.addAll(fieldMapper.getQueryFieldNames(indexField));
                }
                query.set(SIMILARITY_FIELDS, indexFields.toArray(new String[fields.size()]));
                query.set(STREAM_BODY, contextValue.getValue());
                processedFieldConstraints.put(fieldConstraint.getKey(), fieldConstraint.getValue());
            } else {
                //similarity constraint already present -> ignore further
                //NOTE: users are informed about that by NOT including further
                //      similarity constraints in the query included in the
                //      response
                log.warn("The parsed FieldQuery contains multiple Similarity constraints." + "However only a single one can be supported per query. Because of " + "this all further Similarity constraints will be ignored!");
                log.warn("Ignore SimilarityConstraint:");
                log.warn(" > Field      : {}", fieldConstraint.getKey());
                log.warn(" > Context    : {}", simConstraint.getContext());
                log.warn(" > Add Fields : {}", simConstraint.getAdditionalFields());
            }
        } else {
            IndexConstraint indexConstraint = createIndexConstraint(fieldConstraint);
            if (indexConstraint.isInvalid()) {
                log.warn("Unable to create IndexConstraint for Constraint {} (type: {}) and Field {} (Reosens: {})", new Object[] { fieldConstraint.getValue(), fieldConstraint.getValue().getType(), fieldConstraint.getKey(), indexConstraint.getInvalidMessages() });
            } else {
                if (firstConstraint) {
                    queryString.append('(');
                    firstConstraint = false;
                } else {
                    queryString.append(") AND (");
                }
                indexConstraint.encode(queryString);
                //set the constraint (may be changed because of some unsupported features)
                processedFieldConstraints.put(fieldConstraint.getKey(), //if null
                indexConstraint.getFieldQueryConstraint() == null ? //assume no change and add the parsed one
                fieldConstraint.getValue() : //add the changed version
                indexConstraint.getFieldQueryConstraint());
            }
        }
    }
    if (!firstConstraint) {
        queryString.append(')');
    }
    //set the constraints as processed to the parsed query
    fieldQuery.removeAllConstraints();
    for (Entry<String, Constraint> constraint : processedFieldConstraints.entrySet()) {
        fieldQuery.setConstraint(constraint.getKey(), constraint.getValue());
    }
    if (queryString.length() > 0) {
        String qs = queryString.toString();
        log.debug("QueryString: {}", qs);
        if (MLT_QUERY_TYPE.equals(query.getRequestHandler())) {
            query.set(CommonParams.FQ, qs);
        } else {
            query.setQuery(qs);
        }
    }
    log.debug("Solr Query: {}", query);
    return query;
}
Also used : SimilarityConstraint(org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint) HashMap(java.util.HashMap) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) SimilarityConstraint(org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint) RangeConstraint(org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) Constraint(org.apache.stanbol.entityhub.servicesapi.query.Constraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ArrayList(java.util.ArrayList) IndexValue(org.apache.stanbol.entityhub.yard.solr.model.IndexValue) SolrQuery(org.apache.solr.client.solrj.SolrQuery) IndexDataTypeEnum(org.apache.stanbol.entityhub.yard.solr.defaults.IndexDataTypeEnum) IndexField(org.apache.stanbol.entityhub.yard.solr.model.IndexField)

Example 8 with Constraint

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

the class FieldMappingUtils method parseConstraint.

private static Constraint parseConstraint(String filterString) {
    if (filterString.startsWith("d=")) {
        String[] dataTypeStrings = filterString.substring(2).split(";");
        Set<String> dataTypes = new HashSet<String>();
        for (int i = 0; i < dataTypeStrings.length; i++) {
            DataTypeEnum dataType = DataTypeEnum.getDataTypeByShortName(dataTypeStrings[i]);
            if (dataType == null) {
                dataType = DataTypeEnum.getDataType(dataTypeStrings[i]);
            }
            if (dataType != null) {
                dataTypes.add(dataType.getUri());
            } else {
                log.warn(String.format("DataType %s not supported! Datatype get not used by this Filter", dataTypeStrings[i]));
            }
        }
        if (dataTypes.isEmpty()) {
            log.warn(String.format("Unable to parse a valied data type form \"%s\"! A data type filter MUST define at least a single dataType. No filter will be used.", filterString));
            return null;
        } else {
            return new ValueConstraint(null, dataTypes);
        }
    } else if (filterString.startsWith("@=")) {
        String[] langs = filterString.substring(2).split(";");
        for (int i = 0; i < langs.length; i++) {
            if (langs[i].length() < 1 || "null".equals(langs[i])) {
                langs[i] = null;
            }
        }
        if (langs.length < 1) {
            log.warn("Unable to parse a language form \"%s\"! A language filter MUST define at least a singel language. No filter will be used." + filterString);
            return null;
        } else {
            return new TextConstraint((String) null, langs);
        }
    } else {
        log.warn(String.format("Filters need to start with \"p=\" (dataType) or \"@=\" (language). Parsed filter: \"%s\".", filterString));
        return null;
    }
}
Also used : DataTypeEnum(org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) Constraint(org.apache.stanbol.entityhub.servicesapi.query.Constraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) HashSet(java.util.HashSet)

Example 9 with Constraint

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

the class FieldQueryToJsonUtils method toJSON.

/**
     * Converts a {@link FieldQuery} to it's JSON representation
     *
     * @param query the Query
     * @return the {@link JSONObject}
     * @throws JSONException
     */
public static JSONObject toJSON(FieldQuery query, NamespacePrefixService nsPrefixService) throws JSONException {
    JSONObject jQuery = new JSONObject();
    jQuery.put("selected", new JSONArray(query.getSelectedFields()));
    JSONArray constraints = new JSONArray();
    jQuery.put("constraints", constraints);
    for (Entry<String, Constraint> fieldConstraint : query) {
        JSONObject jFieldConstraint = convertConstraintToJSON(fieldConstraint.getValue(), nsPrefixService);
        //add the field
        jFieldConstraint.put("field", fieldConstraint.getKey());
        //write the boost if present
        Double boost = fieldConstraint.getValue().getBoost();
        if (boost != null) {
            jFieldConstraint.put("boost", boost);
        }
        //add fieldConstraint
        constraints.put(jFieldConstraint);
    }
    if (query.getLimit() != null) {
        jQuery.put("limit", query.getLimit());
    }
    //if(query.getOffset() != 0){
    jQuery.put("offset", query.getOffset());
    //}
    if (query instanceof LDPathSelect && ((LDPathSelect) query).getLDPathSelect() != null && !((LDPathSelect) query).getLDPathSelect().isEmpty()) {
        jQuery.put("ldpath", ((LDPathSelect) query).getLDPathSelect());
    }
    return jQuery;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) Constraint(org.apache.stanbol.entityhub.servicesapi.query.Constraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) SimilarityConstraint(org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint) RangeConstraint(org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint) JSONArray(org.codehaus.jettison.json.JSONArray) LDPathSelect(org.apache.stanbol.entityhub.ldpath.query.LDPathSelect)

Example 10 with Constraint

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

the class FieldQueryReader method parseConstraint.

private static Constraint parseConstraint(JSONObject jConstraint, NamespacePrefixService nsPrefixService) throws JSONException {
    final Constraint constraint;
    if (jConstraint.has("type") && !jConstraint.isNull("type")) {
        String type = jConstraint.getString("type");
        //considered to represent reference constraints
        if (type.equals("reference")) {
            constraint = parseReferenceConstraint(jConstraint, nsPrefixService);
        } else if (type.equals(ConstraintType.value.name())) {
            constraint = parseValueConstraint(jConstraint, nsPrefixService);
        } else if (type.equals(ConstraintType.text.name())) {
            constraint = parseTextConstraint(jConstraint);
        } else if (type.equals(ConstraintType.range.name())) {
            constraint = parseRangeConstraint(jConstraint, nsPrefixService);
        } else if (type.equals(ConstraintType.similarity.name())) {
            constraint = parseSimilarityConstraint(jConstraint, nsPrefixService);
        } else {
            log.warn(String.format("Unknown Constraint Type %s. Supported values are %s", Arrays.asList("reference", ConstraintType.values())));
            StringBuilder message = new StringBuilder();
            message.append("Parsed Constraint uses an unknown value for 'type'!\n");
            message.append("Supported values: ");
            message.append(ConstraintType.values());
            message.append('\n');
            message.append("Parsed Constraint: \n");
            message.append(jConstraint.toString(4));
            throw new IllegalArgumentException(message.toString());
        }
    } else {
        log.warn(String.format("Earch Constraint MUST HAVE the \"type\" key set to one of the values %s", Arrays.asList("reference", ConstraintType.values())));
        StringBuilder message = new StringBuilder();
        message.append("Parsed Constraint does not define a value for the field 'type'!\n");
        message.append("Supported values: ");
        message.append(ConstraintType.values());
        message.append('\n');
        message.append("Parsed Constraint: \n");
        message.append(jConstraint.toString(4));
        throw new IllegalArgumentException(message.toString());
    }
    //finally parse the optional boost
    if (jConstraint.has("boost")) {
        double boost = jConstraint.optDouble("boost");
        if (boost == Double.NaN || boost <= 0) {
            StringBuilder message = new StringBuilder("The Boost of a Constraint " + "MUST BE a double AND >= 0 (parsed: '");
            message.append(jConstraint.get("boost")).append("')!");
            log.warn(message.toString());
            throw new IllegalArgumentException(message.toString());
        } else {
            constraint.setBoost(boost);
        }
    }
    //else no boost defined
    return constraint;
}
Also used : ValueConstraint(org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint) Constraint(org.apache.stanbol.entityhub.servicesapi.query.Constraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) SimilarityConstraint(org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint) RangeConstraint(org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint)

Aggregations

Constraint (org.apache.stanbol.entityhub.servicesapi.query.Constraint)15 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)15 ValueConstraint (org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint)12 ReferenceConstraint (org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)11 SimilarityConstraint (org.apache.stanbol.entityhub.servicesapi.query.SimilarityConstraint)11 RangeConstraint (org.apache.stanbol.entityhub.servicesapi.query.RangeConstraint)10 JSONArray (org.codehaus.jettison.json.JSONArray)6 JSONObject (org.codehaus.jettison.json.JSONObject)5 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)4 ArrayList (java.util.ArrayList)2 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)2 FieldQueryFactory (org.apache.stanbol.entityhub.servicesapi.query.FieldQueryFactory)2 MODE (org.apache.stanbol.entityhub.servicesapi.query.ValueConstraint.MODE)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Entry (java.util.Map.Entry)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 FieldQueryImpl (org.apache.stanbol.entityhub.core.query.FieldQueryImpl)1 LDPathFieldQueryImpl (org.apache.stanbol.entityhub.ldpath.query.LDPathFieldQueryImpl)1