Below you can find examples of how to use the most frequently called APIs with the Python client.
To index a document, you need to specify three pieces of information: index
, id
, and a document
:
from datetime import datetime
from elasticsearch import Elasticsearch
client = Elasticsearch('https://localhost:9200')
doc = {
'author': 'author_name',
'text': 'Interesting content...',
'timestamp': datetime.now(),
}
resp = client.index(index="test-index", id=1, document=doc)
print(resp['result'])
To get a document, you need to specify its index
and id
:
resp = client.get(index="test-index", id=1)
print(resp['_source'])
You can perform the refresh operation on an index:
client.indices.refresh(index="test-index")
The search()
method returns results that are matching a query:
resp = client.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"])
To update a document, you need to specify three pieces of information: index
, id
, and a doc
:
from datetime import datetime
from elasticsearch import Elasticsearch
client = Elasticsearch('https://localhost:9200')
doc = {
'author': 'author_name',
'text': 'Interesting modified content...',
'timestamp': datetime.now(),
}
resp = client.update(index="test-index", id=1, doc=doc)
print(resp['result'])
You can delete a document by specifying its index
, and id
in the delete()
method:
client.delete(index="test-index", id=1)
The elasticsearch-labs repo contains interactive and executable Python notebooks, sample apps, and resources for testing out Elasticsearch, using the Python client. These examples are mainly focused on vector search, hybrid search and generative AI use cases, but youâll also find examples of basic operations like creating index mappings and performing lexical search.
The Search folder is a good place to start if youâre new to Elasticsearch. This folder contains a number of notebooks that demonstrate the fundamentals of Elasticsearch, like indexing vectors, running lexical, semantic and hybrid searches, and more.
The following notebooks are available:
Hereâs a brief overview of what youâll learn in each notebook.
In the 00-quick-start.ipynb notebook youâll learn how to:
dense_vector
fields.In the 01-keyword-querying-filtering.ipynb notebook, youâll learn how to:
match
and multi-match
queries.text
, number
, date
, or boolean
values.multi-match
query.multi-match
query for tailored results.In the 02-hybrid-search.ipynb notebook, youâll learn how to:
match
query and a kNN
semantic search.In the 03-ELSER.ipynb notebook, youâll learn how to:
text_expansion
query.In the 04-multilingual.ipynb notebook, youâll learn how to:
kNN
semantic search.In the 05-query-rules.ipynb notebook, youâll learn how to:
rule_query
in Query DSL.In the 06-synonyms-api.ipynb notebook, youâll learn how to:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4