Geo Mapping. How to index/query a non-point geo property?
Hello, guys!
I've tried to query complex geoshape, but got empty response. What am I doing wrong?
My query:
transaction = client.submit(
f"""
g.V().has("location", geoWithin(Geoshape.circle({lat}, {lon}, {radius})))
"""
)
Index creation:
transaction = client.submit(
"""
mgmt = graph.openManagement()
location = mgmt.makePropertyKey('location').dataType(Geoshape.class).cardinality(Cardinality.SINGLE).make()
mgmt.buildIndex('byLocation', Vertex.class).addKey(location, Mapping.PREFIX_TREE.asParameter()).buildMixedIndex('search')
mgmt.commit()
"""
)
Vertex creation:
transaction = client.submit(
f"""
g.addV("place").property("location", {graph_geoshape.to_janus_graph_property()})
"""
)
Schema looks like this:
[ '------------------------------------------------------------------------------------------------\n'
'Vertex Label Name | Partitioned | Static |\n'
'---------------------------------------------------------------------------------------------------\n'
'place | false | false |\n'
'location | false | false |\n'
'---------------------------------------------------------------------------------------------------\n'
'Edge Label Name | Directed | Unidirected | Multiplicity |\n'
'---------------------------------------------------------------------------------------------------\n'
'---------------------------------------------------------------------------------------------------\n'
'Property Key Name | Cardinality | Data Type |\n'
'---------------------------------------------------------------------------------------------------\n'
'location | SINGLE | class org.janusgraph.core.attribute.Geoshape |\n'
'place | SINGLE | class org.janusgraph.core.attribute.Geoshape |\n'
'---------------------------------------------------------------------------------------------------\n'
'Graph Index (Vertex) | Type | Unique | Backing | Key: Status |\n'
'---------------------------------------------------------------------------------------------------\n'
'byLocation | Mixed | false | search | location: ENABLED |\n'
'---------------------------------------------------------------------------------------------------\n'
'Graph Index (Edge) | Type | Unique | Backing | Key: Status |\n'
'---------------------------------------------------------------------------------------------------\n'
'---------------------------------------------------------------------------------------------------\n'
'Relation Index (VCI) | Type | Direction | Sort Key | Order | Status |\n'
'---------------------------------------------------------------------------------------------------\n']
It is not clear to me whether you have problems to get any geo predicate working or that your specific example is the issue.
Can you first confirm that the following works for you (on a clean janusgraph-full-0.6.1):
$ bin/janusgraph.sh start
$ bin/gremlin.sh
graph = JanusGraphFactory.open('conf/janusgraph-cql-es.properties')
mgmt = graph.openManagement()
location = mgmt.makePropertyKey('location').dataType(Geoshape.class).cardinality(Cardinality.SINGLE).make()
mgmt.buildIndex('byLocation', Vertex.class).addKey(location, Mapping.PREFIX_TREE.asParameter()).buildMixedIndex('search')
mgmt.commit()
mgmt = graph.openManagement()
mgmt.printSchema()
mgmt.close()
g = graph.traversal()
g.addV().property('location', Geoshape.point(52, 1))
g.V().has("location", geoWithin(Geoshape.circle(51.9, 1.1, 20.0)))
Note that you may have erred on the radius units: these seem to be in kilometers (not miles, I hope, did not do the calculation...).
Best wishes, Marc
On Wed, Mar 30, 2022 at 03:57 PM, <dmitryzezix@...> wrote:
g.V().has("location", geoWithin(Geoshape.circle({lat}, {lon}, {radius})))
Here you go again:
g.addV().property('location', Geoshape.line([[52, 0] as double[], [52, 2] as double[]]))
g.V().elementMap()
21:09:39 WARN org.janusgraph.graphdb.transaction.StandardJanusGraphTx - Query requires iterating over all vertices [()]. For better performance, use indexes
==>[id:4240,label:vertex,location:LINESTRING (52 0, 52 2)]
gremlin> g.V().has("location", geoWithin(Geoshape.circle(1, 52, 200.0))).elementMap()
==>[id:4240,label:vertex,location:LINESTRING (52 0, 52 2)]
gremlin> g.V().has("location", geoContains(Geoshape.point(1, 52)))
==>v[4240]
As you see, the coordinate order conventions are really warped. This may have led you believe things do not work. I had to find this out too ... Still do not not know which coordinate is latitude and which one longitude :-)
If you want, you can make an issue of it, because the coordinate order for the geoWithin predicate is different for Geoshape.point and Geoshape.circle!
Best wishes, Marc
Can you please present an easily reproducible scenario, preferably using the default "bin/janusgraph.sh start", like I showed, with gremlin console output and starting with an empty db/cassandra and db/es directories. From your description it is not clear what exactly happened.
And to be sure, when you first added nodes and then defined the mixed index, you also made sure that the graph was reindexed.
Regards, Marc
Hi Marc,
I confirm, that this works:
g.V().has("location", geoContains(Geoshape.point({lat}, {lon}))).elementMap()
g.V().has("location", geoIntersect(Geoshape.circle({lat}, {lon}, {radius}))).elementMap()
My mistake. I misunderstood the GeoPredicates and also the coordinate order conventions were confusing. I've managed to make my code work! Thanks for your help