ElasticsearchStore#

pydantic model llama_index.vector_stores.ElasticsearchStore#

Elasticsearch vector store.

Parameters
  • index_name – Name of the Elasticsearch index.

  • es_client – Optional. Pre-existing AsyncElasticsearch client.

  • es_url – Optional. Elasticsearch URL.

  • es_cloud_id – Optional. Elasticsearch cloud ID.

  • es_api_key – Optional. Elasticsearch API key.

  • es_user – Optional. Elasticsearch username.

  • es_password – Optional. Elasticsearch password.

  • text_field – Optional. Name of the Elasticsearch field that stores the text.

  • vector_field – Optional. Name of the Elasticsearch field that stores the embedding.

  • batch_size – Optional. Batch size for bulk indexing. Defaults to 200.

  • distance_strategy – Optional. Distance strategy to use for similarity search. Defaults to β€œCOSINE”.

Raises
  • ConnectionError – If AsyncElasticsearch client cannot connect to Elasticsearch.

  • ValueError – If neither es_client nor es_url nor es_cloud_id is provided.

Show JSON schema
{
   "title": "ElasticsearchStore",
   "description": "Elasticsearch vector store.\n\nArgs:\n    index_name: Name of the Elasticsearch index.\n    es_client: Optional. Pre-existing AsyncElasticsearch client.\n    es_url: Optional. Elasticsearch URL.\n    es_cloud_id: Optional. Elasticsearch cloud ID.\n    es_api_key: Optional. Elasticsearch API key.\n    es_user: Optional. Elasticsearch username.\n    es_password: Optional. Elasticsearch password.\n    text_field: Optional. Name of the Elasticsearch field that stores the text.\n    vector_field: Optional. Name of the Elasticsearch field that stores the\n                embedding.\n    batch_size: Optional. Batch size for bulk indexing. Defaults to 200.\n    distance_strategy: Optional. Distance strategy to use for similarity search.\n                    Defaults to \"COSINE\".\n\nRaises:\n    ConnectionError: If AsyncElasticsearch client cannot connect to Elasticsearch.\n    ValueError: If neither es_client nor es_url nor es_cloud_id is provided.",
   "type": "object",
   "properties": {
      "stores_text": {
         "title": "Stores Text",
         "default": true,
         "type": "boolean"
      },
      "is_embedding_query": {
         "title": "Is Embedding Query",
         "default": true,
         "type": "boolean"
      },
      "index_name": {
         "title": "Index Name",
         "type": "string"
      },
      "es_client": {
         "title": "Es Client"
      },
      "es_url": {
         "title": "Es Url",
         "type": "string"
      },
      "es_cloud_id": {
         "title": "Es Cloud Id",
         "type": "string"
      },
      "es_api_key": {
         "title": "Es Api Key",
         "type": "string"
      },
      "es_user": {
         "title": "Es User",
         "type": "string"
      },
      "es_password": {
         "title": "Es Password",
         "type": "string"
      },
      "text_field": {
         "title": "Text Field",
         "default": "content",
         "type": "string"
      },
      "vector_field": {
         "title": "Vector Field",
         "default": "embedding",
         "type": "string"
      },
      "batch_size": {
         "title": "Batch Size",
         "default": 200,
         "type": "integer"
      },
      "distance_strategy": {
         "title": "Distance Strategy",
         "default": "COSINE",
         "enum": [
            "COSINE",
            "DOT_PRODUCT",
            "EUCLIDEAN_DISTANCE"
         ],
         "type": "string"
      },
      "class_name": {
         "title": "Class Name",
         "type": "string",
         "default": "base_component"
      }
   },
   "required": [
      "index_name"
   ]
}

Config
  • schema_extra: function = <function BaseComponent.Config.schema_extra at 0x7ff1e41e53a0>

Fields
  • batch_size (int)

  • distance_strategy (Optional[Literal['COSINE', 'DOT_PRODUCT', 'EUCLIDEAN_DISTANCE']])

  • es_api_key (Optional[str])

  • es_client (Optional[Any])

  • es_cloud_id (Optional[str])

  • es_password (Optional[str])

  • es_url (Optional[str])

  • es_user (Optional[str])

  • index_name (str)

  • stores_text (bool)

  • text_field (str)

  • vector_field (str)

