forked from sunspot/sunspot
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sunspot#177 from wildoats/master
Adding support for range facets
- Loading branch information
Showing
8 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Sunspot | ||
module Query | ||
class RangeFacet < AbstractFieldFacet | ||
def to_params | ||
params = super | ||
params[:"facet.range"] = [@field.indexed_name] | ||
params[qualified_param('range.start')] = @field.to_indexed(@options[:range].first) | ||
params[qualified_param('range.end')] = @field.to_indexed(@options[:range].last) | ||
params[qualified_param('range.gap')] = "#{@options[:range_interval] || 10}" | ||
params | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Sunspot | ||
module Search | ||
class RangeFacet | ||
def initialize(field, search, options) | ||
@field, @search, @options = field, search, options | ||
end | ||
|
||
def field_name | ||
@field.name | ||
end | ||
|
||
def rows | ||
@rows ||= | ||
begin | ||
data = @search.facet_response['facet_ranges'][@field.indexed_name] | ||
gap = (@options[:range_interval] || 10).to_i | ||
rows = [] | ||
|
||
if data['counts'] | ||
Hash[*data['counts']].each_pair do |start_str, count| | ||
start = start_str.to_f | ||
finish = start + gap | ||
rows << FacetRow.new(start..finish, count, self) | ||
end | ||
end | ||
|
||
if @options[:sort] == :count | ||
rows.sort! { |lrow, rrow| rrow.count <=> lrow.count } | ||
else | ||
rows.sort! { |lrow, rrow| lrow.value.first <=> rrow.value.first } | ||
end | ||
rows | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters