Skip to content

Instantly share code, notes, and snippets.

@espeed espeed/gist:10725793
Last active Aug 29, 2015

Embed
What would you like to do?
Bulbs-Rexster Unicode Lookup via Gremlin
# The vertex is created and indexed via a Gremlin script...
>>> name = 'Universit\u00e9 de Montr\u00e9al'
>>> g.university.create(name=name)
POST url: http://localhost:8182/graphs/emptygraph/tp/gremlin
POST body: {"params":{"keys":null,"index_name":"university","data":{"element_type":"university","name":"Universit\u00e9 de Montr\u00e9al"}},"script":"def createIndexedVertex = {\n vertex = g.addVertex()\n index = g.idx(index_name)\n for (entry in data.entrySet()) {\n if (entry.value == null) continue;\n vertex.setProperty(entry.key,entry.value)\n if (keys == null || keys.contains(entry.key))\n\tindex.put(entry.key,String.valueOf(entry.value),vertex)\n }\n return vertex\n }\n def transaction = { final Closure closure ->\n try {\n results = closure();\n g.commit();\n return results; \n } catch (e) {\n g.rollback();\n throw e;\n }\n }\n return transaction(createIndexedVertex);"}
<University: http://localhost:8182/graphs/emptygraph/vertices/0>
# This works...
>>> params = dict(key="name", value=u"Universit\u00e9 de Montr\u00e9al")
>>> g.gremlin.query(script, params).next().data()
POST url: http://localhost:8182/graphs/emptygraph/tp/gremlin
POST body: {"params":{"value":"Universit\u00e9 de Montr\u00e9al","key":"name"},"script":"g.V(key, value)"}
{u'element_type': u'university', u'name': u'Universit\xe9 de Montr\xe9al'}
>>> g.university.get(0)._data
GET url: http://localhost:8182/graphs/emptygraph/vertices/0
GET body: None
{u'element_type': u'university', u'name': u'Universit\xe9 de Montr\xe9al'}
# This doesn't...
>>> name = 'Universit\u00e9 de Montr\u00e9al'
>>> g.university.index.lookup(name=name)
GET url: http://localhost:8182/graphs/emptygraph/indices/university?value=Universit%5Cu00e9+de+Montr%5Cu00e9al&key=name
GET body: None
# curl output...
[james@life ~]$ curl "http://localhost:8182/graphs/emptygraph/indices/university?value=Universit%5Cu00e9+de+Montr%5Cu00e9al&key=name"
{"version":"2.5.0-SNAPSHOT","results":[],"totalSize":0,"queryTime":0.458599}
# it works with a normal value ("James")...
>>> g.university.create(name="James")
POST url: http://localhost:8182/graphs/emptygraph/tp/gremlin
POST body: {"params":{"keys":null,"index_name":"university","data":{"element_type":"university","name":"James"}},"script":"def createIndexedVertex = {\n vertex = g.addVertex()\n index = g.idx(index_name)\n for (entry in data.entrySet()) {\n if (entry.value == null) continue;\n vertex.setProperty(entry.key,entry.value)\n if (keys == null || keys.contains(entry.key))\n\tindex.put(entry.key,String.valueOf(entry.value),vertex)\n }\n return vertex\n }\n def transaction = { final Closure closure ->\n try {\n results = closure();\n g.commit();\n return results; \n } catch (e) {\n g.rollback();\n throw e;\n }\n }\n return transaction(createIndexedVertex);"}
<University: http://localhost:8182/graphs/emptygraph/vertices/1>
>>> g.university.index.lookup(name="James").next().data()
GET url: http://localhost:8182/graphs/emptygraph/indices/university?value=James&key=name
GET body: None
{u'element_type': u'university', u'name': u'James'}
#
# UPDATE & CORRECTION: It DOES work with a manual named index in Gremlin...
#
>>> name =u'Université de Montréal'
>>> name
u'Universit\xe9 de Montr\xe9al'
>>> script = "g.idx(index_name).get(key, value)"
>>> params = dict(index_name="university", key="name", value=name)
>>> g.gremlin.execute(script, params).raw
POST url: http://localhost:8182/graphs/emptygraph/tp/gremlin
POST body: {"params":{"index_name":"university","value":"Universit\u00e9 de Montr\u00e9al","key":"name"},"script":"g.idx(index_name).get(key, value)"}
({'status': '200', 'transfer-encoding': 'chunked', 'server': 'grizzly/2.2.16', 'date': 'Tue, 15 Apr 2014 13:11:04 GMT', 'access-control-allow-origin': '*', 'content-type': 'application/json;charset=UTF-8'}, '{"results":[{"name":"Universit\xc3\xa9 de Montr\xc3\xa9al","element_type":"university","_id":"0","_type":"vertex"}],"success":true,"version":"2.5.0-SNAPSHOT","queryTime":3.887186}')
# And it works when the Gremlin script is g.V(key, value)...
>>> name =u'Université de Montréal'
>>> name
u'Universit\xe9 de Montr\xe9al'
>>> params = dict(index_name="university", key="name", value=name)
>>> script = "g.V(key, value)"
>>> g.gremlin.execute(script, params).raw
POST url: http://localhost:8182/graphs/emptygraph/tp/gremlin
POST body: {"params":{"index_name":"university","value":"Universit\u00e9 de Montr\u00e9al","key":"name"},"script":"g.V(key, value)"}
({'status': '200', 'transfer-encoding': 'chunked', 'server': 'grizzly/2.2.16', 'date': 'Tue, 15 Apr 2014 13:09:31 GMT', 'access-control-allow-origin': '*', 'content-type': 'application/json;charset=UTF-8'}, '{"results":[{"name":"Universit\xc3\xa9 de Montr\xc3\xa9al","element_type":"university","_id":"0","_type":"vertex"}],"success":true,"version":"2.5.0-SNAPSHOT","queryTime":3.799427}')
@spmallette

