From 10c8228d3e1eed486e3e1921f321e4e738a8985b Mon Sep 17 00:00:00 2001 From: maxdemarzi Date: Fri, 11 Mar 2011 13:33:19 -0800 Subject: [PATCH] advanced queries of the node and relationship indexes --- README.rdoc | 6 +++-- lib/neography/rest.rb | 21 ++++++++++++--- spec/integration/rest_index_spec.rb | 41 ++++++++++++++++++++++++++--- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/README.rdoc b/README.rdoc index 79bc834..6a42686 100644 --- a/README.rdoc +++ b/README.rdoc @@ -101,14 +101,16 @@ To Use: @neo.remove_node_from_index(index, key, value, node1) # removes a node from the index with the given key/value pair @neo.remove_node_from_index(index, key, node1) # removes a node from the index with the given key @neo.remove_node_from_index(index, node1) # removes a node from the index - @neo.get_node_index(index, key, value) # queries the index with the given key/value pair + @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.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 @neo.remove_relationship_from_index(index, key, value, rel1) # removes a relationship from the index with the given key/value pair @neo.remove_relationship_from_index(index, key, rel1) # removes a relationship from the index with the given key @neo.remove_relationship_from_index(index, rel1) # removes a relationship from the index - @neo.get_relationship_index(index, key, value) # queries the relationship index with the given key/value pair + @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.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 3b31b2c..2028983 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -262,6 +262,12 @@ 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 + return nil if index.empty? + index + end + alias_method :list_indexes, :list_node_indexes alias_method :add_to_index, :add_node_to_index alias_method :remove_from_index, :remove_node_from_index @@ -294,6 +300,13 @@ def get_relationship_index(index, key, value) return nil if index.empty? index end + + def find_relationship_index(index, key, value) + index = get("/index/relationship/#{index}/#{key}?query=#{value}") || Array.new + return nil if index.empty? + index + end + def traverse(id, return_type, description) options = { :body => {"order" => get_order(description["order"]), "uniqueness" => get_uniqueness(description["uniqueness"]), @@ -343,19 +356,19 @@ def evaluate_response(response) end def get(path,options={}) - evaluate_response(HTTParty.get(configuration + path, options.merge!(@authentication))) + evaluate_response(HTTParty.get(configuration + URI.encode(path), options.merge!(@authentication))) end def post(path,options={}) - evaluate_response(HTTParty.post(configuration + path, options.merge!(@authentication))) + evaluate_response(HTTParty.post(configuration + URI.encode(path), options.merge!(@authentication))) end def put(path,options={}) - evaluate_response(HTTParty.put(configuration + path, options.merge!(@authentication))) + evaluate_response(HTTParty.put(configuration + URI.encode(path), options.merge!(@authentication))) end def delete(path,options={}) - evaluate_response(HTTParty.delete(configuration + path, options.merge!(@authentication))) + evaluate_response(HTTParty.delete(configuration + URI.encode(path), options.merge!(@authentication))) end def get_id(id) diff --git a/spec/integration/rest_index_spec.rb b/spec/integration/rest_index_spec.rb index 5e8247e..8c07fc5 100644 --- a/spec/integration/rest_index_spec.rb +++ b/spec/integration/rest_index_spec.rb @@ -126,7 +126,7 @@ new_index.should be_nil end - it "can remove a relationshp from an index" do + it "can remove a relationship from an index" do new_node1 = @neo.create_node new_node2 = @neo.create_node new_relationship = @neo.create_relationship("friends", new_node1, new_node2) @@ -140,7 +140,7 @@ new_index.should be_nil end - it "can remove a relationshp from an index without supplying value" do + it "can remove a relationship from an index without supplying value" do new_node1 = @neo.create_node new_node2 = @neo.create_node new_relationship = @neo.create_relationship("friends", new_node1, new_node2) @@ -154,7 +154,7 @@ new_index.should be_nil end - it "can remove a relationshp from an index without supplying key nor value" do + it "can remove a relationship from an index without supplying key nor value" do new_node1 = @neo.create_node new_node2 = @neo.create_node new_relationship = @neo.create_relationship("friends", new_node1, new_node2) @@ -204,6 +204,16 @@ @neo.remove_node_from_index("test_index", key, value, new_node) end + it "can find a node index" 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 @@ -215,6 +225,31 @@ new_index.first["self"].should == new_relationship["self"] @neo.remove_relationship_from_index("test_index", key, value, new_relationship) end + + it "can get a relationship index with empty spaces" 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 + " " + generate_text + @neo.add_relationship_to_index("test_index", key, value, new_relationship) + new_index = @neo.get_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 a relationship index" 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 + end