Skip to content

Instantly share code, notes, and snippets.

@Jammink2
Last active April 23, 2019 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jammink2/92a4a3da7a506cc920f57e7986828042 to your computer and use it in GitHub Desktop.
Save Jammink2/92a4a3da7a506cc920f57e7986828042 to your computer and use it in GitHub Desktop.
# loads json files into Elasticsearch
# borrowed heavily from http://carrefax.com/new-blog/2018/3/12/load-json-files-into-elasticsearch
import requests, json, os
from elasticsearch import Elasticsearch
# specify a directory
directory = str(os.getcwd())
# connect to Elasticsearch; by default, ES listens on port 9200
try:
res = requests.get('http://localhost:9200')
print(res.content)
es = Elasticsearch([{'host' : 'localhost', 'port' : '9200'}])
except requests.exceptions.ConnectionError:
print("Can't connect to the port!")
pass
# create an index value object
i = 1
#iterate over each JSON file and load it into Elasticsearch
for filename in os.listdir(directory):
if filename.endswith(".json"):
f = open(filename)
docket_content = f.read()
#send the data into es
es.index(index='myindex', ignore=400, doc_type='docket', id = i, body=json.loads(docket_content))
i = i + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment