Skip to content

Commit

Permalink
fixed dependency of slingshot to 0.0.8 and added ability to index met…
Browse files Browse the repository at this point in the history
…hods in relations
  • Loading branch information
Omar Mekky committed May 24, 2011
1 parent 4125356 commit 3c05e7b
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Expand Up @@ -4,7 +4,7 @@ PATH
mebla (1.1.4)
mebla
mongoid (~> 2.0.1)
slingshot-rb (~> 0.0.7)
slingshot-rb (= 0.0.8)

GEM
remote: http://rubygems.org/
Expand Down Expand Up @@ -50,7 +50,7 @@ GEM
rspec-expectations (2.3.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.3.0)
slingshot-rb (0.0.7)
slingshot-rb (0.0.8)
bundler (~> 1.0.0)
rest-client (~> 1.6.0)
yajl-ruby (> 0.7.9)
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -24,7 +24,7 @@ Jeweler::Tasks.new do |gem|
gem.authors = ["Omar Mekky"]
# Include your dependencies below. Runtime dependencies are required when using your gem,
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
gem.add_runtime_dependency 'slingshot-rb', '~> 0.0.7'
gem.add_runtime_dependency 'slingshot-rb', '0.0.8'
gem.add_runtime_dependency 'mongoid', '~> 2.0.1'

gem.add_development_dependency 'bson', '~> 1.3.0'
Expand Down
48 changes: 37 additions & 11 deletions lib/mebla/context.rb
Expand Up @@ -140,34 +140,60 @@ def index_data(*models)
# only index search fields and methods
document.class.search_fields.each do |field|
if document.attributes.keys.include?(field.to_s)
attrs[field] = document.attributes[field.to_s]
attrs[field] = document.attributes[field.to_s] # attribute
else
attrs[field] = document.send(field)
attrs[field] = document.send(field) # method
end
end

# index relational fields
document.class.search_relations.each do |relation, fields|
items = document.send(relation.to_sym)
items = document.send(relation.to_sym) # get the relation document

next if items.nil?

# N relation side
if items.is_a?(Array)
next if items.empty?
attrs[relation] = []
items.each do |item|
if fields.is_a?(Array)
attrs[relation] << item.attributes.reject{|key, value| !fields.include?(key.to_sym)}
else
attrs[relation] << { fields => item.attributes[fields.to_s] }
if fields.is_a?(Array) # given multiple fields to index
fields_values = {}
fields.each do |field|
if item.attributes.keys.include?(field.to_s)
fields_values.merge!({ field => item.attributes[fields.to_s] }) # attribute
else
fields_values.merge!({ field => item.send(field) }) # method
end
end
attrs[relation] << fields_values
else # only index one field in the relation
if item.attributes.keys.include?(fields.to_s)
attrs[relation] << { fields => item.attributes[fields.to_s] } # attribute
else
attrs[relation] << { fields => item.send(fields) } # method
end
end
end
# 1 relation side
else
attrs[relation] = {}
if fields.is_a?(Array)
attrs[relation].merge!(items.attributes.reject{|key, value| !fields.include?(key.to_sym)})
else
attrs[relation].merge!({ fields => items.attributes[fields.to_s] })
if fields.is_a?(Array) # given multiple fields to index
fields_values = {}
fields.each do |field|
if items.attributes.keys.include?(field.to_s)
fields_values.merge!({ field => items.attributes[fields.to_s] }) # attribute
else
fields_values.merge!({ field => items.send(field) }) # method
end
end
attrs[relation].merge!(fields_values)
else # only index one field in the relation
if items.attributes.keys.include?(fields.to_s)
attrs[relation].merge!({ fields => items.attributes[fields.to_s] }) # attribute
else
attrs[relation].merge!({ fields => items.send(fields) }) # method
end
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/mebla/result_set.rb
Expand Up @@ -45,14 +45,14 @@ def initialize(response)
end
# collect ids
# {class => [ids]}
model_ids[model_class].push hit['_source']['id']
model_ids[model_class] << hit['_source']['id']
end
end

# Cast the results into their appropriate classes
@entries = []
model_ids.each do |model_class, ids|

model_ids.each_pair do |model_class, ids|
unless model_class.embedded?
# Retrieve the results from the database
@entries += model_class.any_in(:_id => ids).entries
Expand All @@ -61,7 +61,7 @@ def initialize(response)
parent_class = model_class.embedded_parent
access_method = model_class.embedded_as

ids.each do |parent_id, entries_ids|
ids.each_pair do |parent_id, entries_ids|
parent = parent_class.find parent_id

