Skip to content

Instantly share code, notes, and snippets.

@Chestermozhao
Last active December 8, 2019 17:32
Show Gist options
  • Save Chestermozhao/7957726e4b37ae793c3a49e88be16375 to your computer and use it in GitHub Desktop.
Save Chestermozhao/7957726e4b37ae793c3a49e88be16375 to your computer and use it in GitHub Desktop.
elasticsearch insert one data
# -*- coding: utf-8 -*-
from elasticsearch import Elasticsearch
es = Elasticsearch(host="localhost", port=9200)
es = Elasticsearch()
data = {
"author": "Chestermo",
"gender": "male",
"age": "24",
"body_fat": "15%",
"interest": ["couch potato", "eat and sleep"]
}
def create_index(index):
es.indices.create(index=index, ignore=400)
def insert_one_data(_index, data):
# index and doc_type you can customize by yourself
res = es.index(index=_index, doc_type='authors', id=5, body=data)
# index will return insert info: like as created is True or False
print(res)
"""
{'_index': 'test-index', '_type': 'authors', '_id': '5', '_version': 1, 'result': 'created', '_shards': {'total': 2, 's
uccessful': 1, 'failed': 0}, '_seq_no': 4, '_primary_term': 1}
"""
index = "test-index"
create_index(index)
insert_one_data(index, data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment