Skip to content

Commit

Permalink
Improve search queries based on datetime
Browse files Browse the repository at this point in the history
This is heavily work in progresss and the content will most likely
change.
  • Loading branch information
adamlazik1 committed Jan 15, 2025
1 parent f49fe8a commit ee12200
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/scoped_search/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,30 @@ def datetime_test(field, operator, value, &block) # :yields: finder_option_type,
# but the field is of datetime type. Change the comparison to return
# more logical results.
if field.datetime?
span = 1.minute if(value =~ /\A\s*\d+\s+\bminutes?\b\s+\bago\b\s*\z/i)
span ||= (timestamp.day_fraction == 0) ? 1.day : 1.hour
if value =~ /(\d{1,2}:){2}\d{1,2}/
span = 1.second
elsif value =~ /(\A\s*\d+\s+\bminutes?\b\s+\bago\b\s*\z|\d{1,2}:\d{1,2})/i
span = 1.minute
elsif timestamp.day_fraction != 0
span = 1.hour
else
span = 1.day
end

if [:eq, :ne].include?(operator)
# Instead of looking for an exact (non-)match, look for dates that
# fall inside/outside the range of timestamps of that day.
negate = (operator == :ne) ? 'NOT ' : ''
field_sql = field.to_sql(operator, &block)
return ["#{negate}(#{field_sql} >= ? AND #{field_sql} < ?)", timestamp, timestamp + span]

elsif operator == :gt
elsif span == 1.day && operator == :gt
# Make sure timestamps on the given date are not included in the results
# by moving the date to the next day.
timestamp += span
operator = :gte

elsif operator == :lte
elsif span == 1.day && operator == :lte
# Make sure the timestamps of the given date are included by moving the
# date to the next date.
timestamp += span
Expand Down

0 comments on commit ee12200

Please sign in to comment.