# Retrieve the results from the database
Expand Down
38 changes: 34 additions & 4 deletions mebla.gemspec
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Omar Mekky"]
s.date = %q{2011-04-08}
s.date = %q{2011-05-24}
s.description = %q{
An elasticsearch wrapper for mongoid odm based on slingshot. Makes integration between ElasticSearch full-text
search engine and Mongoid documents seemless and simple.
Expand Down Expand Up @@ -107,7 +107,17 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<mongoid-rspec>, ["= 1.4.1"])
s.add_development_dependency(%q<database_cleaner>, ["= 0.6.4"])
s.add_development_dependency(%q<bluecloth>, ["~> 2.1.0"])
s.add_runtime_dependency(%q<slingshot-rb>, ["~> 0.0.7"])
s.add_development_dependency(%q<bson>, ["~> 1.3.0"])
s.add_development_dependency(%q<bson_ext>, ["~> 1.3.0"])
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
s.add_development_dependency(%q<rcov>, [">= 0"])
s.add_development_dependency(%q<mongoid-rspec>, ["= 1.4.1"])
s.add_development_dependency(%q<database_cleaner>, ["= 0.6.4"])
s.add_development_dependency(%q<bluecloth>, ["~> 2.1.0"])
s.add_runtime_dependency(%q<slingshot-rb>, ["= 0.0.8"])
s.add_runtime_dependency(%q<mongoid>, ["~> 2.0.1"])
s.add_development_dependency(%q<bson>, ["~> 1.3.0"])
s.add_development_dependency(%q<bson_ext>, ["~> 1.3.0"])
Expand Down Expand Up @@ -147,7 +157,17 @@ Gem::Specification.new do |s|
s.add_dependency(%q<mongoid-rspec>, ["= 1.4.1"])
s.add_dependency(%q<database_cleaner>, ["= 0.6.4"])
s.add_dependency(%q<bluecloth>, ["~> 2.1.0"])
s.add_dependency(%q<slingshot-rb>, ["~> 0.0.7"])
s.add_dependency(%q<bson>, ["~> 1.3.0"])
s.add_dependency(%q<bson_ext>, ["~> 1.3.0"])
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
s.add_dependency(%q<yard>, ["~> 0.6.0"])
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
s.add_dependency(%q<rcov>, [">= 0"])
s.add_dependency(%q<mongoid-rspec>, ["= 1.4.1"])
s.add_dependency(%q<database_cleaner>, ["= 0.6.4"])
s.add_dependency(%q<bluecloth>, ["~> 2.1.0"])
s.add_dependency(%q<slingshot-rb>, ["= 0.0.8"])
s.add_dependency(%q<mongoid>, ["~> 2.0.1"])
s.add_dependency(%q<bson>, ["~> 1.3.0"])
s.add_dependency(%q<bson_ext>, ["~> 1.3.0"])
Expand Down Expand Up @@ -188,7 +208,17 @@ Gem::Specification.new do |s|
s.add_dependency(%q<mongoid-rspec>, ["= 1.4.1"])
s.add_dependency(%q<database_cleaner>, ["= 0.6.4"])
s.add_dependency(%q<bluecloth>, ["~> 2.1.0"])
s.add_dependency(%q<slingshot-rb>, ["~> 0.0.7"])
s.add_dependency(%q<bson>, ["~> 1.3.0"])
s.add_dependency(%q<bson_ext>, ["~> 1.3.0"])
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
s.add_dependency(%q<yard>, ["~> 0.6.0"])
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
s.add_dependency(%q<rcov>, [">= 0"])
s.add_dependency(%q<mongoid-rspec>, ["= 1.4.1"])
s.add_dependency(%q<database_cleaner>, ["= 0.6.4"])
s.add_dependency(%q<bluecloth>, ["~> 2.1.0"])
s.add_dependency(%q<slingshot-rb>, ["= 0.0.8"])
s.add_dependency(%q<mongoid>, ["~> 2.0.1"])
s.add_dependency(%q<bson>, ["~> 1.3.0"])
s.add_dependency(%q<bson_ext>, ["~> 1.3.0"])
Expand Down
20 changes: 10 additions & 10 deletions spec/mebla/searching_spec.rb
Expand Up @@ -10,7 +10,7 @@
it "should search and return the only relevant result" do
results=MongoidAlpha.search "name: Testing index"

results.count.should == 1
results.total.should == 1
end

it "should search and return the only relevant result, and cast it into the correct class type" do
Expand All @@ -34,7 +34,7 @@
it "should search within arrays" do
results = MongoidZeta.search "item2"

results.count.should == 1
results.total.should == 1
end
end

Expand All @@ -47,7 +47,7 @@
it "should search within indexed methods" do
results = MongoidPi.search "returns smth"

results.count.should == 1
results.total.should == 1
end
end

Expand All @@ -62,7 +62,7 @@
it "should search within indexed fields from the relations" do
results = MongoidEpsilon.search "Testing index"

results.count.should == 1
results.total.should == 1
end
end

Expand All @@ -74,14 +74,14 @@
it "should search and return all results of all class types" do
results=Mebla.search "name: Testing index"

results.count.should == 2
results.total.should == 2
(results.each.collect{|e| e.class} & [MongoidAlpha, MongoidBeta]).should =~ [MongoidAlpha, MongoidBeta]
end

it "should search and return only results from the searched class type" do
results=MongoidAlpha.search "name: Testing index"

results.count.should == 1
results.total.should == 1
results.first.class.should == MongoidAlpha
end
end
Expand All @@ -95,7 +95,7 @@
it "should search and return the only relevant result" do
results=MongoidGamma.search "name: Embedded"

results.count.should == 1
results.total.should == 1
end

it "should search and return the only relevant result, and cast it into the correct class type" do
Expand All @@ -120,15 +120,15 @@
end

it "should search and only return results matching the term defined" do
Mebla.search.term(:name, "index").count.should == 2
Mebla.search.term(:name, "index").total.should == 2
end

it "should search and only return results matching the terms defined" do
Mebla.search.terms(:name, ["index", "map"]).count.should == 3
Mebla.search.terms(:name, ["index", "map"]).total.should == 3
end

it "should search and filter results according to the filters defined" do
Mebla.search.terms(:name, ["index", "map"]).only(:value => [1]).count.should == 1
Mebla.search.terms(:name, ["index", "map"]).only(:value => [1]).total.should == 1
end

it "should search and return results along with facets" do
Expand Down

0 comments on commit 3c05e7b

Please sign in to comment.