From cded361f73c8aaa30da710ec58ba3e128752c24f Mon Sep 17 00:00:00 2001 From: Matchu Date: Mon, 21 Jan 2013 13:17:36 -0600 Subject: [PATCH] update item search to original name-matching behavior We originally had a regression on name-matching, where, among other issues, `straw hat` returned items containing both "straw" and "hat", which isn't really helpful behavior since we're sorting alphabetically. Now, `straw hat` behaves as expected. Additionally, "phrases like these" behave as expected, too. --- app/flex/flex_search.yml | 24 ++++++++++++------------ app/models/item/search/query.rb | 27 +++++++++++++++++---------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/app/flex/flex_search.yml b/app/flex/flex_search.yml index f7189caa..a3b3522c 100644 --- a/app/flex/flex_search.yml +++ b/app/flex/flex_search.yml @@ -5,8 +5,6 @@ # ANCHORS litheral key: it will not be used as template # you can store here fragments of queries to reuse in the templates below ANCHORS: - - &name_locale_partial - "name.<>^<>" - &species_support_id_partial term: species_support_id: <> @@ -17,11 +15,17 @@ ANCHORS: terms: restricted_zone_id: <> -_name_locales: - *name_locale_partial +_names: + multi_match: + query: <> + fields: <> + type: phrase -_negative_name_locales: - *name_locale_partial +_negative_names: + multi_match: + query: <> + fields: <> + type: phrase _species_support_ids: *species_support_id_partial @@ -45,20 +49,16 @@ item_search: - query: bool: must: - - multi_match: - query: <> - fields: <<_name_locales>> - term: is_nc: <> - term: is_pb: <> + - <<_names= ~>> - <<_species_support_ids= ~>> - <<_occupied_zone_ids= ~>> - <<_restricted_zone_ids= ~>> must_not: - - multi_match: - query: <> - fields: <<_negative_name_locales>> + - <<_negative_names= ~>> - <<_negative_species_support_ids= ~>> - <<_negative_occupied_zone_ids= ~>> - <<_negative_restricted_zone_ids= ~>> diff --git a/app/models/item/search/query.rb b/app/models/item/search/query.rb index 50fa95aa..dca74fdc 100644 --- a/app/models/item/search/query.rb +++ b/app/models/item/search/query.rb @@ -7,7 +7,7 @@ class Item :species_support_id => Fields::SetField, :occupied_zone_id => Fields::SetField, :restricted_zone_id => Fields::SetField, - :name => Fields::TextField + :name => Fields::SetField } def initialize(filters, user) @@ -42,23 +42,30 @@ class Item I18n.locales_with_neopets_language_code final_flex_params[:locale] = locales.first - if final_flex_params[:name] || final_flex_params[:negative_name] + # Extend the names/negative_names queries with the corresponding + # localalized field names. + if final_flex_params[:_names] || final_flex_params[:_negative_names] locale_entries = locales.map do |locale| boost = (locale == I18n.locale) ? 4 : 1 - {:locale => locale, :boost => boost} + "name.#{locale}^#{boost}" end - if final_flex_params[:name] - final_flex_params[:_name_locales] = locale_entries - end - - if final_flex_params[:negative_name] - final_flex_params[:_negative_name_locales] = locale_entries + # We *could* have set _name_locales once as a partial, but Flex won't + # let us call partials from inside other partials. Whatever. Assign + # it to each name entry instead. I also feel bad doing this + # afterwards, since it's kinda the field's job to return proper flex + # params, but that's a refactor for another day. + [:_names, :_negative_names].each do |key| + if final_flex_params[key] + final_flex_params[key].each do |name_query| + name_query[:fields] = locale_entries + end + end end end result = FlexSearch.item_search(final_flex_params) - result.scoped_loaded_collection(:scopes => {'Item' => Item.with_translations}) + result.loaded_collection end # Load the text query labels from I18n, so that when we see, say,