Gremlin query to filter or get values in map and List

577 views
Skip to first unread message

Bhuvan Ram

unread,
Sep 23, 2019, 11:40:39 AM9/23/19
to Gremlin-users
{
    "empId": "4944",
    "keywords": [
        "Stark",
        "Love you 3000",
        "IRON MAN"
    ],
    "personalDetails": {
    "name":"Tony Stark",
    "Company":"Stark Industries",
    "strength" : "AI"
    }
   
    }
   
Consider i have a map in metadata in above format. When i perform search operation i need employee Id who has Company Stark Industries in common.
And also I have a "keywords" as list of strings , I need to get employee Id who has  Stark and Iron man as keywords in common.
Help me with the tinkerpop gremlin queries to perform above operation.

Stephen Mallette

unread,
Sep 23, 2019, 3:52:09 PM9/23/19
to gremli...@googlegroups.com
Taking your question word-for-word and using TinkerGraph I guess you could do:

gremlin> v = g.addV('person').property('empId',4944).property('personalDetails',[name:"Tony Stark",company:"Stark Industries",strength:"AI"]).next()
==>v[13]
gremlin> g.V(v).property(list, 'keywords', 'Stark')
==>v[13]
gremlin> g.V(v).property(list, 'keywords', 'Love you 3000')
==>v[13]
gremlin> g.V(v).property(list, 'keywords', 'IRON MAN')
==>v[13]
gremlin> g.V().valueMap()
==>[empId:[4944],keywords:[Stark,Love you 3000,IRON MAN],personalDetails:[[name:Tony Stark,company:Stark Industries,strength:AI]]]
gremlin> g.V().has('keywords','Stark').has('keywords','IRON MAN')
==>v[13]
gremlin> g.V().has('personalDetails').filter(values('personalDetails').select('company').is('Stark Industries'))
==>v[13]

Note that I used multiproperties to store the keywords as that seems like the most natural way to query them for TinkerGraph. I stored your "personalDetails" as a Map as you required and I can produce an answer well enough but that feels a bit clunky to me. Not only does it feel "wrong" but this query won't result in an index hit and is basically a full graph scan. I think I'd much prefer to see you remove "personalDetails" and promote the keys of the Map to first class property keys because then you get the possibility for index lookup and much nicer gremlin:

gremlin> v = g.addV('person').property('empId',4945).property('name',"Tony Stark").property('company',"Stark Industries").property('strength',"AI").next()
==>v[26]
gremlin> g.V().has('company','Stark Industries')
==>v[26]

That's much more intuitive. All that said, this is mostly a solution for TinkerGraph. If you use other graphs (like JanusGraph, DS Graph, Neptune, CosmosDB, etc) they might have other schema definition and index creation functions that will better suit what you're trying to do.


--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/dbcc79f4-a6bf-4f46-91e1-fe7e32de43d0%40googlegroups.com.

Bhuvan Ram

unread,
Sep 24, 2019, 12:24:34 PM9/24/19
to gremli...@googlegroups.com
Thanks a lot Stephen :) for your immediate response :)

The information about list worked fine with few change but when it comes to map. I followed the same what you have mentioned but it throws error for me.
"Unable to find any method 'filter' @ line 1, column 49.\r\n\t1 Error(s)\nSource : Microsoft.Azure.Cosmos.Gremlin.Core\n"
I get this above error.

I have another doubt. 

I need to search values without having any keys. For example i just have the search text as " TONY" . I dint have specified key as "keywords". It should search throughout the  
metadata irrespective of the key. Please help me with this.

Thanks in advance :)

On Monday, September 23, 2019 at 9:10:39 PM UTC+5:30, Bhuvan Ram wrote:
{
    "empId": "4944",
    "keywords": [
        "Stark",
        "Love you 3000",
        "IRON MAN"
    ],
    "personalDetails": {
    "name":"Tony Stark",
    "Company":"Stark Industries",
    "strength" : "AI"
    }
    
    }
    
Consider i have a map in metadata in above format. When i perform search operation i need employee Id who has Company Stark Industries in common.
And also I have a "keywords" as list of strings , I need to get employee Id who has  Stark and Iron man as keywords in common.
Help me with the tinkerpop gremlin queries to perform above operation.

Stephen Mallette

unread,
Sep 24, 2019, 12:29:24 PM9/24/19
to gremli...@googlegroups.com
> Unable to find any method 'filter' @ line 1, column 49.\r\n\t1 Error(s)\nSource : Microsoft.Azure.Cosmos.Gremlin.Core\n" 

maybe try where() rather than filter()

It should search throughout the metadata irrespective of the key.

you might need to choose a graph that supports full text search types of queries/indices and model thing differently then. i don't think cosmosdb does that yet, but i could be wrong.

Bhuvan Ram

unread,
Sep 25, 2019, 11:26:35 AM9/25/19
to gremli...@googlegroups.com
Hi Stephen,

How can we perform Pagination in Gremlin Tinkerpop. I see they have Range Step to perform. But i feel it is similar to limit(). 
Can you please tell me about what is the exact difference between Range and limit.

Thanks,
Bhuvaneshwar 

Stephen Mallette

unread,
Sep 26, 2019, 7:02:02 AM9/26/19
to gremli...@googlegroups.com
The limit() step is basically range(0, limit) so one is not really better than the other. It's just that limit() improves Gremlin readability over range() I think and is convenient. For pagination you should look at this Recipe:


and take a look at this blog post, which is related to CosmosDB, but is generally helpful to anyone using Gremlin


Reply all
Reply to author
Forward
0 new messages