redis data altered - new fields added for searchability
This commit is contained in:
parent
dc4f3a763a
commit
c24011e9bc
4
base.py
4
base.py
@ -19,12 +19,8 @@ app = FastAPI(title="codey.lol API",
|
||||
'name': 'codey'
|
||||
},
|
||||
loop=loop)
|
||||
|
||||
app.loop = loop
|
||||
|
||||
|
||||
|
||||
|
||||
constants = importlib.import_module("constants").Constants()
|
||||
util = importlib.import_module("util").Utilities(app, constants)
|
||||
glob_state = importlib.import_module("state").State(app, util, constants)
|
||||
|
@ -15,7 +15,7 @@ from lyric_search_new.constructors import LyricsResult
|
||||
import redis.asyncio as redis
|
||||
from redis.commands.search.query import Query
|
||||
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
|
||||
from redis.commands.search.field import TextField
|
||||
from redis.commands.search.field import TextField, TagField
|
||||
from redis.commands.json.path import Path
|
||||
from . import private
|
||||
|
||||
@ -49,8 +49,8 @@ class RedisCache:
|
||||
"""Create Index"""
|
||||
try:
|
||||
schema = (
|
||||
TextField("$.artist", as_name="artist"),
|
||||
TextField("$.song", as_name="song"),
|
||||
TextField("$.search_artist", as_name="artist"),
|
||||
TextField("$.search_song", as_name="song"),
|
||||
TextField("$.src", as_name="src"),
|
||||
TextField("$.lyrics", as_name="lyrics")
|
||||
)
|
||||
@ -61,12 +61,13 @@ class RedisCache:
|
||||
except Exception as e:
|
||||
await self.notifier.send(f"ERROR @ {__file__.rsplit("/", maxsplit=1)[-1]}", f"Failed to create idx: {str(e)}")
|
||||
|
||||
def sanitize_input(self, artist: str, song: str) -> tuple[str, str]:
|
||||
def sanitize_input(self, artist: str, song: str, fuzzy: bool = False) -> tuple[str, str]:
|
||||
"""
|
||||
Sanitize artist/song input (convert to redis matchable fuzzy query)
|
||||
Args:
|
||||
artist: Input artist
|
||||
song: Input song
|
||||
fuzzy: Whether to create fuzzy query str
|
||||
Returns:
|
||||
tuple[str, str]: Tuple containing the 2 output strings (artist, song)
|
||||
"""
|
||||
@ -74,6 +75,7 @@ class RedisCache:
|
||||
artist = self.regexes[1].sub("", artist).strip()
|
||||
song = self.regexes[0].sub("", song)
|
||||
song = self.regexes[1].sub("", song).strip()
|
||||
if fuzzy:
|
||||
artist = " ".join([f"(%{artist_word}%)" for artist_word in artist.split(" ")])
|
||||
song = " ".join([f"(%{song_word}%)" for song_word in song.split(" ")])
|
||||
return (artist, song)
|
||||
@ -102,6 +104,8 @@ class RedisCache:
|
||||
|
||||
if not is_random_search:
|
||||
logging.debug("Redis: Searching normally first")
|
||||
(artist, song) = self.sanitize_input(artist, song)
|
||||
logging.info("Seeking: %s - %s", artist, song)
|
||||
search_res = await self.redis_client.ft().search(Query(
|
||||
f"@artist:{artist} @song:{song}"
|
||||
))
|
||||
@ -144,11 +148,16 @@ class RedisCache:
|
||||
None
|
||||
"""
|
||||
try:
|
||||
(search_artist, search_song) = self.sanitize_input(lyr_result.artist,
|
||||
lyr_result.song)
|
||||
redis_mapping = {
|
||||
'id': sqlite_id,
|
||||
'src': lyr_result.src,
|
||||
'date_retrieved': time.time(),
|
||||
'artist': lyr_result.artist,
|
||||
'search_artist': search_artist,
|
||||
'search_song': search_song,
|
||||
'search_artistsong': f'{search_artist}\n{search_song}',
|
||||
'song': lyr_result.song,
|
||||
'artistsong': f"{lyr_result.artist}\n{lyr_result.song}",
|
||||
'confidence': lyr_result.confidence,
|
||||
|
Loading…
x
Reference in New Issue
Block a user