use of com.thinkaurelius.titan.core.attribute.Cmp in project atlas by apache.
the class Solr5Index method buildQueryFilter.
public String buildQueryFilter(Condition<TitanElement> condition, KeyInformation.StoreRetriever informations) {
if (condition instanceof PredicateCondition) {
PredicateCondition<String, TitanElement> atom = (PredicateCondition<String, TitanElement>) condition;
Object value = atom.getValue();
String key = atom.getKey();
TitanPredicate titanPredicate = atom.getPredicate();
if (value instanceof Number) {
String queryValue = escapeValue(value);
Preconditions.checkArgument(titanPredicate instanceof Cmp, "Relation not supported on numeric types: " + titanPredicate);
Cmp numRel = (Cmp) titanPredicate;
switch(numRel) {
case EQUAL:
return (key + ":" + queryValue);
case NOT_EQUAL:
return ("-" + key + ":" + queryValue);
case LESS_THAN:
// use right curly to mean up to but not including value
return (key + ":[* TO " + queryValue + "}");
case LESS_THAN_EQUAL:
return (key + ":[* TO " + queryValue + "]");
case GREATER_THAN:
// use left curly to mean greater than but not including value
return (key + ":{" + queryValue + " TO *]");
case GREATER_THAN_EQUAL:
return (key + ":[" + queryValue + " TO *]");
default:
throw new IllegalArgumentException("Unexpected relation: " + numRel);
}
} else if (value instanceof String) {
Mapping map = getStringMapping(informations.get(key));
assert map == TEXT || map == STRING;
if (map == TEXT && !titanPredicate.toString().startsWith("CONTAINS"))
throw new IllegalArgumentException("Text mapped string values only support CONTAINS queries and not: " + titanPredicate);
if (map == STRING && titanPredicate.toString().startsWith("CONTAINS"))
throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + titanPredicate);
// Special case
if (titanPredicate == Text.CONTAINS) {
// e.g. - if terms tomorrow and world were supplied, and fq=text:(tomorrow world)
// sample data set would return 2 documents: one where text = Tomorrow is the World,
// and the second where text = Hello World. Hence, we are decomposing the query string
// and building an AND query explicitly because we need AND semantics
value = ((String) value).toLowerCase();
List<String> terms = Text.tokenize((String) value);
if (terms.isEmpty()) {
return "";
} else if (terms.size() == 1) {
return (key + ":(" + escapeValue(terms.get(0)) + ")");
} else {
And<TitanElement> andTerms = new And<>();
for (String term : terms) {
andTerms.add(new PredicateCondition<>(key, titanPredicate, term));
}
return buildQueryFilter(andTerms, informations);
}
}
if (titanPredicate == Text.PREFIX || titanPredicate == Text.CONTAINS_PREFIX) {
return (key + ":" + escapeValue(value) + "*");
} else if (titanPredicate == Text.REGEX || titanPredicate == Text.CONTAINS_REGEX) {
return (key + ":/" + value + "/");
} else if (titanPredicate == EQUAL) {
return (key + ":\"" + escapeValue(value) + "\"");
} else if (titanPredicate == NOT_EQUAL) {
return ("-" + key + ":\"" + escapeValue(value) + "\"");
} else {
throw new IllegalArgumentException("Relation is not supported for string value: " + titanPredicate);
}
} else if (value instanceof Geoshape) {
Geoshape geo = (Geoshape) value;
if (geo.getType() == Geoshape.Type.CIRCLE) {
Geoshape.Point center = geo.getPoint();
return ("{!geofilt sfield=" + key + " pt=" + center.getLatitude() + "," + center.getLongitude() + " d=" + geo.getRadius() + // distance in kilometers
"} distErrPct=0");
} else if (geo.getType() == Geoshape.Type.BOX) {
Geoshape.Point southwest = geo.getPoint(0);
Geoshape.Point northeast = geo.getPoint(1);
return (key + ":[" + southwest.getLatitude() + "," + southwest.getLongitude() + " TO " + northeast.getLatitude() + "," + northeast.getLongitude() + "]");
} else if (geo.getType() == Geoshape.Type.POLYGON) {
List<Geoshape.Point> coordinates = getPolygonPoints(geo);
StringBuilder poly = new StringBuilder(key + ":\"IsWithin(POLYGON((");
for (Geoshape.Point coordinate : coordinates) {
poly.append(coordinate.getLongitude()).append(" ").append(coordinate.getLatitude()).append(", ");
}
// close the polygon with the first coordinate
poly.append(coordinates.get(0).getLongitude()).append(" ").append(coordinates.get(0).getLatitude());
poly.append(")))\" distErrPct=0");
return (poly.toString());
}
} else if (value instanceof Date) {
String queryValue = escapeValue(toIsoDate((Date) value));
Preconditions.checkArgument(titanPredicate instanceof Cmp, "Relation not supported on date types: " + titanPredicate);
Cmp numRel = (Cmp) titanPredicate;
switch(numRel) {
case EQUAL:
return (key + ":" + queryValue);
case NOT_EQUAL:
return ("-" + key + ":" + queryValue);
case LESS_THAN:
// use right curly to mean up to but not including value
return (key + ":[* TO " + queryValue + "}");
case LESS_THAN_EQUAL:
return (key + ":[* TO " + queryValue + "]");
case GREATER_THAN:
// use left curly to mean greater than but not including value
return (key + ":{" + queryValue + " TO *]");
case GREATER_THAN_EQUAL:
return (key + ":[" + queryValue + " TO *]");
default:
throw new IllegalArgumentException("Unexpected relation: " + numRel);
}
} else if (value instanceof Boolean) {
Cmp numRel = (Cmp) titanPredicate;
String queryValue = escapeValue(value);
switch(numRel) {
case EQUAL:
return (key + ":" + queryValue);
case NOT_EQUAL:
return ("-" + key + ":" + queryValue);
default:
throw new IllegalArgumentException("Boolean types only support EQUAL or NOT_EQUAL");
}
} else if (value instanceof UUID) {
if (titanPredicate == EQUAL) {
return (key + ":\"" + escapeValue(value) + "\"");
} else if (titanPredicate == NOT_EQUAL) {
return ("-" + key + ":\"" + escapeValue(value) + "\"");
} else {
throw new IllegalArgumentException("Relation is not supported for uuid value: " + titanPredicate);
}
} else
throw new IllegalArgumentException("Unsupported type: " + value);
} else if (condition instanceof Not) {
String sub = buildQueryFilter(((Not) condition).getChild(), informations);
if (StringUtils.isNotBlank(sub))
return "-(" + sub + ")";
else
return "";
} else if (condition instanceof And) {
int numChildren = ((And) condition).size();
StringBuilder sb = new StringBuilder();
for (Condition<TitanElement> c : condition.getChildren()) {
String sub = buildQueryFilter(c, informations);
if (StringUtils.isBlank(sub))
continue;
// b. expression is a single statement in the AND.
if (!sub.startsWith("-") && numChildren > 1)
sb.append("+");
sb.append(sub).append(" ");
}
return sb.toString();
} else if (condition instanceof Or) {
StringBuilder sb = new StringBuilder();
int element = 0;
for (Condition<TitanElement> c : condition.getChildren()) {
String sub = buildQueryFilter(c, informations);
if (StringUtils.isBlank(sub))
continue;
if (element == 0)
sb.append("(");
else
sb.append(" OR ");
sb.append(sub);
element++;
}
if (element > 0)
sb.append(")");
return sb.toString();
} else {
throw new IllegalArgumentException("Invalid condition: " + condition);
}
return null;
}
use of com.thinkaurelius.titan.core.attribute.Cmp in project atlas by apache.
the class Solr5Index method supports.
@Override
public boolean supports(KeyInformation information, TitanPredicate titanPredicate) {
Class<?> dataType = information.getDataType();
Mapping mapping = getMapping(information);
if (mapping != DEFAULT && !AttributeUtil.isString(dataType))
return false;
if (Number.class.isAssignableFrom(dataType)) {
return titanPredicate instanceof Cmp;
} else if (dataType == Geoshape.class) {
return titanPredicate == Geo.WITHIN;
} else if (AttributeUtil.isString(dataType)) {
switch(mapping) {
case DEFAULT:
case TEXT:
return titanPredicate == Text.CONTAINS || titanPredicate == Text.CONTAINS_PREFIX || titanPredicate == Text.CONTAINS_REGEX;
case STRING:
return titanPredicate == EQUAL || titanPredicate == NOT_EQUAL || titanPredicate == Text.REGEX || titanPredicate == Text.PREFIX;
}
} else if (dataType == Date.class) {
if (titanPredicate instanceof Cmp)
return true;
} else if (dataType == Boolean.class) {
return titanPredicate == EQUAL || titanPredicate == NOT_EQUAL;
} else if (dataType == UUID.class) {
return titanPredicate == EQUAL || titanPredicate == NOT_EQUAL;
}
return false;
}
use of com.thinkaurelius.titan.core.attribute.Cmp in project incubator-atlas by apache.
the class Solr5Index method supports.
@Override
public boolean supports(KeyInformation information, TitanPredicate titanPredicate) {
Class<?> dataType = information.getDataType();
Mapping mapping = getMapping(information);
if (mapping != DEFAULT && !AttributeUtil.isString(dataType))
return false;
if (Number.class.isAssignableFrom(dataType)) {
return titanPredicate instanceof Cmp;
} else if (dataType == Geoshape.class) {
return titanPredicate == Geo.WITHIN;
} else if (AttributeUtil.isString(dataType)) {
switch(mapping) {
case DEFAULT:
case TEXT:
return titanPredicate == Text.CONTAINS || titanPredicate == Text.CONTAINS_PREFIX || titanPredicate == Text.CONTAINS_REGEX;
case STRING:
return titanPredicate == EQUAL || titanPredicate == NOT_EQUAL || titanPredicate == Text.REGEX || titanPredicate == Text.PREFIX;
}
} else if (dataType == Date.class) {
if (titanPredicate instanceof Cmp)
return true;
} else if (dataType == Boolean.class) {
return titanPredicate == EQUAL || titanPredicate == NOT_EQUAL;
} else if (dataType == UUID.class) {
return titanPredicate == EQUAL || titanPredicate == NOT_EQUAL;
}
return false;
}
use of com.thinkaurelius.titan.core.attribute.Cmp in project titan by thinkaurelius.
the class AbstractVertexCentricQueryBuilder method compileSortKeyConstraints.
private static EdgeSerializer.TypedInterval[] compileSortKeyConstraints(InternalType type, StandardTitanTx tx, And<TitanRelation> conditions) {
long[] sortKeys = type.getSortKey();
EdgeSerializer.TypedInterval[] sortKeyConstraints = new EdgeSerializer.TypedInterval[type.getSortKey().length];
for (int i = 0; i < sortKeys.length; i++) {
InternalType pktype = (InternalType) tx.getExistingType(sortKeys[i]);
Interval interval = null;
// First check for equality constraints, since those are the most constraining
for (Iterator<Condition<TitanRelation>> iter = conditions.iterator(); iter.hasNext(); ) {
Condition<TitanRelation> cond = iter.next();
if (cond instanceof PredicateCondition) {
PredicateCondition<TitanType, TitanRelation> atom = (PredicateCondition) cond;
if (atom.getKey().equals(pktype) && atom.getPredicate() == Cmp.EQUAL && interval == null) {
interval = new PointInterval(atom.getValue());
iter.remove();
}
}
}
// and if so, find a bounding interval from the remaining constraints
if (interval == null && pktype.isPropertyKey() && Comparable.class.isAssignableFrom(((TitanKey) pktype).getDataType())) {
ProperInterval pint = new ProperInterval();
for (Iterator<Condition<TitanRelation>> iter = conditions.iterator(); iter.hasNext(); ) {
Condition<TitanRelation> cond = iter.next();
if (cond instanceof PredicateCondition) {
PredicateCondition<TitanType, TitanRelation> atom = (PredicateCondition) cond;
if (atom.getKey().equals(pktype)) {
TitanPredicate predicate = atom.getPredicate();
Object value = atom.getValue();
if (predicate instanceof Cmp) {
switch((Cmp) predicate) {
case NOT_EQUAL:
break;
case LESS_THAN:
if (pint.getEnd() == null || pint.getEnd().compareTo(value) >= 0) {
pint.setEnd((Comparable) value);
pint.setEndInclusive(false);
}
iter.remove();
break;
case LESS_THAN_EQUAL:
if (pint.getEnd() == null || pint.getEnd().compareTo(value) > 0) {
pint.setEnd((Comparable) value);
pint.setEndInclusive(true);
}
iter.remove();
break;
case GREATER_THAN:
if (pint.getStart() == null || pint.getStart().compareTo(value) <= 0) {
pint.setStart((Comparable) value);
pint.setStartInclusive(false);
}
iter.remove();
break;
case GREATER_THAN_EQUAL:
if (pint.getStart() == null || pint.getStart().compareTo(value) < 0) {
pint.setStart((Comparable) value);
pint.setStartInclusive(true);
}
iter.remove();
break;
}
}
}
} else if (cond instanceof Or) {
// Grab a probe so we can investigate what type of or-condition this is and whether it allows us to constrain this sort key
Condition probe = ((Or) cond).get(0);
if (probe instanceof PredicateCondition && ((PredicateCondition) probe).getKey().equals(pktype) && ((PredicateCondition) probe).getPredicate() == Cmp.EQUAL) {
// We make the assumption that this or-condition is a group of equality constraints for the same type (i.e. an unrolled Contain.IN)
// This assumption is enforced by precondition statements below
// TODO: Consider splitting query on sort key with a limited number (<=3) of possible values in or-clause
// Now, we find the smallest and largest value in this group of equality constraints to bound the interval
Comparable smallest = null, largest = null;
for (Condition child : cond.getChildren()) {
assert child instanceof PredicateCondition;
PredicateCondition pc = (PredicateCondition) child;
assert pc.getKey().equals(pktype);
assert pc.getPredicate() == Cmp.EQUAL;
Object v = pc.getValue();
if (smallest == null) {
smallest = (Comparable) v;
largest = (Comparable) v;
} else {
if (smallest.compareTo(v) > 0) {
smallest = (Comparable) v;
} else if (largest.compareTo(v) < 0) {
largest = (Comparable) v;
}
}
}
// due to probing, there must be at least one
assert smallest != null && largest != null;
if (pint.getEnd() == null || pint.getEnd().compareTo(largest) > 0) {
pint.setEnd(largest);
pint.setEndInclusive(true);
}
if (pint.getStart() == null || pint.getStart().compareTo(smallest) < 0) {
pint.setStart(smallest);
pint.setStartInclusive(true);
}
// We cannot remove this condition from remainingConditions, since its not exactly fulfilled (only bounded)
}
}
}
if (pint.isEmpty())
return null;
if (pint.getStart() != null || pint.getEnd() != null)
interval = pint;
}
sortKeyConstraints[i] = new EdgeSerializer.TypedInterval(pktype, interval);
if (interval == null || !interval.isPoint()) {
break;
}
}
return sortKeyConstraints;
}
use of com.thinkaurelius.titan.core.attribute.Cmp in project incubator-atlas by apache.
the class Solr5Index method buildQueryFilter.
public String buildQueryFilter(Condition<TitanElement> condition, KeyInformation.StoreRetriever informations) {
if (condition instanceof PredicateCondition) {
PredicateCondition<String, TitanElement> atom = (PredicateCondition<String, TitanElement>) condition;
Object value = atom.getValue();
String key = atom.getKey();
TitanPredicate titanPredicate = atom.getPredicate();
if (value instanceof Number) {
String queryValue = escapeValue(value);
Preconditions.checkArgument(titanPredicate instanceof Cmp, "Relation not supported on numeric types: " + titanPredicate);
Cmp numRel = (Cmp) titanPredicate;
switch(numRel) {
case EQUAL:
return (key + ":" + queryValue);
case NOT_EQUAL:
return ("-" + key + ":" + queryValue);
case LESS_THAN:
// use right curly to mean up to but not including value
return (key + ":[* TO " + queryValue + "}");
case LESS_THAN_EQUAL:
return (key + ":[* TO " + queryValue + "]");
case GREATER_THAN:
// use left curly to mean greater than but not including value
return (key + ":{" + queryValue + " TO *]");
case GREATER_THAN_EQUAL:
return (key + ":[" + queryValue + " TO *]");
default:
throw new IllegalArgumentException("Unexpected relation: " + numRel);
}
} else if (value instanceof String) {
Mapping map = getStringMapping(informations.get(key));
assert map == TEXT || map == STRING;
if (map == TEXT && !titanPredicate.toString().startsWith("CONTAINS"))
throw new IllegalArgumentException("Text mapped string values only support CONTAINS queries and not: " + titanPredicate);
if (map == STRING && titanPredicate.toString().startsWith("CONTAINS"))
throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + titanPredicate);
// Special case
if (titanPredicate == Text.CONTAINS) {
// e.g. - if terms tomorrow and world were supplied, and fq=text:(tomorrow world)
// sample data set would return 2 documents: one where text = Tomorrow is the World,
// and the second where text = Hello World. Hence, we are decomposing the query string
// and building an AND query explicitly because we need AND semantics
value = ((String) value).toLowerCase();
List<String> terms = Text.tokenize((String) value);
if (terms.isEmpty()) {
return "";
} else if (terms.size() == 1) {
return (key + ":(" + escapeValue(terms.get(0)) + ")");
} else {
And<TitanElement> andTerms = new And<>();
for (String term : terms) {
andTerms.add(new PredicateCondition<>(key, titanPredicate, term));
}
return buildQueryFilter(andTerms, informations);
}
}
if (titanPredicate == Text.PREFIX || titanPredicate == Text.CONTAINS_PREFIX) {
return (key + ":" + escapeValue(value) + "*");
} else if (titanPredicate == Text.REGEX || titanPredicate == Text.CONTAINS_REGEX) {
return (key + ":/" + value + "/");
} else if (titanPredicate == EQUAL) {
return (key + ":\"" + escapeValue(value) + "\"");
} else if (titanPredicate == NOT_EQUAL) {
return ("-" + key + ":\"" + escapeValue(value) + "\"");
} else {
throw new IllegalArgumentException("Relation is not supported for string value: " + titanPredicate);
}
} else if (value instanceof Geoshape) {
Geoshape geo = (Geoshape) value;
if (geo.getType() == Geoshape.Type.CIRCLE) {
Geoshape.Point center = geo.getPoint();
return ("{!geofilt sfield=" + key + " pt=" + center.getLatitude() + "," + center.getLongitude() + " d=" + geo.getRadius() + // distance in kilometers
"} distErrPct=0");
} else if (geo.getType() == Geoshape.Type.BOX) {
Geoshape.Point southwest = geo.getPoint(0);
Geoshape.Point northeast = geo.getPoint(1);
return (key + ":[" + southwest.getLatitude() + "," + southwest.getLongitude() + " TO " + northeast.getLatitude() + "," + northeast.getLongitude() + "]");
} else if (geo.getType() == Geoshape.Type.POLYGON) {
List<Geoshape.Point> coordinates = getPolygonPoints(geo);
StringBuilder poly = new StringBuilder(key + ":\"IsWithin(POLYGON((");
for (Geoshape.Point coordinate : coordinates) {
poly.append(coordinate.getLongitude()).append(" ").append(coordinate.getLatitude()).append(", ");
}
// close the polygon with the first coordinate
poly.append(coordinates.get(0).getLongitude()).append(" ").append(coordinates.get(0).getLatitude());
poly.append(")))\" distErrPct=0");
return (poly.toString());
}
} else if (value instanceof Date) {
String queryValue = escapeValue(toIsoDate((Date) value));
Preconditions.checkArgument(titanPredicate instanceof Cmp, "Relation not supported on date types: " + titanPredicate);
Cmp numRel = (Cmp) titanPredicate;
switch(numRel) {
case EQUAL:
return (key + ":" + queryValue);
case NOT_EQUAL:
return ("-" + key + ":" + queryValue);
case LESS_THAN:
// use right curly to mean up to but not including value
return (key + ":[* TO " + queryValue + "}");
case LESS_THAN_EQUAL:
return (key + ":[* TO " + queryValue + "]");
case GREATER_THAN:
// use left curly to mean greater than but not including value
return (key + ":{" + queryValue + " TO *]");
case GREATER_THAN_EQUAL:
return (key + ":[" + queryValue + " TO *]");
default:
throw new IllegalArgumentException("Unexpected relation: " + numRel);
}
} else if (value instanceof Boolean) {
Cmp numRel = (Cmp) titanPredicate;
String queryValue = escapeValue(value);
switch(numRel) {
case EQUAL:
return (key + ":" + queryValue);
case NOT_EQUAL:
return ("-" + key + ":" + queryValue);
default:
throw new IllegalArgumentException("Boolean types only support EQUAL or NOT_EQUAL");
}
} else if (value instanceof UUID) {
if (titanPredicate == EQUAL) {
return (key + ":\"" + escapeValue(value) + "\"");
} else if (titanPredicate == NOT_EQUAL) {
return ("-" + key + ":\"" + escapeValue(value) + "\"");
} else {
throw new IllegalArgumentException("Relation is not supported for uuid value: " + titanPredicate);
}
} else
throw new IllegalArgumentException("Unsupported type: " + value);
} else if (condition instanceof Not) {
String sub = buildQueryFilter(((Not) condition).getChild(), informations);
if (StringUtils.isNotBlank(sub))
return "-(" + sub + ")";
else
return "";
} else if (condition instanceof And) {
int numChildren = ((And) condition).size();
StringBuilder sb = new StringBuilder();
for (Condition<TitanElement> c : condition.getChildren()) {
String sub = buildQueryFilter(c, informations);
if (StringUtils.isBlank(sub))
continue;
// b. expression is a single statement in the AND.
if (!sub.startsWith("-") && numChildren > 1)
sb.append("+");
sb.append(sub).append(" ");
}
return sb.toString();
} else if (condition instanceof Or) {
StringBuilder sb = new StringBuilder();
int element = 0;
for (Condition<TitanElement> c : condition.getChildren()) {
String sub = buildQueryFilter(c, informations);
if (StringUtils.isBlank(sub))
continue;
if (element == 0)
sb.append("(");
else
sb.append(" OR ");
sb.append(sub);
element++;
}
if (element > 0)
sb.append(")");
return sb.toString();
} else {
throw new IllegalArgumentException("Invalid condition: " + condition);
}
return null;
}
Aggregations