From 382291d0ae4c29d07f05b7c3d86d1ced1d419823 Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Mon, 12 Jul 2021 08:18:46 +0000 Subject: [PATCH] Fixes #3599 - Searching for integer fields does not work as expected. --- .../can_lookup_search_index_attributes.rb | 25 +++++++++++++++ spec/lib/search_index_backend_spec.rb | 31 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/app/models/application_model/can_lookup_search_index_attributes.rb b/app/models/application_model/can_lookup_search_index_attributes.rb index 8ff5d0873..11f731d62 100644 --- a/app/models/application_model/can_lookup_search_index_attributes.rb +++ b/app/models/application_model/can_lookup_search_index_attributes.rb @@ -3,6 +3,23 @@ module ApplicationModel::CanLookupSearchIndexAttributes extend ActiveSupport::Concern + class RequestCache < ActiveSupport::CurrentAttributes + attribute :integer_attribute_names + + def integer_fields(class_name) + self.integer_attribute_names ||= {} + + updated_at = ObjectManager::Attribute.maximum('updated_at') + return self.integer_attribute_names[class_name][:data] if self.integer_attribute_names[class_name].present? && self.integer_attribute_names[class_name][:updated_at] == updated_at + + self.integer_attribute_names[class_name] = { + updated_at: updated_at, + data: ObjectManager::Attribute.where(object_lookup: ObjectLookup.find_by(name: class_name), data_type: 'integer', editable: true).pluck(:name), + } + self.integer_attribute_names[class_name][:data] + end + end + =begin This function return the attributes for the elastic search with relation hash values. @@ -53,6 +70,14 @@ returns attributes[ attribute_ref_name ] = value end + if self.class.include?(HasObjectManagerAttributesValidation) + RequestCache.integer_fields(self.class.to_s).each do |field| + next if attributes[field].blank? + + attributes["#{field}_text"] = attributes[field].to_s + end + end + attributes end diff --git a/spec/lib/search_index_backend_spec.rb b/spec/lib/search_index_backend_spec.rb index 38caf9893..01c7fe5f9 100644 --- a/spec/lib/search_index_backend_spec.rb +++ b/spec/lib/search_index_backend_spec.rb @@ -85,6 +85,37 @@ RSpec.describe SearchIndexBackend, searchindex: true do expect(result).to eq([{ id: record.id.to_s, type: record_type }]) end end + + context 'does find integer values for ticket data', db_strategy: :reset do + let(:record_type) { 'Ticket'.freeze } + let(:record) { create :ticket, inttest: '1021052349' } + + before do + create(:object_manager_attribute_integer, name: 'inttest', data_option: { + 'default' => 0, + 'min' => 0, + 'max' => 99_999_999, + }) + ObjectManager::Attribute.migration_execute + record.search_index_update_backend + described_class.refresh + end + + it 'finds added records by integer part' do + result = described_class.search('102105', record_type, sort_by: ['updated_at'], order_by: ['desc']) + expect(result).to eq([{ id: record.id.to_s, type: record_type }]) + end + + it 'finds added records by integer' do + result = described_class.search('1021052349', record_type, sort_by: ['updated_at'], order_by: ['desc']) + expect(result).to eq([{ id: record.id.to_s, type: record_type }]) + end + + it 'finds added records by quoted integer' do + result = described_class.search('"1021052349"', record_type, sort_by: ['updated_at'], order_by: ['desc']) + expect(result).to eq([{ id: record.id.to_s, type: record_type }]) + end + end end describe '.append_wildcard_to_simple_query' do