Add exact name match search syntax

This commit is contained in:
Correl Roush 2024-02-21 22:42:15 -05:00
parent e74fd6b9ba
commit 2f701156b3
5 changed files with 39 additions and 4 deletions

View file

@ -11,6 +11,7 @@ cards with certain properties.
- ~bolt~ :: Find all cards with "bolt" in the name - ~bolt~ :: Find all cards with "bolt" in the name
- ~"God of"~ :: Find all cards with "God of" in the name - ~"God of"~ :: Find all cards with "God of" in the name
- ~!"urza's incubator"~ :: Find all cards with the exact name "Urza's Incubator"
- ~t:legendary t:creature c:jund~ :: Find all legendary creatures with a color - ~t:legendary t:creature c:jund~ :: Find all legendary creatures with a color
identity of red/blue/green identity of red/blue/green
- ~color<=ubg~ :: Find all spells that are blue, black, green, or any - ~color<=ubg~ :: Find all spells that are blue, black, green, or any

View file

@ -118,8 +118,14 @@ async def advanced_search(
for i, criterion in enumerate(search.criteria): for i, criterion in enumerate(search.criteria):
param = f"param_{i}" param = f"param_{i}"
if isinstance(criterion, tutor.search.Name): if isinstance(criterion, tutor.search.Name):
constraints.append(f"cards.name ILIKE %({param})s") if criterion.operator == tutor.search.Operator.lte:
params[param] = f"%{criterion.text}%" logger.info("Search NAME LTE")
constraints.append(f"cards.name ILIKE %({param})s")
params[param] = f"%{criterion.text}%"
if criterion.operator == tutor.search.Operator.matches:
logger.info("Search NAME MATCHES")
constraints.append(f"cards.name ILIKE %({param})s")
params[param] = f"{criterion.text}"
if isinstance(criterion, tutor.search.Type): if isinstance(criterion, tutor.search.Type):
constraints.append(f"cards.type_line ILIKE %({param})s") constraints.append(f"cards.type_line ILIKE %({param})s")
params[param] = f"%{criterion.text}%" params[param] = f"%{criterion.text}%"

View file

@ -51,7 +51,7 @@ class Collection(Criterion):
@dataclasses.dataclass @dataclasses.dataclass
class Name(Criterion): class Name(Criterion):
operator: Operator = Operator.matches operator: Operator = Operator.lte
text: parsy.string = "" text: parsy.string = ""
@ -180,7 +180,13 @@ collection = parsy.seq(
text=string_literal, text=string_literal,
).combine_dict(Collection) ).combine_dict(Collection)
name = parsy.seq(text=string_literal).combine_dict(Name)
exact_name = parsy.seq(_prefix=lstring_from("!"), text=string_literal).combine_dict(
functools.partial(Name, operator=Operator.matches)
)
partial_name = parsy.seq(text=string_literal).combine_dict(Name)
name = exact_name | partial_name
criterion = colors | expansion | rarity | type_line | oracle | collection | name criterion = colors | expansion | rarity | type_line | oracle | collection | name

View file

