Search code examples
javascriptjquerynosqlgremlinjanusgraph

Query improvement - gremlin


Problem : Here I am trying to retrieve all the post count by its type

Explaining further more,

  • There are 8 vertices ( post_article,post_gallery,post_chirpy...)

  • Each having a property ( 'type','{{vertex_label}}') - for indexing

    Response :{ “data”: { “total_articles”: 500, “total_chirpies”: 250, “total_events”: 600, “total_shouts”: 400, “total_forums”: 750, “total_galleries”: 700, “total_polls”: 900, “total_groups”: 260 }, “meta”: { “status”: 0, “message”: “success” } }

So here I have wrote the below query to get this output

let data = await g
    .V()
    .has("type", 'post_article')
    .count()
    .store("1")
    .V()
    .has("type", 'post_chirpy')
    .count()
    .store("2")
    .V()
    .has("type", 'post_gallery')
    .count()
    .store("3")
    .V()
    .has("type", 'post_shout')
    .count()
    .store("4")
    .V()
    .has("type",'post_forum')
    .count()
    .store("5")
    .V()
    .has("type", 'post_gallery')
    .count()
    .store("6")
    .V()
    .has("type", 'post_poll')
    .count()
    .store("7")
    .V()
    .has("type", 'groups')
    .count()
    .store("8")
    .project(
      "total_articles", //1
      "total_chirpies", //2
      "total_events", //3
      "total_shouts", //4
      "total_forums", //5
      "total_galleries", //6
      "total_polls", //7
      "total_groups" //8
    )
    .by(__.cap("1"))
    .by(__.cap("2"))
    .by(__.cap("3"))
    .by(__.cap("4"))
    .by(__.cap("5"))
    .by(__.cap("6"))
    .by(__.cap("7"))
    .by(__.cap("8"))
    .next();

Is there any way to improve the above query?


Solution

  • Looks like a good time to use groupCount() to me:

    g.V().has("type",within('post_article','post_chirpy',
                            'post_gallery','post_shout',
                            'post_forum','post_gallery',
                            'post_poll','groups')).
      groupCount().by('type')