Skip to content

Latest commit

 

History

History
53 lines (39 loc) · 1.09 KB

File metadata and controls

53 lines (39 loc) · 1.09 KB
endpoint get
lang python
es_version 9.3
client elasticsearch==9.3.0

Elasticsearch 9.3 get endpoint (Python example)

Use client.get() to retrieve a document by its ID.

response = client.get(index="products", id="prod-1")

doc = response["_source"]
print(f"{doc['name']} — ${doc['price']}")

The response includes metadata (_index, _id, _version, _seq_no, _primary_term) alongside the _source document body.

Selecting fields

Use source_includes or source_excludes to return only the fields you need:

response = client.get(
    index="products",
    id="prod-1",
    source_includes=["name", "price"],
)

To fetch metadata without the source body, disable it entirely:

response = client.get(index="products", id="prod-1", source=False)
version = response["_version"]

Handling missing documents

A NotFoundError is raised when the document does not exist:

from elasticsearch import NotFoundError

try:
    response = client.get(index="products", id="prod-999")
except NotFoundError:
    print("Document not found")