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();
}
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;
}
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;
}
}
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;
}
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;
}
Aggregations