-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulate_index.py
54 lines (48 loc) · 1.52 KB
/
populate_index.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
from elasticsearch import Elasticsearch
import random
import string
import uuid
# Populates the "sam_entities" elastic index with 50,000 random documents
def main():
number_of_records = 50000
elastic_client = Elasticsearch(
"http://localhost:9200"
)
if elastic_client.ping():
print("Connected to Elasticsearch")
if "sam_entities" not in elastic_client.indices.get_alias().keys():
response = elastic_client.indices.create(index="sam_entities")
print("sam_entities index does not exist, creating...", response)
else:
response = elastic_client.delete_by_query(
index="sam_entities",
body={
"query": {
"match_all": {}
}
}
)
print("Deleting previous documents... ", response)
for i in range(number_of_records):
name = "Test" + ''.join(random.choice(string.ascii_lowercase) for i in range(16))
cage_code = build_cage_code()
id = uuid.uuid4()
sam_entity = {
"id": id,
"name": name,
"cage_code": cage_code,
"num": i
}
print("Inserting into Elastic index: ", sam_entity)
elastic_client.index(
index='sam_entities',
document=sam_entity
)
def build_cage_code() -> string:
cage_code = ""
for i in range(6):
rand = random.randint(1, 9)
cage_code = cage_code + str(rand)
return cage_code
if __name__ == "__main__":
main()