diff --git a/app/flex/flex_search.yml b/app/flex/flex_search.yml index 04b73ad5..f7189caa 100644 --- a/app/flex/flex_search.yml +++ b/app/flex/flex_search.yml @@ -7,6 +7,15 @@ ANCHORS: - &name_locale_partial "name.<>^<>" + - &species_support_id_partial + term: + species_support_id: <> + - &occupied_zone_id_partial + terms: + occupied_zone_id: <> + - &restricted_zone_id_partial + terms: + restricted_zone_id: <> _name_locales: *name_locale_partial @@ -14,6 +23,24 @@ _name_locales: _negative_name_locales: *name_locale_partial +_species_support_ids: + *species_support_id_partial + +_negative_species_support_ids: + *species_support_id_partial + +_occupied_zone_ids: + *occupied_zone_id_partial + +_negative_occupied_zone_ids: + *occupied_zone_id_partial + +_restricted_zone_ids: + *restricted_zone_id_partial + +_negative_restricted_zone_ids: + *restricted_zone_id_partial + item_search: - query: bool: @@ -25,21 +52,15 @@ item_search: is_nc: <> - term: is_pb: <> - - term: - species_support_id: <> - - term: - occupied_zone_id: <> - - term: - restricted_zone_id: <> + - <<_species_support_ids= ~>> + - <<_occupied_zone_ids= ~>> + - <<_restricted_zone_ids= ~>> must_not: - - term: - species_support_id: <> - - term: - occupied_zone_id: <> - - term: - restricted_zone_id: <> - multi_match: query: <> fields: <<_negative_name_locales>> + - <<_negative_species_support_ids= ~>> + - <<_negative_occupied_zone_ids= ~>> + - <<_negative_restricted_zone_ids= ~>> sort: - name.<>.untouched diff --git a/app/models/item/search/fields/set_field.rb b/app/models/item/search/fields/set_field.rb index 3598e60a..e089fa13 100644 --- a/app/models/item/search/fields/set_field.rb +++ b/app/models/item/search/fields/set_field.rb @@ -8,35 +8,25 @@ class Item end def <<(filter) - if filter.value.respond_to?(:each) - filter.value.each do |value| - add_value(value, filter.positive?) - end - else - add_value(filter.value, filter.positive?) + if @values[!filter.positive?].include?(filter.value) + raise Item::Search::Contradiction, + "positive #{key} and negative #{key} both contain #{filter.value}" end + + @values[filter.positive?] << filter.value end def to_flex_params { - key => nil_if_empty(@values[true]), - :"negative_#{key}" => nil_if_empty(@values[false]) + :"_#{key}s" => nil_if_empty(@values[true]), + :"_negative_#{key}s" => nil_if_empty(@values[false]) } end private - def add_value(value, is_positive) - if @values[!is_positive].include?(value) - raise Item::Search::Contradiction, - "positive #{key} and negative #{key} both contain #{value}" - end - - @values[is_positive] << value - end - def nil_if_empty(set) - set unless set.empty? + set.map { |value| {key => value} } unless set.empty? end end end