New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pivots [WIP] #968
Open
jasper-lyons
wants to merge
1
commit into
sunspot:master
Choose a base branch
from
jasper-lyons:master
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Pivots [WIP] #968
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
module Sunspot | ||
module Util | ||
# Based on: | ||
# https://github.com/apache/lucene-solr/blob/master/solr/core/src/java/org/apache/solr/util/DateMathParser.java | ||
class DateMathParser | ||
def initialize(date) | ||
@date = case date | ||
when DateTime | ||
date | ||
when Time | ||
date.to_datetime | ||
when Date | ||
date.to_datetime | ||
else | ||
raise "DateMathParser expects a DateTime got: #{date.class}" | ||
end | ||
end | ||
|
||
def evaluate(gap) | ||
scanner = StringScanner.new(gap) | ||
value_stack = [@date] | ||
op_stack = [] | ||
|
||
while !scanner.eos? | ||
if scanner.scan(/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9.]+Z/) | ||
value_stack.push(DateTime.parse(scanner.matched)) | ||
elsif scanner.scan(/[0-9]+/) | ||
value_stack.push(scanner.matched.to_i) | ||
elsif scanner.scan(/[A-Z]+/) | ||
value_stack.push(scanner.matched) | ||
elsif scanner.scan(/\//) | ||
op_stack.push('round') | ||
elsif scanner.scan(/\+/) | ||
op_stack.push('add') | ||
elsif scanner.scan(/-/) | ||
op_stack.push('sub') | ||
else | ||
throw "Error parsing Date Time Math string in range.gap: #{gap}" | ||
end | ||
end | ||
|
||
while !op_stack.empty? | ||
op = op_stack.pop | ||
|
||
case op | ||
when 'round' | ||
unit, value = value_stack.pop, value_stack.pop | ||
value_stack.push(normalize_date(value, unit)) | ||
when 'add' | ||
unit, value, date = value_stack.pop, value_stack.pop, value_stack.pop | ||
value_stack.push(add_date_time(date, value, unit)) | ||
when 'sub' | ||
unit, value, date = value_stack.pop, value_stack.pop, value_stack.pop | ||
value_stack.push(sub_date_time(date, value, unit)) | ||
else | ||
raise "Unrecongnized operator '#{op}' in Date Time Math string in range.gap: #{gap}" | ||
end | ||
end | ||
|
||
value_stack.pop | ||
end | ||
|
||
def normalize_date(date, unit) | ||
case unit | ||
when "YEAR", "YEARS" | ||
DateTime.new(date.year, 1, 1, 0, 0, 0) | ||
when "MONTH", "MONTHS" | ||
DateTime.new(date.year, date.month, 1, 0, 0, 0) | ||
when "DAY", "DAYS" | ||
DateTime.new(date.year, date.month, date.day, 0, 0, 0) | ||
when "DATE" | ||
# not certain how to handle 'DATE' so just pass through date | ||
date | ||
when "HOUR", "HOURS" | ||
DateTime.new(date.year, date.month, date.mday, date.hour, 0, 0) | ||
when "MINUTE", "MINUTES" | ||
DateTime.new(date.year, date.month, date.mday, date.hour, date.minute, 0) | ||
when "SECOND", "SECONDS" | ||
# Not sure how to truncate to nearest second with only second level | ||
# accuracy in ruby | ||
date | ||
when "MILLI", "MILLIS", "MILLISECOND", "MILLISECONDS" | ||
# Not sure how to handle milliseconds so just return date | ||
date | ||
else | ||
raise "Unrecognized Date Time Math unit: #{unit}" | ||
end | ||
end | ||
|
||
def add_date_time(date, value, unit) | ||
case unit | ||
when "YEAR", "YEARS" | ||
date.next_year(value) | ||
when "MONTH", "MONTHS" | ||
date.next_month(value) | ||
when "DAY", "DAYS" | ||
date.next_day(value) | ||
when "DATE" | ||
# Not certain how to handle 'DATE' so just pass through empty date | ||
date | ||
when "HOUR", "HOURS" | ||
date + Rational(value, 24) | ||
when "MINUTE", "MINUTES" | ||
date + Rational(value, 60 * 24) | ||
when "SECOND", "SECONDS" | ||
date + Rational(value, 60 * 60 * 24) | ||
when "MILLI", "MILLIS", "MILLISECOND", "MILLISECONDS" | ||
date + Rational(value, 1000 * 60 * 60 * 24) | ||
else | ||
raise "Unrecognized Date Time Math unit: #{unit}" | ||
end | ||
end | ||
|
||
def sub_date_time(date, value, unit) | ||
case unit | ||
when "YEAR", "YEARS" | ||
date.prev_year(value) | ||
when "MONTH", "MONTHS" | ||
date.prev_month(value) | ||
when "DAY", "DAYS" | ||
date.prev_day(value) | ||
when "DATE" | ||
# Not certain how to handle 'DATE' so just pass through empty date | ||
date | ||
when "HOUR", "HOURS" | ||
date - Rational(value, 24) | ||
when "MINUTE", "MINUTES" | ||
date - Rational(value, 60 * 24) | ||
when "SECOND", "SECONDS" | ||
date - Rational(value, 60 * 60 * 24) | ||
when "MILLI", "MILLIS", "MILLISECOND", "MILLISECONDS" | ||
date - Rational(value, 1000 * 60 * 60 * 24) | ||
else | ||
raise "Unrecognized Date Time Math unit: #{unit}" | ||
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
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,53 @@ | ||
module Sunspot | ||
module Query | ||
# Add a facet class for pivoting ranges | ||
class PivotFacet < AbstractFieldFacet | ||
def initialize(fields, options) | ||
@fields = fields | ||
# This facet operates on mutiple fields | ||
super(nil, options) | ||
end | ||
|
||
# ammended not to rely on @field | ||
def qualified_param(param) | ||
:"facet.pivot.#{param}" | ||
end | ||
|
||
def to_params | ||
super.tap do |params| | ||
# use array so that multiple facet.pivot appear in the search | ||
# string rather than the last facet.pivot key added to the params | ||
# see: | ||
# * https://github.com/sunspot/sunspot/blob/3328212da79178319e98699d408f14513855d3c0/sunspot/lib/sunspot/query/common_query.rb#L81 | ||
# * https://github.com/sunspot/sunspot/blob/3328212da79178319e98699d408f14513855d3c0/sunspot/lib/sunspot/util.rb#L236 | ||
# | ||
params[:"facet.pivot"] = [field_names_with_local_params] | ||
end | ||
end | ||
|
||
private | ||
|
||
def local_params | ||
@local_params ||= | ||
{}.tap do |local_params| | ||
local_params[:range] = @options[:range] if @options[:range] | ||
local_params[:stats] = @options[:stats] if @options[:stats] | ||
local_params[:query] = @options[:query] if @options[:query] | ||
end | ||
end | ||
|
||
def field_names_with_local_params | ||
if local_params.empty? | ||
field_names.join(',') | ||
else | ||
pairs = local_params.map { |key, value| "#{key}=#{value}" } | ||
"{!#{pairs.join(' ')}}#{field_names.join(',')}" | ||
end | ||
end | ||
|
||
def field_names | ||
@fields.map(&:indexed_name) | ||
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,67 @@ | ||
module Sunspot | ||
module Search | ||
class PivotFacet | ||
class Row | ||
def initialize(fields, pivot, setup) | ||
# ordering is important here! | ||
@field, *@deeper_fields = *fields | ||
@pivot = pivot | ||
@setup = setup | ||
end | ||
|
||
attr_reader :field, :deeper_fields | ||
|
||
def result | ||
@pivot | ||
end | ||
|
||
def pivot | ||
result['pivot'].map { |p| Row.new(deeper_fields, p, @setup) } | ||
end | ||
|
||
def range(field_name) | ||
indexed_name = @setup.field(field_name).indexed_name | ||
return unless result['ranges'][indexed_name] | ||
PivotRange.new(result['ranges'][indexed_name]) | ||
end | ||
end | ||
|
||
class PivotRange | ||
def initialize(range) | ||
@range = range | ||
end | ||
|
||
def counts | ||
#probably should be just like the RangeFacet returning FacetRows | ||
@range['counts'].each_slice(2).to_h | ||
end | ||
|
||
def gap | ||
@range['gap'] | ||
end | ||
|
||
def start | ||
@range['start'] | ||
end | ||
|
||
def end | ||
@range['end'] | ||
end | ||
end | ||
|
||
def initialize(fields, setup, search, options) | ||
@fields, @setup, @search, @options = fields, setup, search, options | ||
end | ||
|
||
def rows | ||
@rows ||= @search. | ||
facet_response['facet_pivot'][range_name]. | ||
map { |p| Row.new(@fields, p, @setup) } | ||
end | ||
|
||
def range_name | ||
@fields.map(&:indexed_name).join(',') | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe fields should be in last because you are expecting more than 1 values.
def initialize(pivot, setup, fields).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @anikett-rpx, thanks for the comment!
Could you give me an example of what you mean?
fields
should be an explicit array of field names, entered like.new([:field1, :field2, ...], ...)
.