gremlin

Cannot make Gremlin queries to return what I need


I have this test data.

{
    "entityCode": "1ae1067d-2590-4f2e-ad74-f888a7ea7193",
    "entityName": "Arda",
    "childEntities": [
     {
         "entityCode": "ab5368aa-7abc-4f31-b980-7042fb1943e8",
         "entityName": "Middle Earth",
         "childEntities": [
          {
              "entityCode": "b81636a3-8e21-4ad9-a299-39eed1936a5d",
              "entityName": "Gondor",
              "childEntities": []
          }]

     }]
}

I have two working queries, one to return entity with the ancestors, the 2nd, to return with the descendants.

With the ancestors

g.V()
.has('_affinity', 'geography')
.has('isDeleted', false)
.not(outE('isChildOf').has('hierarchyCollection', 'default'))
.where(inE('isChildOf').has('hierarchyCollection', 'default').has('isDeleted', false))
.emit()
.repeat(inE('isChildOf').has('hierarchyCollection', 'default').has('isDeleted', false)
.dedup().outV().dedup().has('_affinity', 'geography').has('isDeleted', false))
.has('id', 'ab5368aa-7abc-4f31-b980-7042fb1943e8')
.tree()

With the descendants

g.V('ab5368aa-7abc-4f31-b980-7042fb1943e8')
.has('_affinity', 'geography')
.has('isDeleted', false)
.emit()
.repeat(inE('isChildOf').has('hierarchyCollection', 'default').has('isDeleted', false)
.dedup().outV().dedup().has('_affinity', 'geography').has('isDeleted', false))
.tree()

This returns the ancestor

[
  {
    "1ae1067d-2590-4f2e-ad74-f888a7ea7193": {
      "key": {
        "id": "1ae1067d-2590-4f2e-ad74-f888a7ea7193",
        "label": "geography",
        "type": "vertex",
        "properties": {
          "_affinity": [
            {
              "id": "1ae1067d-2590-4f2e-ad74-f888a7ea7193|_affinity",
              "value": "geography"
            }
          ],
          "isDeleted": [
            {
              "id": "266fb8c3-588f-4ac6-93bf-912cb965349a",
              "value": false
            }
          ],
          "entityCode": [
            {
              "id": "e57d00fb-dba6-4792-9763-fbfa2b75788b",
              "value": "1ae1067d-2590-4f2e-ad74-f888a7ea7193"
            }
          ]
        }
      },
      "value": {
        "cf5e5c4f-3a54-48dd-8d3d-aebec1b7b406": {
          "key": {
            "id": "cf5e5c4f-3a54-48dd-8d3d-aebec1b7b406",
            "label": "isChildOf",
            "type": "edge",
            "inVLabel": "geography",
            "outVLabel": "geography",
            "inV": "1ae1067d-2590-4f2e-ad74-f888a7ea7193",
            "outV": "ab5368aa-7abc-4f31-b980-7042fb1943e8",
            "properties": {
              "hierarchyCollection": "default",
              "isDeleted": false
            }
          },
          "value": {
            "ab5368aa-7abc-4f31-b980-7042fb1943e8": {
              "key": {
                "id": "ab5368aa-7abc-4f31-b980-7042fb1943e8",
                "label": "geography",
                "type": "vertex",
                "properties": {
                  "_affinity": [
                    {
                      "id": "ab5368aa-7abc-4f31-b980-7042fb1943e8|_affinity",
                      "value": "geography"
                    }
                  ],
                  "isDeleted": [
                    {
                      "id": "196b8bbd-1c1e-4f58-8ab9-9a2b858550f7",
                      "value": false
                    }
                  ],
                  "entityCode": [
                    {
                      "id": "1b7e64a6-0b73-40ce-8679-e33b9d0e1fa3",
                      "value": "ab5368aa-7abc-4f31-b980-7042fb1943e8"
                    }
                  ]
                }
              },
              "value": {}
            }
          }
        }
      }
    }
  }
]

as well as the descendant

