Skip to content
This repository has been archived by the owner on Jun 30, 2018. It is now read-only.

Commit

Permalink
Allow arbitrary ordering of methods in the facet DSL block
Browse files Browse the repository at this point in the history
Previously, calling `facet_filter` before other facet methods would fail.

Now, you can do this (notice the `facet_filter` comes first):

    facet "test" do
      facet_filter :range, published_on: { from: '2011-01-01', to: '2011-01-01' }
      histogram :myfield, interval: 70
    end

Closes #534.
  • Loading branch information
msonnabaum authored and karmi committed Dec 5, 2012
1 parent 068e0ef commit e76fcf2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
21 changes: 11 additions & 10 deletions lib/tire/search/facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,53 @@ class Facet
def initialize(name, options={}, &block)
@name = name
@options = options
@value = {}
block.arity < 1 ? self.instance_eval(&block) : block.call(self) if block_given?
end

def terms(field, options={})
size = options.delete(:size) || 10
all_terms = options.delete(:all_terms) || false
@value = if field.is_a?(Enumerable) and not field.is_a?(String)
{ :terms => { :fields => field }.update({ :size => size, :all_terms => all_terms }).update(options) }
@value[:terms] = if field.is_a?(Enumerable) and not field.is_a?(String)
{ :fields => field }.update({ :size => size, :all_terms => all_terms }).update(options)
else
{ :terms => { :field => field }.update({ :size => size, :all_terms => all_terms }).update(options) }
{ :field => field }.update({ :size => size, :all_terms => all_terms }).update(options)
end
self
end

def date(field, options={})
interval = options.delete(:interval) || 'day'
@value = { :date_histogram => { :field => field, :interval => interval }.update(options) }
@value[:date_histogram] = { :field => field, :interval => interval }.update(options)
self
end

def range(field, ranges=[], options={})
@value = { :range => { :field => field, :ranges => ranges }.update(options) }
@value[:range] = { :field => field, :ranges => ranges }.update(options)
self
end

def histogram(field, options={})
@value = { :histogram => (options.delete(:histogram) || {:field => field}.update(options)) }
@value[:histogram] = (options.delete(:histogram) || {:field => field}.update(options))
self
end

def statistical(field, options={})
@value = { :statistical => (options.delete(:statistical) || {:field => field}.update(options)) }
@value[:statistical] = (options.delete(:statistical) || {:field => field}.update(options))
self
end

def terms_stats(key_field, value_field, options={})
@value = { :terms_stats => {:key_field => key_field, :value_field => value_field}.update(options) }
@value[:terms_stats] = {:key_field => key_field, :value_field => value_field}.update(options)
self
end

def query(&block)
@value = { :query => Query.new(&block).to_hash }
@value[:query] = Query.new(&block).to_hash
end

def filter(type, options={})
@value = { :filter => Filter.new(type, options) }
@value[:filter] = Filter.new(type, options)
self
end

Expand Down
17 changes: 14 additions & 3 deletions test/integration/facets_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,19 @@ class FacetsIntegrationTest < Test::Unit::TestCase
assert_equal 1, s.results.facets.size
assert_equal 'ruby', s.results.facets['tags']['terms'].first['term']
assert_equal 1, s.results.facets['tags']['terms'].first['count'].to_i
end
end

should "allow arbitrary order of methods in the DSL block" do
s = Tire.search('articles-test', :search_type => 'count') do
facet 'tags' do
facet_filter :range, { :published_on => { :from => '2011-01-01', :to => '2011-01-01' } }
terms :tags
end
end

assert_equal 1, s.results.facets.size
assert_equal 'ruby', s.results.facets['tags']['terms'].first['term']
assert_equal 1, s.results.facets['tags']['terms'].first['count'].to_i
end

context "terms" do
Expand Down Expand Up @@ -265,8 +276,8 @@ class FacetsIntegrationTest < Test::Unit::TestCase
assert_equal 2, facets["count"], facets.inspect
end

end
end

end
end

end
9 changes: 9 additions & 0 deletions test/unit/search_facet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ def foo; 'bar'; end
f['foo'][:facet_filter].to_json )
end

should "allow arbitrary ordering of methods in the DSL block" do
f = Facet.new('foo') do
facet_filter :terms, :tags => ['ruby']
terms :published_on
end.to_hash

assert_equal( { :terms => {:tags => ['ruby'] }}.to_json, f['foo'][:facet_filter].to_json)
end

end

context "terms facet" do
Expand Down

0 comments on commit e76fcf2

Please sign in to comment.