-
Notifications
You must be signed in to change notification settings - Fork 15
/
reindex.py
51 lines (40 loc) · 1.6 KB
/
reindex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Helper script to reindex all arXiv papers."""
import click
import time
from search.factory import create_ui_web_app
from search.services import index
app = create_ui_web_app()
@app.cli.command()
@click.argument("old_index", nargs=1)
@click.argument("new_index", nargs=1)
def reindex(old_index: str, new_index: str):
"""
Reindex the documents in `old_index` to `new_index`.
This will create `new_index` with the current configured mappings if it
does not already exist.
"""
click.echo(f"Reindex papers in `{old_index}` to `{new_index}`")
if not index.SearchSession.index_exists(old_index):
click.echo(f"Source index `{old_index}` does not exist.")
r = index.SearchSession.reindex(old_index, new_index)
if not r:
raise click.ClickException("Failed to get or create new index")
click.echo(f"Started reindexing task")
task_id = r["task"]
with click.progressbar(length=100, label="percent complete") as progress:
while True:
status = index.SearchSession.get_task_status(task_id)
total = float(status["task"]["status"]["total"])
if status["completed"] or total == 0:
progress.update(100)
break
updated = status["task"]["status"]["updated"]
created = status["task"]["status"]["created"]
deleted = status["task"]["status"]["deleted"]
complete = (updated + created + deleted) / total
progress.update(complete * 100)
if complete == 1:
break
time.sleep(2)
if __name__ == "__main__":
reindex()