Skip to content

Commit 86be9a1

Browse files
committed
index-build
1 parent 39b2e74 commit 86be9a1

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

poliloom/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ API documentation available at http://localhost:8000/docs
7474
| `poliloom import-politicians` | Import politicians linking to existing entities |
7575
| `poliloom enrich-wikipedia` | Extract politician data from Wikipedia using AI |
7676
| `poliloom garbage-collect` | Remove entities deleted from Wikidata |
77-
| `poliloom index-build` | Build Meilisearch index (use `--rebuild` for first run) |
77+
| `poliloom index-build` | Build Meilisearch index (`--rebuild` to start fresh) |
7878
| `poliloom index-delete` | Delete the Meilisearch index |
7979
| `poliloom index-stats` | Show index status and task progress |
8080

poliloom/poliloom/cli.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -978,15 +978,15 @@ def index_delete(confirm):
978978
@click.option(
979979
"--rebuild",
980980
is_flag=True,
981-
help="Delete and recreate index before indexing (required for first run)",
981+
help="Delete and recreate index before indexing",
982982
)
983983
def index_build(batch_size, rebuild):
984984
"""Build Meilisearch index from database.
985985
986986
Indexes all documents from Location, Country, Language, and Politician tables.
987987
Uses upsert semantics - unchanged documents won't be re-embedded.
988988
989-
Use --rebuild to delete and recreate the index (required for first run).
989+
Use --rebuild to delete and recreate the index from scratch.
990990
"""
991991
from poliloom.search import INDEX_NAME, SearchDocument, SearchService
992992

@@ -999,6 +999,8 @@ def index_build(batch_size, rebuild):
999999
click.echo(f" Recreated index '{INDEX_NAME}'")
10001000
else:
10011001
click.echo("⏳ Building Meilisearch index...")
1002+
if search_service.ensure_index():
1003+
click.echo(f" Created index '{INDEX_NAME}'")
10021004

10031005
# Reindex all documents from each model type
10041006
models = _get_search_indexed_models()

poliloom/poliloom/search.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ def delete_index(self) -> None:
103103
raise
104104
logger.debug(f"Index '{INDEX_NAME}' does not exist, nothing to delete")
105105

106+
def ensure_index(self) -> bool:
107+
"""Create the index if it doesn't exist.
108+
109+
Returns:
110+
True if index was created, False if it already existed.
111+
"""
112+
try:
113+
self.client.get_index(INDEX_NAME)
114+
logger.debug(f"Index '{INDEX_NAME}' already exists")
115+
return False
116+
except meilisearch.errors.MeilisearchApiError as e:
117+
if "index_not_found" not in str(e):
118+
raise
119+
self.create_index()
120+
return True
121+
106122
def index_documents(self, documents: list[SearchDocument]) -> Optional[int]:
107123
"""Index documents to Meilisearch.
108124

0 commit comments

Comments
 (0)