Browse Source

Fixes #2085 - timezone issue with elasticsearch

Mantas Masalskis 3 years ago
parent
commit
a99f87abe6
3 changed files with 20 additions and 0 deletions
  1. 1 0
      lib/search_index_backend.rb
  2. 16 0
      spec/lib/search_index_backend_spec.rb
  3. 3 0
      spec/support/time_zone.rb

+ 1 - 0
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,
       }

+ 16 - 0
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

+ 3 - 0
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