This comment has been minimized.

Copy link

spmallette commented Apr 15, 2014

seems to work as a key index lookup...

curl -v "http://localhost:8182/graphs/emptygraph/vertices?key=name&value=Universit%5Cu00e9%20de%20Montr%5Cu00e9al"
* About to connect() to localhost port 8182 (#0)
*   Trying 127.0.0.1... connected
> GET /graphs/emptygraph/vertices?key=name&value=Universit%5Cu00e9%20de%20Montr%5Cu00e9al HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: localhost:8182
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: application/json;charset=UTF-8
< server: grizzly/2.2.16
< Access-Control-Allow-Origin: *
< Date: Tue, 15 Apr 2014 12:11:50 GMT
< Content-Length: 138
< 
* Connection #0 to host localhost left intact
* Closing connection #0
{"version":"2.5.0","results":[{"name":"Universit\\u00e9 de Montr\\u00e9al","_id":"1","_type":"vertex"}],"totalSize":1,"queryTime":7.35214}
@espeed

This comment has been minimized.

Copy link
Owner Author

espeed commented Apr 15, 2014

@spmallette, how did you create the vertex above?

@spmallette

This comment has been minimized.

Copy link

spmallette commented Apr 15, 2014

curl -v "http://localhost:8182/graphs/emptygraph/indices/uni?key=name&value=Universit%5Cu00e9%20de%20Montr%5Cu00e9al"
* About to connect() to localhost port 8182 (#0)
*   Trying 127.0.0.1... connected
> GET /graphs/emptygraph/indices/uni?key=name&value=Universit%5Cu00e9%20de%20Montr%5Cu00e9al HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: localhost:8182
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: application/json;charset=UTF-8
< server: grizzly/2.2.16
< Access-Control-Allow-Origin: *
< Date: Tue, 15 Apr 2014 13:32:19 GMT
< Content-Length: 139
< 
* Connection #0 to host localhost left intact
* Closing connection #0
{"version":"2.5.0","results":[{"name":"Universit\\u00e9 de Montr\\u00e9al","_id":"1","_type":"vertex"}],"totalSize":1,"queryTime":3.025388}
@espeed

This comment has been minimized.

Copy link
Owner Author

espeed commented Apr 15, 2014

Ok, the key index works for me too...

curl -v -X POST "http://localhost:8182/graphs/emptygraph/vertices?name=Universit%5Cu00e9%20de%20Montr%5Cu00e9al"
* Adding handle: conn: 0x16bda30
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x16bda30) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8182 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8182 (#0)
> POST /graphs/emptygraph/vertices?name=Universit%5Cu00e9%20de%20Montr%5Cu00e9al HTTP/1.1
> User-Agent: curl/7.32.0
> Host: localhost:8182
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: application/json;charset=UTF-8
* Server grizzly/2.2.16 is not blacklisted
< server: grizzly/2.2.16
< Access-Control-Allow-Origin: *
< Vary: Accept
< Date: Tue, 15 Apr 2014 13:31:26 GMT
< Transfer-Encoding: chunked
< 
* Connection #0 to host localhost left intact
{"version":"2.5.0-SNAPSHOT","results":{"name":"Universit\\u00e9 de Montr\\u00e9al","_id":"2","_type":"vertex"},"queryTime":42.939456}You have new mail in /var/spool/mail/james


curl -v "http://localhost:8182/graphs/emptygraph/vertices?key=name&value=Universit%5Cu00e9%20de%20Montr%5Cu00e9al"
* Adding handle: conn: 0x8b4a40
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x8b4a40) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8182 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8182 (#0)
> GET /graphs/emptygraph/vertices?key=name&value=Universit%5Cu00e9%20de%20Montr%5Cu00e9al HTTP/1.1
> User-Agent: curl/7.32.0
> Host: localhost:8182
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: application/json;charset=UTF-8
* Server grizzly/2.2.16 is not blacklisted
< server: grizzly/2.2.16
< Access-Control-Allow-Origin: *
< Date: Tue, 15 Apr 2014 13:31:35 GMT
< Transfer-Encoding: chunked
< 
* Connection #0 to host localhost left intact
{"version":"2.5.0-SNAPSHOT","results":[{"name":"Universit\\u00e9 de Montr\\u00e9al","_id":"2","_type":"vertex"}],"totalSize":1,"queryTime":0.551491}[
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.