From 1617844903ccd3fac57d4b4eab153c20c94214c7 Mon Sep 17 00:00:00 2001 From: Julien Portalier Date: Tue, 14 Nov 2023 11:31:40 +0100 Subject: [PATCH] Fix: search by fullpath instead of multi field name (removed in elasticsearch 2) --- app/models/elastic_search/query_helper.rb | 4 +-- .../field/identifier_fields/luhn_field.rb | 5 ++- app/models/search.rb | 19 ++++++---- app/models/search_base.rb | 35 +++++++++++-------- 4 files changed, 36 insertions(+), 27 deletions(-) diff --git a/app/models/elastic_search/query_helper.rb b/app/models/elastic_search/query_helper.rb index 89a93a3f5..d87bfcf66 100644 --- a/app/models/elastic_search/query_helper.rb +++ b/app/models/elastic_search/query_helper.rb @@ -44,7 +44,7 @@ def full_text_search(text, search_base, collection, fields = nil) field = collection.fields.find { |x| x.code == key || x.name == key} next unless field - key = field.es_code + key = "properties.#{field.es_code}" op, value = SearchParser.get_op_and_val value # Check if the user is searching a label instead of the code @@ -83,7 +83,7 @@ def search_value_ids(text, collection, fields_to_search = nil) regex = /#{Regexp.escape text}/i fields_to_search.each do |field| option_id = search_value_id field, regex - codes[field.es_code] = option_id if option_id + codes["properties.#{field.es_code}"] = option_id if option_id end codes end diff --git a/app/models/field/identifier_fields/luhn_field.rb b/app/models/field/identifier_fields/luhn_field.rb index 43ecb055e..52c1f7992 100644 --- a/app/models/field/identifier_fields/luhn_field.rb +++ b/app/models/field/identifier_fields/luhn_field.rb @@ -61,10 +61,9 @@ def compute_luhn_verifier(str) def largest_existing_luhn_value_in_this_field(collection) # Find largest existing value in ES - field_es_code = "properties.#{@field.es_code}" search = collection.new_search - search.field_exists(field_es_code) - search.sort field_es_code, false + search.field_exists(@field.es_code) + search.sort(@field.es_code, false) search.offset(0) search.limit(1) search.show_deleted diff --git a/app/models/search.rb b/app/models/search.rb index b7bf21230..f74f8925b 100644 --- a/app/models/search.rb +++ b/app/models/search.rb @@ -79,13 +79,7 @@ def sort(es_code, ascendent = true) when 'name' sort = 'name.downcase' else - es_code = remove_at_from_code es_code - field = fields.find { |x| x.code == es_code || x.es_code == es_code } - if field && field.kind == 'text' - sort = "properties.#{field.es_code}.downcase" - else - sort = decode(es_code) - end + sort = sort_key(es_code) end ascendent = ascendent ? 'asc' : 'desc' @@ -95,6 +89,17 @@ def sort(es_code, ascendent = true) self end + protected def sort_key(es_code) + es_code = remove_at_from_code es_code + field = fields.find { |x| x.code == es_code || x.es_code == es_code } + + if field && field.kind == 'text' + query_key(field, downcase: true) + else + "properties.#{decode(es_code)}" + end + end + def sort_multiple(sort_list) sort_list.each_pair do |es_code, ascendent| sort(es_code, ascendent) diff --git a/app/models/search_base.rb b/app/models/search_base.rb index 3dd175195..82384164d 100644 --- a/app/models/search_base.rb +++ b/app/models/search_base.rb @@ -31,7 +31,7 @@ def uuid(uuid) def eq(field, value) if value.blank? - add_filter missing: {field: field.es_code} + add_filter missing: {field: query_key(field)} return self end @@ -48,27 +48,34 @@ def not_eq(field, value) end def query_params(field, value) - query_key = field.es_code validated_value = field.parse_for_query(value, @use_codes_instead_of_es_codes) if field.kind == 'date' - date_field_range(query_key, validated_value) + date_field_range(query_key(field), validated_value) elsif field.kind == 'yes_no' && !validated_value.is_a?(Array) && !Field.yes?(value) - { not: { :term => { query_key => true }}} # so we return false & nil values + { not: { :term => { query_key(field) => true }}} # so we return false & nil values elsif validated_value.is_a? Array - { terms: {query_key => validated_value} } + { terms: {query_key(field) => validated_value} } else - { term: {query_key => validated_value} } + { term: {query_key(field) => validated_value} } end # elsif field.select_kind? - # {term: {query_key => validated_value}} - # add_filter term: {query_key => validated_value} + # {term: {query_key(field) => validated_value}} + # add_filter term: {query_key(field) => validated_value} # else # end end + def query_key(field, downcase: false) + if downcase + "properties.#{field.es_code}.downcase" + else + "properties.#{field.es_code}" + end + end + def date_field_range(key, valid_value) date_from = valid_value[:date_from] date_to = valid_value[:date_to] @@ -78,20 +85,18 @@ def date_field_range(key, valid_value) def under(field, value) if value.blank? - add_filter missing: {field: field.es_code} + add_filter missing: {field: query_key(field)} return self end value = field.descendants_of_in_hierarchy value - query_key = field.es_code - add_filter terms: {query_key => value} + add_filter terms: {query_key(field) => value} self end def starts_with(field, value) validated_value = field.apply_format_query_validation(value, @use_codes_instead_of_es_codes) - query_key = field.es_code - add_prefix key: query_key, value: validated_value + add_prefix key: query_key(field), value: validated_value self end @@ -99,7 +104,7 @@ def starts_with(field, value) class_eval %Q( def #{op}(field, value) validated_value = field.apply_format_query_validation(value, @use_codes_instead_of_es_codes) - add_filter range: {field.es_code => {#{op}: validated_value}} + add_filter range: {query_key(field) => {#{op}: validated_value}} self end ) @@ -266,7 +271,7 @@ def hierarchy(es_code, value) if value.present? eq field, value else - add_filter not: {exists: {field: es_code}} + add_filter not: {exists: {field: query_key(field)}} end end