Fixes #3599 - Searching for integer fields does not work as expected.

This commit is contained in:
Rolf Schmidt 2021-07-12 08:18:46 +00:00 committed by Thorsten Eckel
parent 2770f0a370
commit 382291d0ae
2 changed files with 56 additions and 0 deletions

View file

@ -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

View file

@ -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