Skip to content

Instantly share code, notes, and snippets.

@arisetyo
Created January 7, 2014 03:01
Show Gist options
  • Save arisetyo/8294037 to your computer and use it in GitHub Desktop.
Save arisetyo/8294037 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