mirror of
https://github.com/flibusta-apps/meilie_updater.git
synced 2025-12-06 07:05:37 +01:00
Update documents structures
This commit is contained in:
@@ -31,12 +31,21 @@ async def update_books(ctx) -> bool:
|
|||||||
postgres_pool = await get_postgres_pool()
|
postgres_pool = await get_postgres_pool()
|
||||||
|
|
||||||
with concurrent.futures.ThreadPoolExecutor() as pool:
|
with concurrent.futures.ThreadPoolExecutor() as pool:
|
||||||
count = await postgres_pool.fetchval("SELECT count(*) FROM books;")
|
count = await postgres_pool.fetchval(
|
||||||
|
"SELECT count(*) FROM books WHERE is_deleted = 'f';"
|
||||||
|
)
|
||||||
|
|
||||||
for offset in range(0, count, 4096):
|
for offset in range(0, count, 4096):
|
||||||
rows = await postgres_pool.fetch(
|
rows = await postgres_pool.fetch(
|
||||||
"SELECT id, title, is_deleted FROM books"
|
"SELECT id, title, lang, "
|
||||||
f" ORDER BY id LIMIT 4096 OFFSET {offset}"
|
" array(SELECT author FROM book_authors "
|
||||||
|
" WHERE books.id = book_authors.book) as authors, "
|
||||||
|
" array(SELECT author FROM translations "
|
||||||
|
" WHERE books.id = translations.book) as translators, "
|
||||||
|
" array(SELECT sequence FROM book_sequences "
|
||||||
|
" WHERE books.id = book_sequences.book) as sequences "
|
||||||
|
"FROM books "
|
||||||
|
f"ORDER BY id LIMIT 4096 OFFSET {offset}"
|
||||||
)
|
)
|
||||||
|
|
||||||
documents = [dict(row) for row in rows]
|
documents = [dict(row) for row in rows]
|
||||||
@@ -44,7 +53,7 @@ async def update_books(ctx) -> bool:
|
|||||||
await loop.run_in_executor(pool, index.add_documents, documents)
|
await loop.run_in_executor(pool, index.add_documents, documents)
|
||||||
|
|
||||||
index.update_searchable_attributes(["title"])
|
index.update_searchable_attributes(["title"])
|
||||||
index.update_filterable_attributes(["is_deleted"])
|
index.update_filterable_attributes(["lang", "authors", "translators", "sequences"])
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -62,8 +71,15 @@ async def update_authors(ctx) -> bool:
|
|||||||
|
|
||||||
for offset in range(0, count, 4096):
|
for offset in range(0, count, 4096):
|
||||||
rows = await postgres_pool.fetch(
|
rows = await postgres_pool.fetch(
|
||||||
"SELECT id, first_name, last_name, middle_name FROM authors"
|
"SELECT id, first_name, last_name, middle_name, "
|
||||||
f" ORDER BY id LIMIT 4096 OFFSET {offset}"
|
" array("
|
||||||
|
" SELECT DISTINCT lang FROM book_authors "
|
||||||
|
" LEFT JOIN books ON book = books.id "
|
||||||
|
" WHERE authors.id = book_authors.author "
|
||||||
|
" AND books.is_deleted = 'f' "
|
||||||
|
" ) as langs "
|
||||||
|
"FROM authors "
|
||||||
|
f"ORDER BY id LIMIT 4096 OFFSET {offset}"
|
||||||
)
|
)
|
||||||
|
|
||||||
documents = [dict(row) for row in rows]
|
documents = [dict(row) for row in rows]
|
||||||
@@ -71,6 +87,7 @@ async def update_authors(ctx) -> bool:
|
|||||||
await loop.run_in_executor(pool, index.add_documents, documents)
|
await loop.run_in_executor(pool, index.add_documents, documents)
|
||||||
|
|
||||||
index.update_searchable_attributes(["first_name", "last_name", "middle_name"])
|
index.update_searchable_attributes(["first_name", "last_name", "middle_name"])
|
||||||
|
index.update_filterable_attributes(["langs"])
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -88,7 +105,15 @@ async def update_sequences(ctx) -> bool:
|
|||||||
|
|
||||||
for offset in range(0, count, 4096):
|
for offset in range(0, count, 4096):
|
||||||
rows = await postgres_pool.fetch(
|
rows = await postgres_pool.fetch(
|
||||||
f"SELECT id, name FROM sequences ORDER BY id LIMIT 4096 OFFSET {offset}"
|
"SELECT id, name, "
|
||||||
|
" array("
|
||||||
|
" SELECT DISTINCT lang FROM book_sequences "
|
||||||
|
" LEFT JOIN books ON book = books.id "
|
||||||
|
" WHERE sequences.id = book_sequences.sequence "
|
||||||
|
" AND books.is_deleted = 'f' "
|
||||||
|
" ) as langs "
|
||||||
|
"FROM sequences "
|
||||||
|
f"ORDER BY id LIMIT 4096 OFFSET {offset}"
|
||||||
)
|
)
|
||||||
|
|
||||||
documents = [dict(row) for row in rows]
|
documents = [dict(row) for row in rows]
|
||||||
@@ -96,6 +121,7 @@ async def update_sequences(ctx) -> bool:
|
|||||||
await loop.run_in_executor(pool, index.add_documents, documents)
|
await loop.run_in_executor(pool, index.add_documents, documents)
|
||||||
|
|
||||||
index.update_searchable_attributes(["name"])
|
index.update_searchable_attributes(["name"])
|
||||||
|
index.update_filterable_attributes(["langs"])
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
@@ -10,4 +10,5 @@ class WorkerSettings:
|
|||||||
functions = [update, update_books, update_authors, update_sequences]
|
functions = [update, update_books, update_authors, update_sequences]
|
||||||
on_startup = startup
|
on_startup = startup
|
||||||
redis_settings = get_redis_settings()
|
redis_settings = get_redis_settings()
|
||||||
max_jobs = 2
|
max_jobs = 3
|
||||||
|
job_timeout = 15 * 60
|
||||||
|
|||||||
Reference in New Issue
Block a user