Skip to content

Instantly share code, notes, and snippets.

@neilghosh
Created February 10, 2018 14:29
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 neilghosh/06ca1ffb79381fbe6cd2e10aa3ed34e8 to your computer and use it in GitHub Desktop.
Save neilghosh/06ca1ffb79381fbe6cd2e10aa3ed34e8 to your computer and use it in GitHub Desktop.
Upload CSV data to Elastic Search Index
import csv
import json
import requests
# with open('products.csv', 'rb') as csvfile:
# spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
# for row in spamreader:
# print ', '.join(row)
url = "http://localhost:9200/_bulk?pretty"
def get_line():
with open('product1000.csv') as csvfile:
file = csv.reader(csvfile, delimiter=',', quotechar='|')
for i in file:
yield i
lines_required = 100
gen = get_line()
while True:
try:
chunk = [next(gen) for i in range(lines_required)]
json_data=""
for item in chunk:
json_data += json.dumps({"index" : {"_index" : "jda_com","_type" : "products","_id": item[0]}}) +"\n"+ json.dumps({"description":item[1]})+"\n"
payload = json_data.rstrip('\n')
print payload
headers = {'content-type': 'application/json'}
r = requests.post(url,data=payload,headers=headers)
print "chunk sent "+str(r.status_code)
except Exception as e:
print "done";
print(e)
break;
# curl -XPOST 'localhost:9200/_bulk?pretty' -H 'Content-Type: application/json' -d'
# { "index" : { "_index" : "jda_com", "_type" : "products", "_id" : "406c7989-fe9e-46ef-9f85-20506f1a7981" } }
# { "description" : "Some other value1" }
# { "index" : { "_index" : "jda_com", "_type" : "products", "_id" : "406c7989-fe9e-46ef-9f85-20506f1a7982" } }
# { "description" : "Some other value2" }
# '
@neilghosh
Copy link
Author

Queries

`curl -XPUT 'localhost:9200/jda_com/products/406c7989-fe9e-46ef-9f85-20506f1a7988?pretty' -H 'Content-Type: application/json' -d'
{
"description" : "Duvet Set Khaki Plaid "
}
'

GET

curl -XGET 'localhost:9200/jda_com/products/406c7989-fe9e-46ef-9f85-20506f1a7988?pretty'

curl -XGET 'localhost:9200/jda_com/products/_search'

curl -XGET 'localhost:9200/jda_com/products/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query" : {
"match" : {
"description" : "Duvet Plaid"
}
}
}
'

curl -XPOST 'localhost:9200/_bulk?pretty' -H 'Content-Type: application/json' -d'
{ "index" : { "_index" : "jda_com", "_type" : "products", "_id" : "406c7989-fe9e-46ef-9f85-20506f1a7981" } }
{ "description" : "Some other value1" }
{ "index" : { "_index" : "jda_com", "_type" : "products", "_id" : "406c7989-fe9e-46ef-9f85-20506f1a7982" } }
{ "description" : "Some other value2" }
'
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment