From a99f87abe648df6de4213d951ce576b778fca98f Mon Sep 17 00:00:00 2001 From: Mantas Masalskis Date: Tue, 6 Jul 2021 18:40:09 +0000 Subject: [PATCH] Fixes #2085 - timezone issue with elasticsearch --- lib/search_index_backend.rb | 1 + spec/lib/search_index_backend_spec.rb | 16 ++++++++++++++++ spec/support/time_zone.rb | 3 +++ 3 files changed, 20 insertions(+) diff --git a/lib/search_index_backend.rb b/lib/search_index_backend.rb index 584dc494b..159d0de8c 100644 --- a/lib/search_index_backend.rb +++ b/lib/search_index_backend.rb @@ -285,6 +285,7 @@ remove whole data from index condition = { 'query_string' => { 'query' => append_wildcard_to_simple_query(query), + 'time_zone' => Setting.get('timezone_default').presence || 'UTC', 'default_operator' => 'AND', 'analyze_wildcard' => true, } diff --git a/spec/lib/search_index_backend_spec.rb b/spec/lib/search_index_backend_spec.rb index cfce74658..38caf9893 100644 --- a/spec/lib/search_index_backend_spec.rb +++ b/spec/lib/search_index_backend_spec.rb @@ -69,6 +69,22 @@ RSpec.describe SearchIndexBackend, searchindex: true do it { is_expected.to be_an(Array).and not_include(nil).and be_empty } end end + + context 'search with date that requires time zone conversion', time_zone: 'Europe/Vilnius' do + let(:record_type) { 'Ticket'.freeze } + let(:record) { create :ticket } + + before do + travel_to(Time.zone.parse('2019-01-01 23:33')) + described_class.add(record_type, record) + described_class.refresh + end + + it 'finds record in effective time zone' do + result = described_class.search('created_at: [2019-01-01 TO 2019-01-01]', record_type) + expect(result).to eq([{ id: record.id.to_s, type: record_type }]) + end + end end describe '.append_wildcard_to_simple_query' do diff --git a/spec/support/time_zone.rb b/spec/support/time_zone.rb index 1a8f8f09e..79a6ade7c 100644 --- a/spec/support/time_zone.rb +++ b/spec/support/time_zone.rb @@ -3,11 +3,14 @@ RSpec.configure do |config| config.around(:each, :time_zone) do |example| if example.metadata[:type] == :system + # RSpec System/Capybara tests use TZ variable to set timezone in browser old_tz = ENV['TZ'] ENV['TZ'] = example.metadata[:time_zone] example.run else + # Other RSpec tests run inside of the same process and don't take TZ into account. + # Mocking time zone via Time object is enough Time.use_zone(example.metadata[:time_zone]) { example.run } end ensure