0
votes

I am fairly new to graph db's and gremlin and I am having a similar problem to others, see this question, in that I am trying to get the Resource Verticies that meet the all the Criteria of a selected Item. So for the following graph

enter image description here

  • Item 1 should return Resource 1 & Resource 2.
  • Item 2 should return Resource 2 only.

Here's a script to create the sample data:

g.addV("Resource").property("name", "Resource1")
g.addV("Resource").property("name", "Resource2")

g.addV("Criteria").property("name", "Criteria1")
g.addV("Criteria").property("name", "Criteria2")

g.addV("Item").property("name", "Item1")
g.addV("Item").property("name", "Item2")


g.V().has("Resource", "name", "Resource1").addE("isOf").to(g.V().has("Criteria", "name", "Criteria1"))
g.V().has("Resource", "name", "Resource2").addE("isOf").to(g.V().has("Criteria", "name", "Criteria1"))
g.V().has("Resource", "name", "Resource2").addE("isOf").to(g.V().has("Criteria", "name", "Criteria2"))

g.V().has("Item", "name", "Item1").addE("needs").to(g.V().has("Criteria", "name", "Criteria1"))
g.V().has("Item", "name", "Item2").addE("needs").to(g.V().has("Criteria", "name", "Criteria1"))
g.V().has("Item", "name", "Item2").addE("needs").to(g.V().has("Criteria", "name", "Criteria2"))

When I try the following, I get Resource 1 & 2 as it is looking at all related Resources to both Criteria, whereas I only want the Resources that match both Criteria (Resource 2).

g.V()
    .hasLabel('Item')
    .has('name', 'Item2')
    .outE('needs')
    .inV()
    .aggregate("x")
    .inE('isOf')
    .outV()
    .dedup()

So if I try the following, as the referenced question suggests.

g.V()
    .hasLabel('Item')
    .has('name', 'Item2')
    .outE('needs')
    .inV()
    .aggregate("x")
    .inE('isOf')
    .outV()
    .dedup()
    .filter(
        out("isOf")
        .where(within("x"))
        .count()
        .where(eq("x"))
        .by()
        .by(count(local)))
        .valueMap()

I get the following exception as the answer provided for the other question doesn't work in Azure CosmosDB graph database as it doesn't support the gremlin Filter statement.

Failure in submitting query: g.V().hasLabel('Item').has('name', 'Item2').outE('needs').inV().aggregate("x").inE('isOf').outV().dedup().filter(out("isOf").where(within("x")).count().where(eq("x")).by().by(count(local))).valueMap(): "Script eval error: \r\n\nActivityId : d2eccb49-9ca5-4ac6-bfd7-b851d63662c9\nExceptionType : GraphCompileException\nExceptionMessage :\r\n\tGremlin Query Compilation Error: Unable to find any method 'filter' @ line 1, column 113.\r\n\t1 Error(s)\nSource : Microsoft.Azure.Cosmos.Gremlin.Core\n\tGremlinRequestId : d2eccb49-9ca5-4ac6-bfd7-b851d63662c9\n\tContext : graphcompute\n\tScope : graphparse-translate-csharpexpressionbinding\n\tGraphInterOpStatusCode : QuerySyntaxError\n\tHResult : 0x80131500\r\n"

I'm interested to know if there is a way to solve my problem with the gremlin steps MS provide in Azure CosmosDB (these).

1

1 Answers

2
votes

Just replace filter with where. The query will work equally the same.