[
  {
    "ab5368aa-7abc-4f31-b980-7042fb1943e8": {
      "key": {
        "id": "ab5368aa-7abc-4f31-b980-7042fb1943e8",
        "label": "geography",
        "type": "vertex",
        "properties": {
          "_affinity": [
            {
              "id": "ab5368aa-7abc-4f31-b980-7042fb1943e8|_affinity",
              "value": "geography"
            }
          ],
          "isDeleted": [
            {
              "id": "196b8bbd-1c1e-4f58-8ab9-9a2b858550f7",
              "value": false
            }
          ],
          "entityCode": [
            {
              "id": "1b7e64a6-0b73-40ce-8679-e33b9d0e1fa3",
              "value": "ab5368aa-7abc-4f31-b980-7042fb1943e8"
            }
          ]
        }
      },
      "value": {
        "e21c09e1-5308-41cd-bf33-ec0f7e1931dc": {
          "key": {
            "id": "e21c09e1-5308-41cd-bf33-ec0f7e1931dc",
            "label": "isChildOf",
            "type": "edge",
            "inVLabel": "geography",
            "outVLabel": "geography",
            "inV": "ab5368aa-7abc-4f31-b980-7042fb1943e8",
            "outV": "b81636a3-8e21-4ad9-a299-39eed1936a5d",
            "properties": {
              "hierarchyCollection": "default",
              "isDeleted": false
            }
          },
          "value": {
            "b81636a3-8e21-4ad9-a299-39eed1936a5d": {
              "key": {
                "id": "b81636a3-8e21-4ad9-a299-39eed1936a5d",
                "label": "geography",
                "type": "vertex",
                "properties": {
                  "_affinity": [
                    {
                      "id": "b81636a3-8e21-4ad9-a299-39eed1936a5d|_affinity",
                      "value": "geography"
                    }
                  ],
                  "isDeleted": [
                    {
                      "id": "e5560c3d-aca6-455a-8184-09f102b7a532",
                      "value": false
                    }
                  ],
                  "entityCode": [
                    {
                      "id": "dcec8e42-afad-4cef-8362-614f2591dfe1",
                      "value": "b81636a3-8e21-4ad9-a299-39eed1936a5d"
                    }
                  ]
                }
              },
              "value": {}
            }
          }
        }
      }
    }
  }
]

What I need now is not the entire entities, but the three entityCodes only (or, two and two, but one is the same one).

I have tried the following statements:

  1. .select('entityCode'),

  2. .select('id')

  3. .unfold()

  4. .cap('entityCode')

  5. .path().by('entityCode')

but the result is always wrong. Either it is all the Guids in the database, or an empty array, or different kind of exceptions. I am not familiar with the gremlin syntax, so unsure what to try more. Please advise.


Solution

  • I dropped the idea of using the existing query and started from scratch. These two worked:

    For the descendants

            var query = $"g.V('ab5368aa-7abc-4f31-b980-7042fb1943e8')" +
                        $".has('_affinity', 'geography')" +
                        $".inE('isChildOf')" +
                        $".has('hierarchyCollection', 'default')" +
                        $".has('isDeleted', false)" +
                        $".outV()" +
                        $".emit()" +
                        $".repeat(inE('isChildOf').has('_affinity', 'geography').has('isDeleted', false).has('hierarchyCollection', 'default').outV())" +
                        $".values('entityCode')";
    

    And for the ancestors

    var query = $"g.V('ab5368aa-7abc-4f31-b980-7042fb1943e8')" +
                $".has('_affinity', 'geography')" +
                $".outE('isChildOf')" +
                $".has('hierarchyCollection', 'default')" +
                $".has('isDeleted', false)" +
                $".inV()" +
                $".has('_affinity', 'geography')" +
                $".has('isDeleted', false)" +
                $".emit()" +
                $".repeat(outE('isChildOf').has('hierarchyCollection', 'default').has('isDeleted', false).inV().has('_affinity', 'geography').has('isDeleted', false))" +
                $".values('entityCode')";