2013-01-26 07:52:58 -08:00
|
|
|
# encoding=utf-8
|
|
|
|
# ^ to put the regex in utf-8 mode
|
|
|
|
|
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
|
|
|
class Item
|
|
|
|
module Search
|
|
|
|
class Query
|
2014-02-26 22:21:20 -08:00
|
|
|
def initialize(filters, user, text=nil)
|
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
|
|
|
@filters = filters
|
2013-01-22 21:52:34 -08:00
|
|
|
@user = user
|
2014-02-26 22:21:20 -08:00
|
|
|
@text = text
|
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
|
|
|
end
|
|
|
|
|
2023-07-22 18:13:11 -07:00
|
|
|
def results
|
|
|
|
@filters.map(&:to_query).inject(Item.all, &:merge).
|
|
|
|
alphabetize_by_translations(Query.locale)
|
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
|
|
|
end
|
2014-02-26 21:55:14 -08:00
|
|
|
|
|
|
|
def to_s
|
2014-02-26 22:21:20 -08:00
|
|
|
@text || @filters.map(&:to_s).join(' ')
|
2014-02-26 21:55:14 -08:00
|
|
|
end
|
2014-02-26 22:21:20 -08:00
|
|
|
|
2023-07-22 18:13:11 -07:00
|
|
|
def self.locale
|
|
|
|
(I18n.fallbacks[I18n.locale] &
|
|
|
|
I18n.locales_with_neopets_language_code).first
|
|
|
|
end
|
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
|
|
|
|
2013-01-26 07:52:58 -08:00
|
|
|
TEXT_FILTER_EXPR = /([+-]?)(?:(\p{Word}+):)?(?:"([^"]+)"|(\S+))/
|
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
|
|
|
def self.from_text(text, user=nil)
|
|
|
|
filters = []
|
2013-01-22 21:52:34 -08:00
|
|
|
|
2023-07-22 18:13:11 -07:00
|
|
|
text.scan(TEXT_FILTER_EXPR) do |sign, key, quoted_value, unquoted_value|
|
|
|
|
key = 'name' if key.blank?
|
|
|
|
value = quoted_value || unquoted_value
|
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
|
|
|
is_positive = (sign != '-')
|
|
|
|
|
2023-07-22 18:13:11 -07:00
|
|
|
case key
|
|
|
|
when 'name'
|
2023-07-26 11:46:10 -07:00
|
|
|
filters << (is_positive ?
|
|
|
|
Filter.name_includes(value, locale) :
|
|
|
|
Filter.name_excludes(value, locale))
|
2023-07-26 12:28:25 -07:00
|
|
|
when 'occupies'
|
|
|
|
filters << (is_positive ?
|
|
|
|
Filter.occupies(value, locale) :
|
|
|
|
Filter.not_occupies(value, locale))
|
2023-07-26 12:41:37 -07:00
|
|
|
when 'restricts'
|
|
|
|
filters << (is_positive ?
|
|
|
|
Filter.restricts(value, locale) :
|
|
|
|
Filter.not_restricts(value, locale))
|
2023-07-28 14:45:10 -07:00
|
|
|
when 'fits'
|
|
|
|
color_name, species_name = value.split('-')
|
|
|
|
begin
|
|
|
|
pet_type = PetType.matching_name(color_name, species_name, locale).first!
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
message = I18n.translate('items.search.errors.not_found.pet_type',
|
|
|
|
name1: color_name.capitalize, name2: species_name.capitalize)
|
|
|
|
raise Item::Search::Error, message
|
|
|
|
end
|
|
|
|
filters << (is_positive ?
|
|
|
|
Filter.fits(pet_type.body_id, color_name, species_name) :
|
|
|
|
Filter.not_fits(pet_type.body_id, color_name, species_name))
|
2023-07-28 15:06:33 -07:00
|
|
|
when 'user'
|
|
|
|
if user.nil?
|
|
|
|
message = I18n.translate('items.search.errors.not_logged_in')
|
|
|
|
raise Item::Search::Error, message
|
|
|
|
end
|
|
|
|
case value
|
|
|
|
when 'owns'
|
|
|
|
filters << (is_positive ?
|
2023-07-29 12:48:45 -07:00
|
|
|
Filter.owned_by(user) :
|
|
|
|
Filter.not_owned_by(user))
|
2023-07-28 15:06:33 -07:00
|
|
|
when 'wants'
|
2023-07-29 12:48:45 -07:00
|
|
|
filters << (is_positive ?
|
|
|
|
Filter.wanted_by(user) :
|
|
|
|
Filter.not_wanted_by(user))
|
2023-07-28 15:06:33 -07:00
|
|
|
else
|
|
|
|
message = I18n.translate('items.search.errors.not_found.ownership',
|
|
|
|
keyword: value)
|
|
|
|
raise Item::Search::Error, message
|
|
|
|
end
|
2023-07-22 18:13:11 -07:00
|
|
|
when 'is'
|
|
|
|
case value
|
|
|
|
when 'nc'
|
2023-07-26 11:46:10 -07:00
|
|
|
filters << (is_positive ? Filter.is_nc : Filter.is_not_nc)
|
2023-07-22 18:13:11 -07:00
|
|
|
when 'np'
|
2023-07-26 11:46:10 -07:00
|
|
|
filters << (is_positive ? Filter.is_np : Filter.is_not_np)
|
2023-07-26 11:51:52 -07:00
|
|
|
when 'pb'
|
|
|
|
filters << (is_positive ? Filter.is_pb : Filter.is_not_pb)
|
2023-07-22 18:13:11 -07:00
|
|
|
else
|
|
|
|
message = I18n.translate('items.search.errors.not_found.label',
|
2023-07-26 11:06:36 -07:00
|
|
|
:label => "is:#{value}")
|
2023-07-22 18:13:11 -07:00
|
|
|
raise Item::Search::Error, message
|
2013-01-26 07:52:58 -08:00
|
|
|
end
|
|
|
|
else
|
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
|
|
|
message = I18n.translate('items.search.errors.not_found.label',
|
2023-07-22 18:13:11 -07:00
|
|
|
:label => key)
|
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
|
|
|
raise Item::Search::Error, message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-26 22:21:20 -08:00
|
|
|
self.new(filters, user, text)
|
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
|
|
|
end
|
2014-02-26 21:55:14 -08:00
|
|
|
|
|
|
|
def self.from_params(params, user=nil)
|
2023-07-29 13:14:23 -07:00
|
|
|
filters = []
|
|
|
|
|
|
|
|
params.values.each do |filter_params|
|
|
|
|
key = filter_params[:key]
|
|
|
|
value = filter_params[:value]
|
|
|
|
is_positive = filter_params[:is_positive] != 'false'
|
|
|
|
|
|
|
|
case filter_params[:key]
|
|
|
|
when 'name'
|
|
|
|
filters << (is_positive ?
|
|
|
|
Filter.name_includes(value, locale) :
|
|
|
|
Filter.name_excludes(value, locale))
|
|
|
|
when 'is_nc'
|
|
|
|
filters << (is_positive ? Filter.is_nc : Filter.is_not_nc)
|
|
|
|
when 'occupied_zone_set_name'
|
|
|
|
filters << (is_positive ?
|
|
|
|
Filter.occupies(value, locale) :
|
|
|
|
Filter.not_occupies(value, locale))
|
|
|
|
when 'restricted_zone_set_name'
|
|
|
|
filters << (is_positive ?
|
|
|
|
Filter.restricts(value, locale) :
|
|
|
|
Filter.not_restricts(value, locale))
|
|
|
|
when 'fits_pet_type'
|
|
|
|
pet_type = PetType.find(value)
|
|
|
|
color_name = pet_type.color.name
|
|
|
|
species_name = pet_type.species.name
|
|
|
|
filters << (is_positive ?
|
|
|
|
Filter.fits(pet_type.body_id, color_name, species_name) :
|
|
|
|
Filter.not_fits(pet_type.body_id, color_name, species_name))
|
|
|
|
when 'user_closet_hanger_ownership'
|
|
|
|
case value
|
|
|
|
when 'true'
|
|
|
|
filters << (is_positive ?
|
|
|
|
Filter.owned_by(user) :
|
|
|
|
Filter.not_owned_by(user))
|
|
|
|
when 'false'
|
|
|
|
filters << (is_positive ?
|
|
|
|
Filter.wanted_by(user) :
|
|
|
|
Filter.not_wanted_by(user))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Rails.logger.warn "Ignoring unexpected search filter key: #{key}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.new(filters, user)
|
2023-07-22 18:13:11 -07:00
|
|
|
end
|
|
|
|
end
|
2014-04-02 18:26:53 -07:00
|
|
|
|
2023-07-22 18:13:11 -07:00
|
|
|
class Error < Exception
|
|
|
|
end
|
2014-04-02 18:26:53 -07:00
|
|
|
|
2023-07-22 18:13:11 -07:00
|
|
|
private
|
2014-02-26 21:55:14 -08:00
|
|
|
|
2023-07-26 11:46:10 -07:00
|
|
|
# A Filter is basically a wrapper for an Item scope, with extra info about
|
|
|
|
# how to convert it into a search query string.
|
|
|
|
class Filter
|
2023-07-29 13:14:23 -07:00
|
|
|
def initialize(query, text)
|
2023-07-26 11:46:10 -07:00
|
|
|
@query = query
|
2023-07-29 13:14:23 -07:00
|
|
|
@text = text
|
2014-02-26 21:55:14 -08:00
|
|
|
end
|
2023-07-22 18:13:11 -07:00
|
|
|
|
|
|
|
def to_query
|
2023-07-26 11:46:10 -07:00
|
|
|
@query
|
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
|
|
|
end
|
2023-07-22 18:13:11 -07:00
|
|
|
|
|
|
|
def to_s
|
2023-07-29 13:14:23 -07:00
|
|
|
@text
|
2023-07-22 18:13:11 -07:00
|
|
|
end
|
|
|
|
|
2023-07-26 11:46:10 -07:00
|
|
|
def inspect
|
|
|
|
"#<#{self.class.name} #{@text.inspect}>"
|
2023-07-22 18:13:11 -07:00
|
|
|
end
|
|
|
|
|
2023-07-26 11:46:10 -07:00
|
|
|
def self.name_includes(value, locale)
|
|
|
|
text = /\s/.match(value) ? '"' + value + '"' : value
|
|
|
|
self.new Item.name_includes(value, locale), text
|
2023-07-22 18:13:11 -07:00
|
|
|
end
|
|
|
|
|
2023-07-26 11:46:10 -07:00
|
|
|
def self.name_excludes(value, locale)
|
|
|
|
text = '-' + (/\s/.match(value) ? '"' + value + '"' : value)
|
|
|
|
self.new Item.name_excludes(value, locale), text
|
2023-07-22 18:13:11 -07:00
|
|
|
end
|
|
|
|
|
2023-07-26 12:28:25 -07:00
|
|
|
def self.occupies(value, locale)
|
|
|
|
self.new Item.occupies(value, locale), "occupies:#{value}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.not_occupies(value, locale)
|
|
|
|
self.new Item.not_occupies(value, locale), "-occupies:#{value}"
|
|
|
|
end
|
|
|
|
|
2023-07-26 12:41:37 -07:00
|
|
|
def self.restricts(value, locale)
|
|
|
|
self.new Item.restricts(value, locale), "restricts:#{value}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.not_restricts(value, locale)
|
|
|
|
self.new Item.not_restricts(value, locale), "-restricts:#{value}"
|
|
|
|
end
|
|
|
|
|
2023-07-28 14:45:10 -07:00
|
|
|
def self.fits(body_id, color_name, species_name)
|
|
|
|
# NOTE: Some color syntaxes are weird, like `fits:"polka dot-aisha"`!
|
|
|
|
value = "#{color_name.downcase}-#{species_name.downcase}"
|
|
|
|
value = '"' + value + '"' if value.include? ' '
|
|
|
|
self.new Item.fits(body_id), "fits:#{value}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.not_fits(body_id, color_name, species_name)
|
|
|
|
# NOTE: Some color syntaxes are weird, like `fits:"polka dot-aisha"`!
|
|
|
|
value = "#{color_name.downcase}-#{species_name.downcase}"
|
|
|
|
value = '"' + value + '"' if value.include? ' '
|
|
|
|
self.new Item.not_fits(body_id), "-fits:#{value}"
|
|
|
|
end
|
|
|
|
|
2023-07-29 12:48:45 -07:00
|
|
|
def self.owned_by(user)
|
2023-07-28 15:06:33 -07:00
|
|
|
self.new user.owned_items, 'user:owns'
|
|
|
|
end
|
|
|
|
|
2023-07-29 12:48:45 -07:00
|
|
|
def self.not_owned_by(user)
|
|
|
|
self.new user.unowned_items, 'user:owns'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.wanted_by(user)
|
2023-07-28 15:06:33 -07:00
|
|
|
self.new user.wanted_items, 'user:wants'
|
|
|
|
end
|
|
|
|
|
2023-07-29 12:48:45 -07:00
|
|
|
def self.not_wanted_by(user)
|
|
|
|
self.new user.unwanted_items, 'user:wants'
|
|
|
|
end
|
|
|
|
|
2023-07-26 11:46:10 -07:00
|
|
|
def self.is_nc
|
|
|
|
self.new Item.is_nc, 'is:nc'
|
2023-07-22 18:13:11 -07:00
|
|
|
end
|
|
|
|
|
2023-07-26 11:46:10 -07:00
|
|
|
def self.is_not_nc
|
|
|
|
self.new Item.is_np, '-is:nc'
|
2023-07-22 18:13:11 -07:00
|
|
|
end
|
|
|
|
|
2023-07-26 11:46:10 -07:00
|
|
|
def self.is_np
|
|
|
|
self.new Item.is_np, 'is:np'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_not_np
|
|
|
|
self.new Item.is_nc, '-is:np'
|
2023-07-22 18:13:11 -07:00
|
|
|
end
|
2023-07-26 11:51:52 -07:00
|
|
|
|
|
|
|
def self.is_pb
|
|
|
|
self.new Item.is_pb, 'is:pb'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_not_pb
|
|
|
|
self.new Item.is_not_pb, '-is:pb'
|
|
|
|
end
|
2023-07-28 14:45:10 -07:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.build_fits_filter_text(color_name, species_name)
|
|
|
|
# NOTE: Colors like "Polka Dot" must be written as
|
|
|
|
# `fits:"polka dot-aisha"`.
|
|
|
|
value = "#{color_name.downcase}-#{species_name.downcase}"
|
|
|
|
value = '"' + value + '"' if value.include? ' '
|
|
|
|
"fits:#{value}"
|
|
|
|
end
|
globalized search first draft
Confirmed features:
* Output (retrieval, sorting, etc.)
* Name (positive and negative, but new behavior)
* Flags (positive and negative)
Planned features:
* users:owns, user:wants
Known issues:
* Sets are broken
* Don't render properly
* Shouldn't actually be done as joined sets, anyway, since
we actually want (set1_zone1 OR set1_zone2) AND
(set2_zone1 OR set2_zone2), which will require breaking
it into multiple terms queries.
* Name has regressed: ignores phrases, doesn't require *all*
words. While we're breaking sets into multiple queries,
maybe we'll do something similar for name. In fact, we
really kinda have to if we're gonna keep sorting by name,
since "straw hat" returns all hats. Eww.
2013-01-18 21:23:37 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|