-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.py
76 lines (52 loc) · 1.97 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
This file contains index operations.
Run the following to check the available methods:
.. code-block:: shellpython
python index.py --help
"""
import json
from pprint import pprint
import typer
from opensearchpy import helpers, OpenSearch
from config import INDEX_NAME, client
app = typer.Typer()
@app.command("load-data")
def load_data():
"""Send multiple data to an OpenSearch client."""
def load_data():
"""Yields data from json file."""
# full_format_recipes.json source:
# https://www.kaggle.com/hugodarwood/epirecipes?select=full_format_recipes.json
with open("full_format_recipes.json", "r") as f:
data = json.load(f)
for recipe in data:
yield {"_index": INDEX_NAME, "_source": recipe}
data = load_data()
print(f"Ingesting {INDEX_NAME} data")
response = helpers.bulk(client, data)
print(f"Data sent to your OpenSearch with response: {response}")
@app.command("delete-index")
def delete_index(index_name=INDEX_NAME):
"""Delete all the documents of certain index name, and do not raise exceptions"""
client.indices.delete(index=index_name, ignore=[400, 404])
@app.command("get-cluster-info")
def get_cluster_info():
"""Get information about your OpenSearch cluster"""
return pprint(OpenSearch.info(client), width=100, indent=1)
@app.command("get-mapping")
def get_mapping():
"""Retrieve mapping for the index.
The mapping lists all the fields and their data types."""
# list of all the cluster's indices
indices = client.indices.get_alias("*").keys()
# Example:
# dict_keys(['.kibana_1', 'epicurious-recipes'])
mapping_data = client.indices.get_mapping(INDEX_NAME)
# Find index doc_type
doc_type = list(mapping_data[INDEX_NAME]["mappings"].keys())[0]
schema = mapping_data[INDEX_NAME]["mappings"][doc_type]
pprint(list(schema.keys()))
print("\n")
pprint(schema, width=80, indent=0)
if __name__ == "__main__":
app()