use of org.janusgraph.core.attribute.Geoshape 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.attribute.Geoshape in project janusgraph by JanusGraph.
the class JanusGraphIoRegistryTest method testLegacyGeoshapAsGryo.
@Test
public void testLegacyGeoshapAsGryo() {
final Geoshape point = Geoshape.point(1.0d, 4.0d);
Kryo kryo = new Kryo();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
Output output = new Output(outStream, 4096);
output.writeLong(1);
output.writeFloat((float) point.getPoint().getLatitude());
output.writeFloat((float) point.getPoint().getLongitude());
output.flush();
Geoshape.GeoShapeGryoSerializer serializer = new Geoshape.GeoShapeGryoSerializer();
Input input = new Input(new ByteArrayInputStream(outStream.toByteArray()), 4096);
assertEquals(point, serializer.read(kryo, input, Geoshape.class));
}
use of org.janusgraph.core.attribute.Geoshape in project janusgraph by JanusGraph.
the class LuceneExample method indexDocs.
void indexDocs(IndexWriter writer, String documentId, Map<String, Object> docMap) throws IOException {
Document doc = new Document();
Field documentIdField = new StringField("docid", documentId, Field.Store.YES);
doc.add(documentIdField);
for (Map.Entry<String, Object> kv : docMap.entrySet()) {
String key = kv.getKey();
Object value = kv.getValue();
if (value instanceof Number) {
final Field field;
if (value instanceof Integer || value instanceof Long) {
field = new LongPoint(key, ((Number) value).longValue());
} else {
// double or float
field = new DoublePoint(key, ((Number) value).doubleValue());
}
doc.add(field);
} else if (value instanceof String) {
String str = (String) value;
Field field = new TextField(key + TXT_SUFFIX, str, Field.Store.NO);
doc.add(field);
if (str.length() < 256)
field = new StringField(key + STR_SUFFIX, str, Field.Store.NO);
doc.add(field);
} else if (value instanceof Geoshape) {
Shape shape = ((Geoshape) value).getShape();
for (IndexableField f : getSpatialStrategy(key).createIndexableFields(shape)) {
doc.add(f);
}
} else
throw new IllegalArgumentException("Unsupported type: " + value);
}
writer.updateDocument(new Term("docid", documentId), doc);
}
use of org.janusgraph.core.attribute.Geoshape in project janusgraph by JanusGraph.
the class ElasticSearchIndex method getFilter.
public Map<String, Object> getFilter(Condition<?> condition, KeyInformation.StoreRetriever information) {
if (condition instanceof PredicateCondition) {
final PredicateCondition<String, ?> atom = (PredicateCondition) condition;
Object value = atom.getValue();
final String key = atom.getKey();
final JanusGraphPredicate predicate = atom.getPredicate();
if (value instanceof Number) {
Preconditions.checkArgument(predicate instanceof Cmp, "Relation not supported on numeric types: " + predicate);
final Cmp numRel = (Cmp) predicate;
switch(numRel) {
case EQUAL:
return compat.term(key, value);
case NOT_EQUAL:
return compat.boolMustNot(compat.term(key, value));
case LESS_THAN:
return compat.lt(key, value);
case LESS_THAN_EQUAL:
return compat.lte(key, value);
case GREATER_THAN:
return compat.gt(key, value);
case GREATER_THAN_EQUAL:
return compat.gte(key, value);
default:
throw new IllegalArgumentException("Unexpected relation: " + numRel);
}
} else if (value instanceof String) {
final Mapping mapping = getStringMapping(information.get(key));
final String fieldName;
if (mapping == 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 (mapping == Mapping.STRING && Text.HAS_CONTAINS.contains(predicate))
throw new IllegalArgumentException("String mapped string values do not support CONTAINS queries: " + predicate);
if (mapping == Mapping.TEXTSTRING && !(Text.HAS_CONTAINS.contains(predicate) || predicate instanceof Cmp)) {
fieldName = getDualMappingName(key);
} else {
fieldName = key;
}
if (predicate == Text.CONTAINS || predicate == Cmp.EQUAL) {
return compat.match(key, value);
} else if (predicate == Text.CONTAINS_PREFIX) {
if (!ParameterType.TEXT_ANALYZER.hasParameter(information.get(key).getParameters()))
value = ((String) value).toLowerCase();
return compat.prefix(fieldName, value);
} else if (predicate == Text.CONTAINS_REGEX) {
if (!ParameterType.TEXT_ANALYZER.hasParameter(information.get(key).getParameters()))
value = ((String) value).toLowerCase();
return compat.regexp(fieldName, value);
} else if (predicate == Text.PREFIX) {
return compat.prefix(fieldName, value);
} else if (predicate == Text.REGEX) {
return compat.regexp(fieldName, value);
} else if (predicate == Cmp.NOT_EQUAL) {
return compat.boolMustNot(compat.match(fieldName, value));
} else if (predicate == Text.FUZZY || predicate == Text.CONTAINS_FUZZY) {
return compat.fuzzyMatch(fieldName, value);
} else if (predicate == Cmp.LESS_THAN) {
return compat.lt(fieldName, value);
} else if (predicate == Cmp.LESS_THAN_EQUAL) {
return compat.lte(fieldName, value);
} else if (predicate == Cmp.GREATER_THAN) {
return compat.gt(fieldName, value);
} else if (predicate == Cmp.GREATER_THAN_EQUAL) {
return compat.gte(fieldName, value);
} else
throw new IllegalArgumentException("Predicate is not supported for string value: " + predicate);
} else if (value instanceof Geoshape && Mapping.getMapping(information.get(key)) == Mapping.DEFAULT) {
// geopoint
final Geoshape shape = (Geoshape) value;
Preconditions.checkArgument(predicate instanceof Geo && predicate != Geo.CONTAINS, "Relation not supported on geopoint types: " + predicate);
final Map<String, Object> query;
switch(shape.getType()) {
case CIRCLE:
final Geoshape.Point center = shape.getPoint();
query = compat.geoDistance(key, center.getLatitude(), center.getLongitude(), shape.getRadius());
break;
case BOX:
final Geoshape.Point southwest = shape.getPoint(0);
final Geoshape.Point northeast = shape.getPoint(1);
query = compat.geoBoundingBox(key, southwest.getLatitude(), southwest.getLongitude(), northeast.getLatitude(), northeast.getLongitude());
break;
case POLYGON:
final List<List<Double>> points = IntStream.range(0, shape.size()).mapToObj(i -> ImmutableList.of(shape.getPoint(i).getLongitude(), shape.getPoint(i).getLatitude())).collect(Collectors.toList());
query = compat.geoPolygon(key, points);
break;
default:
throw new IllegalArgumentException("Unsupported or invalid search shape type for geopoint: " + shape.getType());
}
return predicate == Geo.DISJOINT ? compat.boolMustNot(query) : query;
} else if (value instanceof Geoshape) {
Preconditions.checkArgument(predicate instanceof Geo, "Relation not supported on geoshape types: " + predicate);
final Geoshape shape = (Geoshape) value;
final Map<String, Object> geo;
switch(shape.getType()) {
case CIRCLE:
final Geoshape.Point center = shape.getPoint();
geo = ImmutableMap.of(ES_TYPE_KEY, "circle", ES_GEO_COORDS_KEY, ImmutableList.of(center.getLongitude(), center.getLatitude()), "radius", shape.getRadius() + "km");
break;
case BOX:
final Geoshape.Point southwest = shape.getPoint(0);
final Geoshape.Point northeast = shape.getPoint(1);
geo = ImmutableMap.of(ES_TYPE_KEY, "envelope", ES_GEO_COORDS_KEY, ImmutableList.of(ImmutableList.of(southwest.getLongitude(), northeast.getLatitude()), ImmutableList.of(northeast.getLongitude(), southwest.getLatitude())));
break;
case LINE:
final List lineCoords = IntStream.range(0, shape.size()).mapToObj(i -> ImmutableList.of(shape.getPoint(i).getLongitude(), shape.getPoint(i).getLatitude())).collect(Collectors.toList());
geo = ImmutableMap.of(ES_TYPE_KEY, "linestring", ES_GEO_COORDS_KEY, lineCoords);
break;
case POLYGON:
final List polyCoords = IntStream.range(0, shape.size()).mapToObj(i -> ImmutableList.of(shape.getPoint(i).getLongitude(), shape.getPoint(i).getLatitude())).collect(Collectors.toList());
geo = ImmutableMap.of(ES_TYPE_KEY, "polygon", ES_GEO_COORDS_KEY, ImmutableList.of(polyCoords));
break;
case POINT:
geo = ImmutableMap.of(ES_TYPE_KEY, "point", ES_GEO_COORDS_KEY, ImmutableList.of(shape.getPoint().getLongitude(), shape.getPoint().getLatitude()));
break;
default:
throw new IllegalArgumentException("Unsupported or invalid search shape type: " + shape.getType());
}
return compat.geoShape(key, geo, (Geo) predicate);
} else if (value instanceof Date || value instanceof Instant) {
Preconditions.checkArgument(predicate instanceof Cmp, "Relation not supported on date types: " + predicate);
final Cmp numRel = (Cmp) predicate;
if (value instanceof Instant) {
value = Date.from((Instant) value);
}
switch(numRel) {
case EQUAL:
return compat.term(key, value);
case NOT_EQUAL:
return compat.boolMustNot(compat.term(key, value));
case LESS_THAN:
return compat.lt(key, value);
case LESS_THAN_EQUAL:
return compat.lte(key, value);
case GREATER_THAN:
return compat.gt(key, value);
case GREATER_THAN_EQUAL:
return compat.gte(key, value);
default:
throw new IllegalArgumentException("Unexpected relation: " + numRel);
}
} else if (value instanceof Boolean) {
final Cmp numRel = (Cmp) predicate;
switch(numRel) {
case EQUAL:
return compat.term(key, value);
case NOT_EQUAL:
return compat.boolMustNot(compat.term(key, value));
default:
throw new IllegalArgumentException("Boolean types only support EQUAL or NOT_EQUAL");
}
} else if (value instanceof UUID) {
if (predicate == Cmp.EQUAL) {
return compat.term(key, value);
} else if (predicate == Cmp.NOT_EQUAL) {
return compat.boolMustNot(compat.term(key, value));
} else {
throw new IllegalArgumentException("Only equal or not equal is supported for UUIDs: " + predicate);
}
} else
throw new IllegalArgumentException("Unsupported type: " + value);
} else if (condition instanceof Not) {
return compat.boolMustNot(getFilter(((Not) condition).getChild(), information));
} else if (condition instanceof And) {
final List queries = StreamSupport.stream(condition.getChildren().spliterator(), false).map(c -> getFilter(c, information)).collect(Collectors.toList());
return compat.boolMust(queries);
} else if (condition instanceof Or) {
final List queries = StreamSupport.stream(condition.getChildren().spliterator(), false).map(c -> getFilter(c, information)).collect(Collectors.toList());
return compat.boolShould(queries);
} else
throw new IllegalArgumentException("Invalid condition: " + condition);
}
use of org.janusgraph.core.attribute.Geoshape in project janusgraph by JanusGraph.
the class GeoshapeTest method testEquality.
@Test
public void testEquality() {
Geoshape c = Geoshape.circle(10.0, 12.5, 100);
Geoshape b = Geoshape.box(20.0, 22.5, 40.5, 60.5);
Geoshape l = Geoshape.line(Arrays.asList(new double[][] { { 10.5, 20.5 }, { 10.5, 22.5 }, { 12.5, 22.5 } }));
Geoshape p = Geoshape.polygon(Arrays.asList(new double[][] { { 10.5, 20.5 }, { 8.0, 21.75 }, { 10.5, 22.5 }, { 11.75, 25.0 }, { 12.5, 22.5 }, { 15.0, 21.0 }, { 12.5, 20.5 }, { 11.75, 18.0 }, { 10.5, 20.5 } }));
assertEquals(Geoshape.circle(10.0, 12.5, 100), c);
assertEquals(Geoshape.box(20.0, 22.5, 40.5, 60.5), b);
assertEquals(Geoshape.line(Arrays.asList(new double[][] { { 10.5, 20.5 }, { 10.5, 22.5 }, { 12.5, 22.5 } })), l);
assertEquals(Geoshape.polygon(Arrays.asList(new double[][] { { 10.5, 20.5 }, { 8.0, 21.75 }, { 10.5, 22.5 }, { 11.75, 25.0 }, { 12.5, 22.5 }, { 15.0, 21.0 }, { 12.5, 20.5 }, { 11.75, 18.0 }, { 10.5, 20.5 } })), p);
assertEquals(Geoshape.circle(10.0, 12.5, 100).hashCode(), c.hashCode());
assertEquals(Geoshape.box(20.0, 22.5, 40.5, 60.5).hashCode(), b.hashCode());
assertEquals(Geoshape.line(Arrays.asList(new double[][] { { 10.5, 20.5 }, { 10.5, 22.5 }, { 12.5, 22.5 } })).hashCode(), l.hashCode());
assertEquals(Geoshape.polygon(Arrays.asList(new double[][] { { 10.5, 20.5 }, { 8.0, 21.75 }, { 10.5, 22.5 }, { 11.75, 25.0 }, { 12.5, 22.5 }, { 15.0, 21.0 }, { 12.5, 20.5 }, { 11.75, 18.0 }, { 10.5, 20.5 } })).hashCode(), p.hashCode());
assertNotSame(c.hashCode(), b.hashCode());
assertNotSame(c.hashCode(), l.hashCode());
assertNotSame(c.hashCode(), p.hashCode());
assertNotSame(b.hashCode(), l.hashCode());
assertNotSame(b.hashCode(), p.hashCode());
assertNotSame(l.hashCode(), p.hashCode());
assertNotSame(c, b);
assertNotSame(c, l);
assertNotSame(c, p);
assertNotSame(b, l);
assertNotSame(b, p);
assertNotSame(l, p);
}
Aggregations