Skip to content

Commit

Permalink
advanced queries of the node and relationship indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Mar 11, 2011
1 parent 3e41e09 commit 10c8228
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
6 changes: 4 additions & 2 deletions README.rdoc
Expand Up @@ -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
Expand Down
21 changes: 17 additions & 4 deletions lib/neography/rest.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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"]),
Expand Down Expand Up @@ -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)
Expand Down
41 changes: 38 additions & 3 deletions spec/integration/rest_index_spec.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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


Expand Down

0 comments on commit 10c8228

Please sign in to comment.