-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_to_server.py
56 lines (48 loc) · 1.52 KB
/
push_to_server.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
52
53
54
55
56
from elasticsearch import Elasticsearch
import sys
import os
from elasticsearch import helpers
import json
# connect the the host
HOST = "http://localhost:9200/"
es = Elasticsearch(hosts=[HOST])
# create request_body
request_body = {
"settings" : {
"number_of_shards": 5,
"number_of_replicas": 0
}
}
# create mapping
mapping = {
"document": {
"properties": {
"id": {"type":"string"},
"title": {"type":"string", "analyzer": "english"},
"body": {"type":"string", "analyzer": "english"},
"categorie": {"type":"keyword"},
"viewcount": {"type":"integer"},
"score": {"type":"integer"},
"creation_date": {"type":"date", "format": "date_hour_minute_second"},
"answers": {"type":"string", "analyzer": "english"},
"comments": {"type":"string", "analyzer": "english"},
"answer_score": {"type":"integer"},
"accepted_answer": {"type":"string", "analyzer": "english"},
"accepted_answer_score": {"type":"integer"},
"link": {"type":"string", "analyzer": "english"},
}
}
}
# delete index if there is any
try:
es.indices.delete(index='index')
except:
pass
# create index and add mapping to it
es.indices.create(index = 'index', body = request_body)
es.indices.put_mapping(index="index", doc_type="document", body=mapping)
folder = tuple(os.listdir('json_bulks'))
# add all bulks to elasticsearch
for bulk in folder:
print(bulk)
os.system("curl -s -XPOST http://localhost:9200/_bulk --data-binary @json_bulks/" + bulk)