Created
January 7, 2014 03:01
-
-
Save arisetyo/8294037 to your computer and use it in GitHub Desktop.
Using Python ElasticSearch Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from elasticsearch import Elasticsearch | |
es = Elasticsearch() | |
res = es.get(index="belajar", doc_type='pesan', id=1) | |
print(res['_source']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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