Skip to content

Instantly share code, notes, and snippets.

@Yogendra0Sharma
Forked from arisetyo/get.py
Created May 26, 2017 08:23
Show Gist options
  • Save Yogendra0Sharma/42294d5d64437e3c96eb242277e1b425 to your computer and use it in GitHub Desktop.
Save Yogendra0Sharma/42294d5d64437e3c96eb242277e1b425 to your computer and use it in GitHub Desktop.
Using Python ElasticSearch Client
from elasticsearch import Elasticsearch
es = Elasticsearch()
res = es.get(index="belajar", doc_type='pesan', id=1)
print(res['_source'])
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch()
# WRITE TO ES
doc = {
'user': 'arisetyo',
'message': 'ini ceritanya tweet yang mau di-index. buat di-search entar broh..',
'postDate': datetime(2013, 01, 06, 10, 25, 10)
}
res = es.index(index="belajar", doc_type='pesan', id=1, body=doc)
print(res['ok'])
from elasticsearch import Elasticsearch
es = Elasticsearch()
# using range for query
'''
res = es.search(
index='belajar',
doc_type='pesan',
body={
'query': {
'range': {
'postDate': {
'from':'20100101', 'to':'20140101'
}
}
}
}
)
'''
res = es.search(
index='belajar',
doc_type='pesan',
body={
'query': {
'match': {
'user': 'arisetyo'
}
}
}
)
print("Got %d Hits" % res['hits']['total'])
for hit in res['hits']['hits']:
print("%(postDate)s %(user)s: %(message)s" % hit["_source"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment