# ElasticsearchEmbeddings

> **Class** in `langchain_elasticsearch`

📖 [View in docs](https://reference.langchain.com/python/langchain-elasticsearch/_sync/embeddings/ElasticsearchEmbeddings)

`Elasticsearch` embedding models.

This class provides an interface to generate embeddings using a model deployed
in an Elasticsearch cluster. It requires an Elasticsearch connection and the
model_id of the model deployed in the cluster.

In Elasticsearch you need to have an embedding model loaded and deployed.
- https://www.elastic.co/guide/en/elasticsearch/reference/current/infer-trained-model.html
- https://www.elastic.co/guide/en/machine-learning/current/ml-nlp-deploy-models.html

## Signature

```python
ElasticsearchEmbeddings(
    self,
    model_id: str,
    *,
    input_field: str = 'text_field',
    client: Optional[Elasticsearch] = None,
    es_url: Optional[str] = None,
    es_cloud_id: Optional[str] = None,
    es_user: Optional[str] = None,
    es_api_key: Optional[str] = None,
    es_password: Optional[str] = None,
)
```

## Description

**Setup:**

Install `langchain_elasticsearch` and start Elasticsearch locally using
the start-local script.

```bash
pip install -qU langchain_elasticsearch
curl -fsSL https://elastic.co/start-local | sh
```

This will create an `elastic-start-local` folder. To start Elasticsearch
and Kibana:
```bash
cd elastic-start-local
./start.sh
```

Elasticsearch will be available at `http://localhost:9200`. The password
for the `elastic` user and API key are stored in the `.env` file in the
`elastic-start-local` folder.

Initialize the ElasticsearchEmbeddings instance.

**Instantiate:**

```python
from langchain_elasticsearch import ElasticsearchEmbeddings

embeddings = ElasticsearchEmbeddings(
    model_id="your_model_id",
    es_url="http://localhost:9200"
)
```

**Instantiate with API key (URL):**
```python
from langchain_elasticsearch import ElasticsearchEmbeddings

embeddings = ElasticsearchEmbeddings(
    model_id="your_model_id",
    es_url="http://localhost:9200",
    es_api_key="your-api-key"
)
```

**Instantiate with username/password (URL):**
```python
from langchain_elasticsearch import ElasticsearchEmbeddings

embeddings = ElasticsearchEmbeddings(
    model_id="your_model_id",
    es_url="http://localhost:9200",
    es_user="elastic",
    es_password="password"
)
```

If you want to use a cloud hosted Elasticsearch instance, you can pass in the
es_cloud_id argument instead of the es_url argument.

**Instantiate from cloud (with username/password):**
    ```python
    from langchain_elasticsearch import ElasticsearchEmbeddings

    embeddings = ElasticsearchEmbeddings(
        model_id="your_model_id",
        es_cloud_id="<cloud_id>",
        es_user="elastic",
        es_password="<password>"
    )
    ```

**Instantiate from cloud (with API key):**
    ```python
    from langchain_elasticsearch import ElasticsearchEmbeddings

    embeddings = ElasticsearchEmbeddings(
        model_id="your_model_id",
        es_cloud_id="<cloud_id>",
        es_api_key="your-api-key"
    )
    ```

You can also connect to an existing Elasticsearch instance by passing in a
pre-existing Elasticsearch connection via the client argument.

**Instantiate from existing connection:**
    ```python
    from langchain_elasticsearch import ElasticsearchEmbeddings
    from elasticsearch import Elasticsearch

    client = Elasticsearch("http://localhost:9200")
    embeddings = ElasticsearchEmbeddings(
        model_id="your_model_id",
        client=client
    )
    ```

**Generate embeddings:**

```python
documents = [
    "This is an example document.",
    "Another example document to generate embeddings for.",
]
embeddings_list = embeddings.embed_documents(documents)
```

**Generate query embedding:**

```python
query_embedding = embeddings.embed_query("What is this about?")
```

For synchronous applications, use the `ElasticsearchEmbeddings` class.
For asynchronous applications, use the `AsyncElasticsearchEmbeddings` class.

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `model_id` | `str` | Yes | The model_id of the model deployed in the Elasticsearch cluster. |
| `input_field` | `str` | No | The name of the key for the input text field in the document. Defaults to text_field. (default: `'text_field'`) |
| `client` | `AsyncElasticsearch or Elasticsearch` | No |  Pre-existing Elasticsearch connection. Either provide this OR credentials. (default: `None`) |
| `es_url` | `str` | No | URL of the Elasticsearch instance to connect to. (default: `None`) |
| `es_cloud_id` | `str` | No | Cloud ID of the Elasticsearch instance. (default: `None`) |
| `es_user` | `str` | No | Username to use when connecting to Elasticsearch. (default: `None`) |
| `es_api_key` | `str` | No | API key to use when connecting to Elasticsearch. (default: `None`) |
| `es_password` | `str` | No | Password to use when connecting to Elasticsearch. (default: `None`) |

## Extends

- `Embeddings`

## Constructors

```python
__init__(
    self,
    model_id: str,
    *,
    input_field: str = 'text_field',
    client: Optional[Elasticsearch] = None,
    es_url: Optional[str] = None,
    es_cloud_id: Optional[str] = None,
    es_user: Optional[str] = None,
    es_api_key: Optional[str] = None,
    es_password: Optional[str] = None,
)
```

| Name | Type |
|------|------|
| `model_id` | `str` |
| `input_field` | `str` |
| `client` | `Optional[Elasticsearch]` |
| `es_url` | `Optional[str]` |
| `es_cloud_id` | `Optional[str]` |
| `es_user` | `Optional[str]` |
| `es_api_key` | `Optional[str]` |
| `es_password` | `Optional[str]` |


## Properties

- `client`
- `model_id`
- `input_field`

## Methods

- [`embed_documents()`](https://reference.langchain.com/python/langchain-elasticsearch/_sync/embeddings/ElasticsearchEmbeddings/embed_documents)
- [`embed_query()`](https://reference.langchain.com/python/langchain-elasticsearch/_sync/embeddings/ElasticsearchEmbeddings/embed_query)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-elastic/blob/e78a327f38e09abd10128ed28c8774e9a8f9fec0/libs/elasticsearch/langchain_elasticsearch/_sync/embeddings.py#L15)