Skip to content

Commit

Permalink
Fix: search by fullpath instead of multi field name (removed in elast…
Browse files Browse the repository at this point in the history
…icsearch 2)
  • Loading branch information
ysbaddaden committed Nov 14, 2023
1 parent 2a10b79 commit 1617844
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
4 changes: 2 additions & 2 deletions app/models/elastic_search/query_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions app/models/field/identifier_fields/luhn_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 12 additions & 7 deletions app/models/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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)
Expand Down
35 changes: 20 additions & 15 deletions app/models/search_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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]
Expand All @@ -78,28 +85,26 @@ 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

['lt', 'lte', 'gt', 'gte'].each do |op|
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
)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 1617844

Please sign in to comment.