Last active
August 29, 2015 13:59
-
-
Save espeed/10725793 to your computer and use it in GitHub Desktop.
Bulbs-Rexster Unicode Lookup via Gremlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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}') | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, the key index works for me too...