Movatterモバイル変換


[0]ホーム

URL:


Loading

Examples

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 adocument:

from datetime import datetimefrom elasticsearch import Elasticsearchclient = 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 itsindex andid:

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")

Thesearch() 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 adoc:

from datetime import datetimefrom elasticsearch import Elasticsearchclient = 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 itsindex, andid in thedelete() method:

client.delete(index="test-index", id=1)

Theelasticsearch-labs repo contains interactive and executablePython 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.

TheSearch 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 andhybrid searches, and more.

The following notebooks are available:

Here’s a brief overview of what you’ll learn in each notebook.

In the00-quick-start.ipynb notebook you’ll learn how to:

  • Use the Elasticsearch Python client for various operations.
  • Create and define an index for a sample dataset withdense_vector fields.
  • Transform book titles into embeddings usingSentence Transformers and index them into Elasticsearch.
  • Perform k-nearest neighbors (knn) semantic searches.
  • Integrate traditional text-based search with semantic search, for a hybrid search system.
  • Use reciprocal rank fusion (RRF) to intelligently combine search results from different retrieval systems.

In the01-keyword-querying-filtering.ipynb notebook, you’ll learn how to:

  • Usequery and filter contexts to search and filter documents in Elasticsearch.
  • Execute full-text searches withmatch andmulti-match queries.
  • Query and filter documents based ontext,number,date, orboolean values.
  • Run multi-field searches using themulti-match query.
  • Prioritize specific fields in themulti-match query for tailored results.

In the02-hybrid-search.ipynb notebook, you’ll learn how to:

  • Combine results of traditional text-based search with semantic search, for a hybrid search system.
  • Transform fields in the sample dataset into embeddings using the Sentence Transformer model and index them into Elasticsearch.
  • Use theRRF API to combine the results of amatch query and akNN semantic search.
  • Walk through a super simple toy example that demonstrates, step by step, how RRF ranking works.

In the03-ELSER.ipynb notebook, you’ll learn how to:

  • Use the Elastic Learned Sparse Encoder (ELSER) for text expansion-powered semantic search, out of the box — without training, fine-tuning, or embeddings generation.
  • Download and deploy the ELSER model in your Elastic environment.
  • Create an Elasticsearch index named search-movies with specific mappings and index a dataset of movie descriptions.
  • Create an ingest pipeline containing an inference processor for ELSER model execution.
  • Reindex the data from search-movies into another index, elser-movies, using the ELSER pipeline for text expansion.
  • Observe the results of running the documents through the model by inspecting the additional terms it adds to documents, which enhance searchability.
  • Perform simple keyword searches on the elser-movies index to assess the impact of ELSER’s text expansion.
  • Execute ELSER-powered semantic searches using thetext_expansion query.

In the04-multilingual.ipynb notebook, you’ll learn how to:

  • Use a multilingual embedding model for semantic search across languages.
  • Transform fields in the sample dataset into embeddings using the Sentence Transformer model and index them into Elasticsearch.
  • Use filtering with akNN semantic search.
  • Walk through a super simple toy example that demonstrates, step by step, how multilingual search works across languages, and within non-English languages.

In the05-query-rules.ipynb notebook, you’ll learn how to:

  • Use the query rules management APIs to create and edit promotional rules based on contextual queries.
  • Apply these query rules by using therule_query in Query DSL.

In the06-synonyms-api.ipynb notebook, you’ll learn how to:

  • Use the synonyms management API to create a synonyms set to enhance your search recall.
  • Configure an index to use search-time synonyms.
  • Update synonyms in real time.
  • Run queries that are enhanced by synonyms.
  • Generative AI. Notebooks that demonstrate various use cases for Elasticsearch as the retrieval engine and vector store for LLM-powered applications.
  • Integrations. Notebooks that demonstrate how to integrate popular services and projects with Elasticsearch, including OpenAI, Hugging Face, and LlamaIndex
  • Langchain. Notebooks that demonstrate how to integrate Elastic with LangChain, a framework for developing applications powered by language models.

Welcome to the docs for thelatest Elastic product versions, including Elastic Stack 9.0 and Elastic Cloud Serverless.To view previous versions, go toelastic.co/guide.


[8]ページ先頭

©2009-2025 Movatter.jp