@ -212,6 +212,7 @@ class SearchHandler(RequestHandler):
limit = int(self.get_argument("limit", "10")) limit = int(self.get_argument("limit", "10"))
sort_by = self.get_argument("sort_by", "rarity") sort_by = self.get_argument("sort_by", "rarity")
search = tutor.search.search.parse(query) search = tutor.search.search.parse(query)
logger.debug(search)
copies = await tutor.database.advanced_search( copies = await tutor.database.advanced_search(
cursor, cursor,
search, search,

View file

@ -108,21 +108,30 @@ paths:
`bolt` `bolt`
: Find all cards with "bolt" in the name : Find all cards with "bolt" in the name
`"God of"` `"God of"`
: Find all cards with "God of" in the name : Find all cards with "God of" in the name
`!"urza's incubator"`
: Find all cards with the exact name "Urza's Incubator"
`t:legendary t:creature c:jund` `t:legendary t:creature c:jund`
: Find all legendary creatures with a color : Find all legendary creatures with a color
identity of red/blue/green identity of red/blue/green
`color<=ubg` `color<=ubg`
: Find all spells that are blue, black, green, or any : Find all spells that are blue, black, green, or any
combination thereof. combination thereof.
`color:red set:stx rarity>=rare` `color:red set:stx rarity>=rare`
: Find all red cards in Strixhaven that are : Find all red cards in Strixhaven that are
rare or mythic rare or mythic
`t:enchantment o:"enters the battlefield"` `t:enchantment o:"enters the battlefield"`
: Find all enchantments with ETB : Find all enchantments with ETB
effects effects
@ -134,28 +143,35 @@ paths:
Keywords Keywords
: `c`, `color` : `c`, `color`
Operators Operators
: `:` (matches), `>=` (greater than or equal to), `<=` (less than : `:` (matches), `>=` (greater than or equal to), `<=` (less than
or equal to) or equal to)
Matches cards of the chosen color or colors. Matches cards of the chosen color or colors.
Single colors Single colors
: `w` or `white`, `u` or `blue`, `b` or `black, =g` or `green`, `r` or `red` : `w` or `white`, `u` or `blue`, `b` or `black, =g` or `green`, `r` or `red`
Any combination of abbreviated single colors Any combination of abbreviated single colors
: e.g.: `rg`, `uw`, or `wubgr` : e.g.: `rg`, `uw`, or `wubgr`
Ravnican guilds Ravnican guilds
: `boros` (white/red), `golgari` (green/black), `selesnya` : `boros` (white/red), `golgari` (green/black), `selesnya`
(green/white), `dimir` (blue/black), `orzhov` (white/black), `izzet` (green/white), `dimir` (blue/black), `orzhov` (white/black), `izzet`
(blue/red), `gruul` (red/green), `azorius` (white/blue), `rakdos` (black/red), (blue/red), `gruul` (red/green), `azorius` (white/blue), `rakdos` (black/red),
`simic` (green/blue) `simic` (green/blue)
Alaran shards Alaran shards
: `bant` (white/green/blue), `esper` (blue/white/black), : `bant` (white/green/blue), `esper` (blue/white/black),
`grixis` (black/blue/red), `jund` (red/black/green), `naya` (green/red/white) `grixis` (black/blue/red), `jund` (red/black/green), `naya` (green/red/white)
Tarkirian wedges Tarkirian wedges
: `abzan` (white/black/green), `jeskai` (white/blue/red), : `abzan` (white/black/green), `jeskai` (white/blue/red),
`sultai` (blue/black/green), `mardu` (white/black/red), `temur` `sultai` (blue/black/green), `mardu` (white/black/red), `temur`
@ -166,6 +182,7 @@ paths:
Keywords Keywords
: `s`, `set`, `e`, `expansion` : `s`, `set`, `e`, `expansion`
Operators Operators
: `:` (matches) : `:` (matches)
@ -174,6 +191,7 @@ paths:
Keywords Keywords
: `r`, `rarity` : `r`, `rarity`
Operators Operators
: `:` (matches), `>=` (greater than or equal to), `<=` (less than : `:` (matches), `>=` (greater than or equal to), `<=` (less than
or equal to) or equal to)
@ -183,6 +201,7 @@ paths:
Keywords Keywords
: `t`, `type` : `t`, `type`
Operators Operators
: `:` (matches) : `:` (matches)
@ -191,6 +210,7 @@ paths:
Keywords Keywords
: `o`, `oracle` : `o`, `oracle`
Operators Operators
: `:` (matches) : `:` (matches)
@ -199,6 +219,7 @@ paths:
Keywords Keywords
: `collection` : `collection`
Operators Operators
: `:` (matches) : `:` (matches)
responses: responses: