.and() against mixed indices followed by .limit(...) does not behave as expected
Reference to initial discussion in Google Groups: https://groups.google.com/forum/#!topic/janusgraph-users/lziYf6oocVw
The .and() operator does not appear to behave as one would expect based on the latest 0.3.0 snapshot - possibly introduced after merging PR https://github.com/JanusGraph/janusgraph/issues/163 ?
With the following schema:
p_type_idx = mgmt.makePropertyKey("type").dataType(String.class).make();
p_category_idx = mgmt.makePropertyKey("category").dataType(String.class).make();
mgmt.buildIndex("type_idx", Vertex.class).addKey(p_type_idx).buildCompositeIndex();
mgmt.buildIndex("category_idx", Vertex.class).addKey(p_category_idx).buildCompositeIndex();
p_name_idx = mgmt.makePropertyKey("name").dataType(String.class).make();
p_description_idx = mgmt.makePropertyKey("description").dataType(String.class).make();
mgmt.buildIndex('vmi_descriptors', Vertex.class).addKey(p_type_idx).addKey(p_category_idx).addKey(p_name_idx, Mapping.TEXT.asParameter()).addKey(p_description_idx, Mapping.TEXT.asParameter()).buildMixedIndex("es_search");
Add some data to test (I'm using proprietary data at the moment, but this should suffice to recreate the issue):
graph.addVertex().property('type','location').property('category','GEO').property('name','Fredericksburg, VA').property('description','foo foo');
graph.addVertex().property('type','location').property('category','GEO').property('name','Fredonia, PA').property('description','bar bar');
graph.addVertex().property('type','location').property('category','GEO').property('name','Cape May, NJ').property('description','baz baz');
graph.addVertex().property('type','person').property('category','SOCIAL').property('name','Fred Smith').property('description','cool kid');
graph.addVertex().property('type','person').property('category','SOCIAL').property('name','Fred Castor').property('description','crazy kid');
graph.addVertex().property('type','person').property('category','SOCIAL').property('name','Bill Brasky').property('description','other person');
1.) This works:
gremlin> g.V().has('name',textContainsFuzzy('Fred')).limit(1)
==>v[299152]
2.) As does this now (thanks!):
gremlin> g.V().and(__.has('type','person'), __.has('name',textContainsFuzzy('Fred')))
==>v[2404536]
==>v[299152]
3.) But this returns nothing:
gremlin> g.V().and(__.has('type','person'), __.has('name',textContainsFuzzy('Fred'))).limit(1)
4.) This returns nothing:
gremlin> g.V().has('type','person').has('name',textContainsFuzzy('Fred')).limit(1)
6.) HOWEVER, this DOES return a result:
gremlin> g.V().or(__.has('type','person'), __.has('name',textContainsFuzzy('Fred'))).limit(1)
==>v[2179216]
7.) This actually does return a result, interestingly:
gremlin> g.V().and(__.has('name',textContainsFuzzy('Fred'))).limit(1)
==>v[299152]
Hope this helps. Thanks!
@Phocion what storage backend and indexing backend are you using?
Also, are you running these queries immediately after adding the vertices? Keep in mind there is an availability delay for the indexing backend.
Hi Jason,
Currently using ScyllaDB (2.1.3-0.20180501.8e33e80ad) + Elastic (Version: 5.6.2, Build: 57e20f3/2017-09-23T13:16:45.703Z, JVM: 1.8.0_144)
This isn't immediately after loading data.
The fact that .and(...) works, while .and(...).limit(1) does not, should be somewhat telling as to the nature of the bug, I would think (compare example 2 with 3).
FWIW, I ran your test case against the pre-packaged distribution with C* and ES and it returned the expected results correctly.
gremlin> graph = JanusGraphFactory.open('conf/janusgraph-cassandra-es.properties')
==>standardjanusgraph[cassandrathrift:[127.0.0.1]]
gremlin> mgmt = graph.openManagement()
==>org.janusgraph.graphdb.database.management.ManagementSystem@3a2e9f5b
gremlin> p_type_idx = mgmt.makePropertyKey('type').dataType(String.class).make()
==>type
gremlin> p_category_idx = mgmt.makePropertyKey('category').dataType(String.class).make()
==>category
gremlin> mgmt.buildIndex('type_idx', Vertex.class).addKey(p_type_idx).buildCompositeIndex()
==>type_idx
gremlin> mgmt.buildIndex('category_idx', Vertex.class).addKey(p_category_idx).buildCompositeIndex()
==>category_idx
gremlin> p_name_idx = mgmt.makePropertyKey('name').dataType(String.class).make()
==>name
gremlin> p_description_idx = mgmt.makePropertyKey('description').dataType(String.class).make()
==>description
gremlin> mgmt.buildIndex('vmi_descriptors', Vertex.class).addKey(p_type_idx).addKey(p_category_idx).addKey(p_name_idx, Mapping.TEXT.asParameter()).addKey(p_description_idx, Mapping.TEXT.asParameter()).buildMixedIndex('search')
==>vmi_descriptors
gremlin> mgmt.commit()
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[standardjanusgraph[cassandrathrift:[127.0.0.1]], standard]
gremlin> g.addV().property('type','location').property('category','GEO').property('name','Fredericksburg, VA').property('description','foo foo')
==>v[4208]
gremlin> g.addV().property('type','location').property('category','GEO').property('name','Fredonia, PA').property('description','bar bar')
==>v[4192]
gremlin> g.addV().property('type','location').property('category','GEO').property('name','Cape May, NJ').property('description','baz baz')
==>v[8288]
gremlin> g.addV().property('type','person').property('category','SOCIAL').property('name','Fred Smith').property('description','cool kid')
==>v[8304]
gremlin> g.addV().property('type','person').property('category','SOCIAL').property('name','Fred Castor').property('description','crazy kid')
==>v[4160]
gremlin> g.addV().property('type','person').property('category','SOCIAL').property('name','Bill Brasky').property('description','other person')
==>v[12384]
gremlin> g.tx().commit()
==>null
gremlin> Thread.sleep(10*1000)
==>null
gremlin> g.V().has('name',textContainsFuzzy('Fred')).limit(1)
==>v[8304]
gremlin> g.V().and(__.has('type','person'), __.has('name',textContainsFuzzy('Fred')))
==>v[4160]
==>v[8304]
gremlin> g.V().and(__.has('type','person'), __.has('name',textContainsFuzzy('Fred'))).limit(1)
==>v[4160]
gremlin> g.V().has('type','person').has('name',textContainsFuzzy('Fred')).limit(1)
==>v[4160]
gremlin> g.V().or(__.has('type','person'), __.has('name',textContainsFuzzy('Fred'))).limit(1)
09:24:48 WARN org.janusgraph.graphdb.transaction.StandardJanusGraphTx - Query requires iterating over all vertices [()]. For better performance, use indexes
==>v[8304]
gremlin> g.V().and(__.has('name',textContainsFuzzy('Fred'))).limit(1)
==>v[8304]
This problem is not specific to scylladb. Happens with cassandra too. This bug was introduced in janusgraph 0.2.0.
Commit introducing the bug - Please look at the changes in StandardJanusGraphTx https://github.com/JanusGraph/janusgraph/commit/f87917bc428c8d899e5ede83185708794ce9a255#diff-022be93bbc23d60a3d1be7dc205a12c3
StandardJanusGraphTx (around line 1280). Look at the call to QueryUtil.processIntersectingRetrievals This function takes the first page of elements returned from the first indexQuery and tries to match it against the results from the later indices. If a match is found, it returns the values. If a match is not found, it does not look at the second page from the primary index :( The limit passsed is used in determining the page size of the first one.
I hope this will be helpful in fixing the issue.
@nathsachindra Thank you for adding those details. If you have some insights on how to fix this or even a reproducible test scenario, we'd welcome your contribution.
@davidclement90 Can you please investigate this? Thanks.
The key here is to traverse vertices across all 'selected' indices till we hit the limit or all records are exhausted. The older code prior to the said commit used to do explicitly the same.
To reproduce the issue, have a jg query across two indices. (I noticed this with a mixed index and a composite index). The limit used should be smaller than the number of hits you would get from the first index (with no matches).
In the example code posted above, if you retain type_idx as a compositeIndex and a name_idx as a mixed index, in a query involving the two properties, type_idx will always be the first index used (See GraphCentricQueryBuilder.constructQueryWithoutProfile).
With your test dataset, a query like: #1 g.V().has('type', 'person').has('name', textContains('Bill')).range(0,1).values('name') or #2 g.V().has('type', 'person').has('name', textContains('Bill')).limit(1).values('name') would return no values, whereas #3 g.V().has('type', 'person').has('name', textContains('Bill')).values('name') will get you the values
The current version of this code attempts to match v[8304] with name containing 'Bill'. With a limit = 1 on the base query, it will never find a match. This behavior would be seen for #1 & #2
The older code used to get the next set from the first index, till it found 1 result.
With a limit not set, #3, all three vertices with type = person will be picked up and will hence find a match. This however is not a workaround for a large graph, since there will always be a hard limit in play.
submitted a fix for the issue above, please could committers review? I would say it is a pretty critical issue for anyone who uses Janusgraph with more than one index in their entire schema?..
Fixed by #1330.