IMPORTANT: No additional bug fixes or documentation updates
will be released for this version. For the latest information, see the
current release documentation.
Examples
editExamples
editBelow you can find examples of how to use the most frequently called APIs with the Python client.
Indexing a document
editTo index a document, you need to specify three pieces of information: index
,
id
, and a body
:
from datetime import datetime from elasticsearch import Elasticsearch es = Elasticsearch('https://localhost:9200') doc = { 'author': 'author_name', 'text': 'Interensting content...', 'timestamp': datetime.now(), } resp = es.index(index="test-index", id=1, document=doc) print(resp['result'])
Getting a document
editTo get a document, you need to specify its index
and id
:
resp = es.get(index="test-index", id=1) print(resp['_source'])
Refreshing an index
editYou can perform the refresh operation on an index:
es.indices.refresh(index="test-index")
Searching for a document
editThe search()
method returns results that are matching a query:
resp = es.search(index="test-index", query={"match_all": {}}) print("Got %d Hits:" % resp['hits']['total']['value']) for hit in resp['hits']['hits']: print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
Updating a document
editTo update a document, you need to specify three pieces of information: index
,
id
, and a body
:
from datetime import datetime from elasticsearch import Elasticsearch client = Elasticsearch('https://localhost:9200') doc = { 'author': 'author_name', 'text': 'Interensting modified content...', 'timestamp': datetime.now(), } resp = client.update(index="test-index", id=1, document=doc) print(resp['result'])
Deleting a document
editYou can delete a document by specifying its index
, and id
in the delete()
method:
client.delete(index="test-index", id=1)