-
Notifications
You must be signed in to change notification settings - Fork 47
/
index_data.py
42 lines (34 loc) · 1.19 KB
/
index_data.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
from __future__ import print_function
import sys
import time
from django.core.management.base import BaseCommand
from django.conf import settings
from elasticsearch_dsl.connections import connections
from elasticsearch.helpers import streaming_bulk
from qa.models import Question, Answer
from qa.search import index
class Command(BaseCommand):
def handle(self, *args, **kwargs):
self.es = connections.get_connection()
index.delete(ignore=404)
index.create()
self.verbose_run(Question)
self.verbose_run(Answer)
def verbose_run(self, model, report_every=100):
name = model._meta.verbose_name
print('Indexing %s: ' % name, end='')
start = time.time()
cnt = 0
for _ in streaming_bulk(
self.es,
(m.to_search().to_dict(True) for m in model.objects.all().iterator()),
index=settings.ES_INDEX,
doc_type=name.lower(),
):
cnt += 1
if cnt % report_every:
print('.', end='')
sys.stdout.flush()
print('DONE\nIndexing %d %s in %.2f seconds'% (
cnt, name, time.time() - start
))