Skip to content

Commit

Permalink
Merge pull request sunspot#146 from alindeman/raphaelcm-field_grouping
Browse files Browse the repository at this point in the history
Add support for additional grouping request parameters.
  • Loading branch information
alindeman committed Dec 21, 2011
2 parents 20a7640 + 149db25 commit 65ced78
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,16 @@ Post.search do
order_by(:average_rating, :desc)
end
end

# Facet count is based on the most relevant document of each group
# matching the query (>= Solr 3.4)
Post.search do
group :blog_id_str do
truncate
end

facet :blog_id_str, :extra => :any
end
```

### Geospatial
Expand Down
20 changes: 20 additions & 0 deletions sunspot/lib/sunspot/dsl/field_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ def limit(num)
@group.limit = num
end

#
# If set, facet counts are based on the most relevant document of
# each group matching the query.
#
# Supported in Solr 3.4 and above.
#
# ==== Example
#
# Sunspot.search(Post) do
# group :title do
# truncate
# end
#
# facet :title, :extra => :any
# end
#
def truncate
@group.truncate = true
end

# Specify the order that results should be returned in. This method can
# be called multiple times; precedence will be in the order given.
#
Expand Down
3 changes: 2 additions & 1 deletion sunspot/lib/sunspot/query/field_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Query
# A FieldGroup groups by the unique values of a given field.
#
class FieldGroup
attr_accessor :limit
attr_accessor :limit, :truncate

def initialize(field)
if field.multiple?
Expand All @@ -27,6 +27,7 @@ def to_params

params.merge!(@sort.to_params("group."))
params[:"group.limit"] = @limit if @limit
params[:"group.truncate"] = @truncate if @truncate

params
end
Expand Down
13 changes: 13 additions & 0 deletions sunspot/spec/integration/faceting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ def self.test_field_type(name, attribute, field, *args)
search.facet(:title).rows.first.value.should == :none
search.facet(:title).rows.first.count.should == 1
end

it 'gives correct facet count when group == true and truncate == true' do
search = Sunspot.search(Post) do
group :title do
truncate
end

facet :title, :extra => :any
end

# Should be 5 instead of 11
search.facet(:title).rows.first.count.should == 5
end
end

context 'multiselect faceting' do
Expand Down
1 change: 1 addition & 0 deletions sunspot/spec/integration/field_grouping_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require File.expand_path("../spec_helper", File.dirname(__FILE__))
require File.expand_path("../helpers/search_helper", File.dirname(__FILE__))

describe "field grouping" do
before :each do
Expand Down

0 comments on commit 65ced78

Please sign in to comment.