use of org.janusgraph.core.schema.Mapping in project janusgraph by JanusGraph.
the class SolrIndex method supports.
@Override
public boolean supports(KeyInformation information, JanusGraphPredicate predicate) {
final Class<?> dataType = information.getDataType();
final Mapping mapping = Mapping.getMapping(information);
if (mapping != Mapping.DEFAULT && !AttributeUtil.isString(dataType) && !(mapping == Mapping.PREFIX_TREE && AttributeUtil.isGeo(dataType)))
return false;
if (Number.class.isAssignableFrom(dataType)) {
return predicate instanceof Cmp;
} else if (dataType == Geoshape.class) {
switch(mapping) {
case DEFAULT:
return predicate == Geo.WITHIN || predicate == Geo.INTERSECT;
case PREFIX_TREE:
return predicate == Geo.INTERSECT || predicate == Geo.WITHIN || predicate == Geo.CONTAINS;
}
} else if (AttributeUtil.isString(dataType)) {
switch(mapping) {
case DEFAULT:
case TEXT:
return predicate == Text.CONTAINS || predicate == Text.CONTAINS_PREFIX || predicate == Text.CONTAINS_REGEX || predicate == Text.CONTAINS_FUZZY;
case STRING:
return predicate instanceof Cmp || predicate == Text.REGEX || predicate == Text.PREFIX || predicate == Text.FUZZY;
}
} else if (dataType == Date.class || dataType == Instant.class) {
return predicate instanceof Cmp;
} else if (dataType == Boolean.class) {
return predicate == Cmp.EQUAL || predicate == Cmp.NOT_EQUAL;
} else if (dataType == UUID.class) {
return predicate == Cmp.EQUAL || predicate == Cmp.NOT_EQUAL;
}
return false;
}
use of org.janusgraph.core.schema.Mapping in project janusgraph by JanusGraph.
the class SolrIndex method buildQueryFilter.
public String buildQueryFilter(Condition<JanusGraphElement> condition, KeyInformation.StoreRetriever information) {
if (condition instanceof PredicateCondition) {
final PredicateCondition<String, JanusGraphElement> atom = (PredicateCondition<String, JanusGraphElement>) condition;
final Object value = atom.getValue();
final String key = atom.getKey();
final JanusGraphPredicate predicate = atom.getPredicate();
if (value instanceof Number) {
final String queryValue = escapeValue(value);
Preconditions.checkArgument(predicate instanceof Cmp, "Relation not supported on numeric types: " + predicate);
final Cmp numRel = (Cmp) predicate;
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) {
final Mapping map = getStringMapping(information.get(key));
assert map == Mapping.TEXT || map == Mapping.STRING;
if (map == Mapping.TEXT && !(Text.HAS_CONTAINS.contains(predicate) || predicate instanceof Cmp))
throw new IllegalArgumentException("Text mapped string values only support CONTAINS and Compare queries and not: " + predicate);
if (map == Mapping.STRING && Text.HAS_CONTAINS.contains(predicate))
throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + predicate);
// Special case
if (predicate == Text.CONTAINS) {
return tokenize(information, value, key, predicate, ParameterType.TEXT_ANALYZER.findParameter(information.get(key).getParameters(), null));
} else if (predicate == Text.PREFIX || predicate == Text.CONTAINS_PREFIX) {
return (key + ":" + escapeValue(value) + "*");
} else if (predicate == Text.REGEX || predicate == Text.CONTAINS_REGEX) {
return (key + ":/" + value + "/");
} else if (predicate == Cmp.EQUAL) {
final String tokenizer = ParameterType.STRING_ANALYZER.findParameter(information.get(key).getParameters(), null);
if (tokenizer != null) {
return tokenize(information, value, key, predicate, tokenizer);
} else {
return (key + ":\"" + escapeValue(value) + "\"");
}
} else if (predicate == Cmp.NOT_EQUAL) {
return ("-" + key + ":\"" + escapeValue(value) + "\"");
} else if (predicate == Text.FUZZY || predicate == Text.CONTAINS_FUZZY) {
return (key + ":" + escapeValue(value) + "~");
} else if (predicate == Cmp.LESS_THAN) {
return (key + ":[* TO \"" + escapeValue(value) + "\"}");
} else if (predicate == Cmp.LESS_THAN_EQUAL) {
return (key + ":[* TO \"" + escapeValue(value) + "\"]");
} else if (predicate == Cmp.GREATER_THAN) {
return (key + ":{\"" + escapeValue(value) + "\" TO *]");
} else if (predicate == Cmp.GREATER_THAN_EQUAL) {
return (key + ":[\"" + escapeValue(value) + "\" TO *]");
} else {
throw new IllegalArgumentException("Relation is not supported for string value: " + predicate);
}
} else if (value instanceof Geoshape) {
final Mapping map = Mapping.getMapping(information.get(key));
Preconditions.checkArgument(predicate instanceof Geo && predicate != Geo.DISJOINT, "Relation not supported on geo types: " + predicate);
Preconditions.checkArgument(map == Mapping.PREFIX_TREE || predicate == Geo.WITHIN || predicate == Geo.INTERSECT, "Relation not supported on geopoint types: " + predicate);
final Geoshape geo = (Geoshape) value;
if (geo.getType() == Geoshape.Type.CIRCLE && (predicate == Geo.INTERSECT || map == Mapping.DEFAULT)) {
final 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 && (predicate == Geo.INTERSECT || map == Mapping.DEFAULT)) {
final Geoshape.Point southwest = geo.getPoint(0);
final Geoshape.Point northeast = geo.getPoint(1);
return (key + ":[" + southwest.getLatitude() + "," + southwest.getLongitude() + " TO " + northeast.getLatitude() + "," + northeast.getLongitude() + "]");
} else if (map == Mapping.PREFIX_TREE) {
return key + ":\"" + SPATIAL_PREDICATES.get(predicate) + "(" + geo + ")\" distErrPct=0";
} else {
throw new IllegalArgumentException("Unsupported or invalid search shape type: " + geo.getType());
}
} else if (value instanceof Date || value instanceof Instant) {
final String s = value.toString();
final String queryValue = escapeValue(value instanceof Date ? toIsoDate((Date) value) : value.toString());
Preconditions.checkArgument(predicate instanceof Cmp, "Relation not supported on date types: " + predicate);
final Cmp numRel = (Cmp) predicate;
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) {
final Cmp numRel = (Cmp) predicate;
final 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 (predicate == Cmp.EQUAL) {
return (key + ":\"" + escapeValue(value) + "\"");
} else if (predicate == Cmp.NOT_EQUAL) {
return ("-" + key + ":\"" + escapeValue(value) + "\"");
} else {
throw new IllegalArgumentException("Relation is not supported for uuid value: " + predicate);
}
} else
throw new IllegalArgumentException("Unsupported type: " + value);
} else if (condition instanceof Not) {
final String sub = buildQueryFilter(((Not) condition).getChild(), information);
if (StringUtils.isNotBlank(sub))
return "-(" + sub + ")";
else
return "";
} else if (condition instanceof And) {
final int numChildren = ((And) condition).size();
final StringBuilder sb = new StringBuilder();
for (final Condition<JanusGraphElement> c : condition.getChildren()) {
final String sub = buildQueryFilter(c, information);
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) {
final StringBuilder sb = new StringBuilder();
int element = 0;
for (final Condition<JanusGraphElement> c : condition.getChildren()) {
final String sub = buildQueryFilter(c, information);
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);
}
}
use of org.janusgraph.core.schema.Mapping in project janusgraph by JanusGraph.
the class LuceneIndex method getSpatialStrategy.
private SpatialStrategy getSpatialStrategy(String key, KeyInformation ki) {
SpatialStrategy strategy = spatial.get(key);
final Mapping mapping = Mapping.getMapping(ki);
final int maxLevels = ParameterType.INDEX_GEO_MAX_LEVELS.findParameter(ki.getParameters(), DEFAULT_GEO_MAX_LEVELS);
final double distErrorPct = ParameterType.INDEX_GEO_DIST_ERROR_PCT.findParameter(ki.getParameters(), DEFAULT_GEO_DIST_ERROR_PCT);
if (strategy == null) {
synchronized (spatial) {
if (!spatial.containsKey(key)) {
// strategy = new RecursivePrefixTreeStrategy(grid, key);
if (mapping == Mapping.DEFAULT) {
strategy = PointVectorStrategy.newInstance(ctx, key);
} else {
final SpatialPrefixTree grid = new QuadPrefixTree(ctx, maxLevels);
strategy = new RecursivePrefixTreeStrategy(grid, key);
((PrefixTreeStrategy) strategy).setDistErrPct(distErrorPct);
}
spatial.put(key, strategy);
} else
return spatial.get(key);
}
}
return strategy;
}
use of org.janusgraph.core.schema.Mapping in project janusgraph by JanusGraph.
the class LuceneIndex method supports.
@Override
public boolean supports(KeyInformation information) {
if (information.getCardinality() != Cardinality.SINGLE)
return false;
final Class<?> dataType = information.getDataType();
final Mapping mapping = Mapping.getMapping(information);
if (Number.class.isAssignableFrom(dataType) || dataType == Date.class || dataType == Instant.class || dataType == Boolean.class || dataType == UUID.class) {
return mapping == Mapping.DEFAULT;
} else if (AttributeUtil.isString(dataType)) {
return mapping == Mapping.DEFAULT || mapping == Mapping.STRING || mapping == Mapping.TEXT;
} else if (AttributeUtil.isGeo(dataType)) {
return mapping == Mapping.DEFAULT || mapping == Mapping.PREFIX_TREE;
}
return false;
}
use of org.janusgraph.core.schema.Mapping in project janusgraph by JanusGraph.
the class LuceneIndex method convertQuery.
private SearchParams convertQuery(Condition<?> condition, final KeyInformation.StoreRetriever information, final LuceneCustomAnalyzer delegatingAnalyzer) {
final SearchParams params = new SearchParams();
if (condition instanceof PredicateCondition) {
final PredicateCondition<String, ?> atom = (PredicateCondition) condition;
Object value = atom.getValue();
final String key = atom.getKey();
final JanusGraphPredicate janusgraphPredicate = atom.getPredicate();
if (value instanceof Number) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on numeric types: " + janusgraphPredicate);
params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, (Number) value));
} else if (value instanceof String) {
if (janusgraphPredicate == Cmp.LESS_THAN) {
params.addQuery(TermRangeQuery.newStringRange(key, null, value.toString(), false, false));
} else if (janusgraphPredicate == Cmp.LESS_THAN_EQUAL) {
params.addQuery(TermRangeQuery.newStringRange(key, null, value.toString(), false, true));
} else if (janusgraphPredicate == Cmp.GREATER_THAN) {
params.addQuery(TermRangeQuery.newStringRange(key, value.toString(), null, false, false));
} else if (janusgraphPredicate == Cmp.GREATER_THAN_EQUAL) {
params.addQuery(TermRangeQuery.newStringRange(key, value.toString(), null, true, false));
} else {
final Mapping map = Mapping.getMapping(information.get(key));
if ((map == Mapping.DEFAULT || map == Mapping.TEXT) && !Text.HAS_CONTAINS.contains(janusgraphPredicate))
throw new IllegalArgumentException("Text mapped string values only support CONTAINS queries and not: " + janusgraphPredicate);
if (map == Mapping.STRING && Text.HAS_CONTAINS.contains(janusgraphPredicate))
throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + janusgraphPredicate);
if (janusgraphPredicate == Text.CONTAINS) {
tokenize(params, map, delegatingAnalyzer, ((String) value).toLowerCase(), key, janusgraphPredicate);
} else if (janusgraphPredicate == Text.CONTAINS_PREFIX) {
tokenize(params, map, delegatingAnalyzer, (String) value, key, janusgraphPredicate);
} else if (janusgraphPredicate == Text.PREFIX) {
params.addQuery(new PrefixQuery(new Term(key, (String) value)));
} else if (janusgraphPredicate == Text.REGEX) {
final RegexpQuery rq = new RegexpQuery(new Term(key, (String) value));
params.addQuery(rq);
} else if (janusgraphPredicate == Text.CONTAINS_REGEX) {
// This is terrible -- there is probably a better way
// putting this to lowercase because Text search is supposed to be case insensitive
final RegexpQuery rq = new RegexpQuery(new Term(key, ".*" + (((String) value).toLowerCase()) + ".*"));
params.addQuery(rq);
} else if (janusgraphPredicate == Cmp.EQUAL) {
tokenize(params, map, delegatingAnalyzer, (String) value, key, janusgraphPredicate);
} else if (janusgraphPredicate == Cmp.NOT_EQUAL) {
final BooleanQuery.Builder q = new BooleanQuery.Builder();
q.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
q.add(new TermQuery(new Term(key, (String) value)), BooleanClause.Occur.MUST_NOT);
params.addQuery(q.build());
} else if (janusgraphPredicate == Text.FUZZY) {
params.addQuery(new FuzzyQuery(new Term(key, (String) value)));
} else if (janusgraphPredicate == Text.CONTAINS_FUZZY) {
value = ((String) value).toLowerCase();
final Builder b = new BooleanQuery.Builder();
for (final String term : Text.tokenize((String) value)) {
b.add(new FuzzyQuery(new Term(key, term)), BooleanClause.Occur.MUST);
}
params.addQuery(b.build());
} else
throw new IllegalArgumentException("Relation is not supported for string value: " + janusgraphPredicate);
}
} else if (value instanceof Geoshape) {
Preconditions.checkArgument(janusgraphPredicate instanceof Geo, "Relation not supported on geo types: " + janusgraphPredicate);
final Shape shape = ((Geoshape) value).getShape();
final SpatialOperation spatialOp = SPATIAL_PREDICATES.get(janusgraphPredicate);
final SpatialArgs args = new SpatialArgs(spatialOp, shape);
params.addQuery(getSpatialStrategy(key, information.get(key)).makeQuery(args));
} else if (value instanceof Date) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on date types: " + janusgraphPredicate);
params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, ((Date) value).getTime()));
} else if (value instanceof Instant) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on instant types: " + janusgraphPredicate);
params.addQuery(numericQuery(key, (Cmp) janusgraphPredicate, ((Instant) value).toEpochMilli()));
} else if (value instanceof Boolean) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on boolean types: " + janusgraphPredicate);
final int intValue;
switch((Cmp) janusgraphPredicate) {
case EQUAL:
intValue = ((Boolean) value) ? 1 : 0;
params.addQuery(IntPoint.newRangeQuery(key, intValue, intValue));
break;
case NOT_EQUAL:
intValue = ((Boolean) value) ? 0 : 1;
params.addQuery(IntPoint.newRangeQuery(key, intValue, intValue));
break;
default:
throw new IllegalArgumentException("Boolean types only support EQUAL or NOT_EQUAL");
}
} else if (value instanceof UUID) {
Preconditions.checkArgument(janusgraphPredicate instanceof Cmp, "Relation not supported on UUID types: " + janusgraphPredicate);
if (janusgraphPredicate == Cmp.EQUAL) {
params.addQuery(new TermQuery(new Term(key, value.toString())));
} else if (janusgraphPredicate == Cmp.NOT_EQUAL) {
final BooleanQuery.Builder q = new BooleanQuery.Builder();
q.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
q.add(new TermQuery(new Term(key, value.toString())), BooleanClause.Occur.MUST_NOT);
params.addQuery(q.build());
} else {
throw new IllegalArgumentException("Relation is not supported for UUID type: " + janusgraphPredicate);
}
} else {
throw new IllegalArgumentException("Unsupported type: " + value);
}
} else if (condition instanceof Not) {
final SearchParams childParams = convertQuery(((Not) condition).getChild(), information, delegatingAnalyzer);
params.addQuery(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
params.addParams(childParams, BooleanClause.Occur.MUST_NOT);
} else if (condition instanceof And) {
for (final Condition c : condition.getChildren()) {
final SearchParams childParams = convertQuery(c, information, delegatingAnalyzer);
params.addParams(childParams, BooleanClause.Occur.MUST);
}
} else if (condition instanceof Or) {
for (final Condition c : condition.getChildren()) {
final SearchParams childParams = convertQuery(c, information, delegatingAnalyzer);
params.addParams(childParams, BooleanClause.Occur.SHOULD);
}
} else
throw new IllegalArgumentException("Invalid condition: " + condition);
return params;
}
Aggregations