field batch_size: int = 200#
field distance_strategy: Optional[Literal['COSINE', 'DOT_PRODUCT', 'EUCLIDEAN_DISTANCE']] = 'COSINE'#
field es_api_key: Optional[str] = None#
field es_client: Optional[Any] = None#
field es_cloud_id: Optional[str] = None#
field es_password: Optional[str] = None#
field es_url: Optional[str] = None#
field es_user: Optional[str] = None#
field index_name: str [Required]#
field stores_text: bool = True#
field text_field: str = 'content'#
field vector_field: str = 'embedding'#
add(nodes: List[BaseNode], *, create_index_if_not_exists: bool = True, **add_kwargs: Any) List[str]#

Add nodes to Elasticsearch index.

Parameters
  • nodes – List of nodes with embeddings.

  • create_index_if_not_exists – Optional. Whether to create the Elasticsearch index if it doesn’t already exist. Defaults to True.

Returns

List of node IDs that were added to the index.

Raises
  • ImportError – If elasticsearch[β€˜async’] python package is not installed.

  • BulkIndexError – If AsyncElasticsearch async_bulk indexing fails.

async adelete(ref_doc_id: str, **delete_kwargs: Any) None#

Async delete node from Elasticsearch index.

Parameters
  • ref_doc_id – ID of the node to delete.

  • delete_kwargs – Optional. Additional arguments to pass to AsyncElasticsearch delete_by_query.

Raises

Exception – If AsyncElasticsearch delete_by_query fails.

async aquery(query: VectorStoreQuery, custom_query: Optional[Callable[[Dict, Optional[VectorStoreQuery]], Dict]] = None, es_filter: Optional[List[Dict]] = None, **kwargs: Any) VectorStoreQueryResult#

Asynchronous query index for top k most similar nodes.

Parameters
  • query_embedding (VectorStoreQuery) – query embedding

  • custom_query – Optional. custom query function that takes in the es query body and returns a modified query body. This can be used to add additional query parameters to the AsyncElasticsearch query.

  • es_filter – Optional. AsyncElasticsearch filter to apply to the query. If filter is provided in the query, this filter will be ignored.

Returns

Result of the query.

Return type

VectorStoreQueryResult

Raises

Exception – If AsyncElasticsearch query fails.

async async_add(nodes: List[BaseNode], *, create_index_if_not_exists: bool = True, **add_kwargs: Any) List[str]#

Asynchronous method to add nodes to Elasticsearch index.

Parameters
  • nodes – List of nodes with embeddings.

  • create_index_if_not_exists – Optional. Whether to create the AsyncElasticsearch index if it doesn’t already exist. Defaults to True.

Returns

List of node IDs that were added to the index.

Raises
  • ImportError – If elasticsearch python package is not installed.

  • BulkIndexError – If AsyncElasticsearch async_bulk indexing fails.

delete(ref_doc_id: str, **delete_kwargs: Any) None#

Delete node from Elasticsearch index.

Parameters
  • ref_doc_id – ID of the node to delete.

  • delete_kwargs – Optional. Additional arguments to pass to Elasticsearch delete_by_query.

Raises

Exception – If Elasticsearch delete_by_query fails.

static get_user_agent() str#

Get user agent for elasticsearch client.

query(query: VectorStoreQuery, custom_query: Optional[Callable[[Dict, Optional[VectorStoreQuery]], Dict]] = None, es_filter: Optional[List[Dict]] = None, **kwargs: Any) VectorStoreQueryResult#

Query index for top k most similar nodes.

Parameters
  • query_embedding (List[float]) – query embedding

  • custom_query – Optional. custom query function that takes in the es query body and returns a modified query body. This can be used to add additional query parameters to the Elasticsearch query.

  • es_filter – Optional. Elasticsearch filter to apply to the query. If filter is provided in the query, this filter will be ignored.

Returns

Result of the query.

Return type

VectorStoreQueryResult

Raises

Exception – If Elasticsearch query fails.

property client: Any#

Get async elasticsearch client.