From bc49b90c295cc3d4a79a8776877ceff1b0262b99 Mon Sep 17 00:00:00 2001 From: Carlo Alberto Degli Atti Date: Mon, 24 Oct 2011 16:46:05 +0200 Subject: [PATCH] finding nodes and relationships through query. Remote Groovy script through Gremlin plugin ext --- README.rdoc | 3 +++ lib/neography/rest.rb | 19 +++++++++++++++---- spec/integration/rest_index_spec.rb | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/README.rdoc b/README.rdoc index 94417f3..9fb6345 100644 --- a/README.rdoc +++ b/README.rdoc @@ -98,6 +98,7 @@ To Use: @neo.remove_node_from_index(index, node1) # removes a node from the index @neo.get_node_index(index, key, value) # exact query of the node index with the given key/value pair @neo.find_node_index(index, key, value) # advanced query of the node index with the given key/value pair + @neo.find_node_index(index, query ) # advanced query of the node index with the given query @neo.list_relationship_indexes # gives names and query templates for relationship indices @neo.create_relationship_index(name, "fulltext", provider) # creates a relationship index with "fulltext" option @neo.add_relationship_to_index(index, key, value, rel1) # adds a relationship to the index with the given key/value pair @@ -106,6 +107,8 @@ To Use: @neo.remove_relationship_from_index(index, rel1) # removes a relationship from the index @neo.get_relationship_index(index, key, value) # exact query of the relationship index with the given key/value pair @neo.find_relationship_index(index, key, value) # advanced query of the relationship index with the given key/value pair + @neo.find_relationship_index(index, query) # advanced query of the relationship index with the given query + @neo.execute_script(script) # sends an arbitrary Groovy script (through the Gremlin plugin) @neo.get_path(node1, node2, relationships, depth=4, algorithm="shortestPath") # finds the shortest path between two nodes diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index 52abac5..eb10f1e 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -263,8 +263,11 @@ def get_node_index(index, key, value) index end - def find_node_index(index, key, value) - index = get("/index/node/#{index}/#{key}?query=#{value}") || Array.new + def find_node_index(*args) + case args.size + when 3 then index = get("/index/node/#{args[0]}/#{args[1]}?query=#{args[2]}") || Array.new + when 2 then index = get("/index/node/#{args[0]}?query=#{args[1]}") || Array.new + end return nil if index.empty? index end @@ -302,8 +305,11 @@ def get_relationship_index(index, key, value) index end - def find_relationship_index(index, key, value) - index = get("/index/relationship/#{index}/#{key}?query=#{value}") || Array.new + def find_relationship_index(*args) + case args.size + when 3 then index = get("/index/relationship/#{args[0]}/#{args[1]}?query=#{args[2]}") || Array.new + when 2 then index = get("/index/relationship/#{args[0]}?query=#{args[1]}") || Array.new + end return nil if index.empty? index end @@ -328,6 +334,11 @@ def get_paths(from, to, relationships, depth=1, algorithm="allPaths") paths = post("/node/#{get_id(from)}/paths", options) || Array.new end + def execute_script(script) + options = { :body => { :script => script } } + post("/ext/GremlinPlugin/graphdb/execute_script", options) + end + private def evaluate_response(response) diff --git a/spec/integration/rest_index_spec.rb b/spec/integration/rest_index_spec.rb index bc773ca..0a51e9e 100644 --- a/spec/integration/rest_index_spec.rb +++ b/spec/integration/rest_index_spec.rb @@ -214,6 +214,16 @@ @neo.remove_node_from_index("test_index", key, value, new_node) end + it "can find a node index using generic query" do + new_node = @neo.create_node + key = generate_text(6) + value = generate_text + @neo.add_node_to_index("test_index", key, value, new_node) + new_index = @neo.find_node_index("test_index", "#{key}: #{value}") + new_index.first["self"].should == new_node["self"] + @neo.remove_node_from_index("test_index", key, value, new_node) + end + it "can get a relationship index" do new_node1 = @neo.create_node new_node2 = @neo.create_node @@ -250,6 +260,18 @@ @neo.remove_relationship_from_index("test_index", key, value, new_relationship) end + it "can find a relationship index using generic query" do + new_node1 = @neo.create_node + new_node2 = @neo.create_node + new_relationship = @neo.create_relationship("friends", new_node1, new_node2) + key = generate_text(6) + value = generate_text + @neo.add_relationship_to_index("test_index", key, value, new_relationship) + new_index = @neo.find_relationship_index("test_index", "#{key}: #{value}") + new_index.first["self"].should == new_relationship["self"] + @neo.remove_relationship_from_index("test_index", key, value, new_relationship) + end + it "can find use the results of a node index" do new_node1 = @neo.create_node new_node2 = @neo.create_node