c56bef871b
Sync docs with Docusaurus / sync (push) Waiting to run
Tests / Check if changed (push) Waiting to run
Tests / format (push) Blocked by required conditions
Tests / check-imports (push) Blocked by required conditions
Tests / Unit / macos-latest (push) Blocked by required conditions
Tests / Unit / ubuntu-latest (push) Blocked by required conditions
Tests / Unit / windows-latest (push) Blocked by required conditions
Tests / mypy (push) Blocked by required conditions
Tests / Integration / ubuntu-latest (push) Blocked by required conditions
Tests / Integration / macos-latest (push) Blocked by required conditions
Tests / Integration / windows-latest (push) Blocked by required conditions
Tests / notify-slack-on-failure (push) Blocked by required conditions
Tests / Mark tests as completed (push) Blocked by required conditions
Docker image release / Build base image (push) Waiting to run
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
68 lines
2.7 KiB
Plaintext
68 lines
2.7 KiB
Plaintext
---
|
||
title: "ElasticsearchDocumentStore"
|
||
id: elasticsearch-document-store
|
||
slug: "/elasticsearch-document-store"
|
||
description: "Use an Elasticsearch database with Haystack."
|
||
---
|
||
|
||
# ElasticsearchDocumentStore
|
||
|
||
Use an Elasticsearch database with Haystack.
|
||
|
||
<div className="key-value-table">
|
||
|
||
| | |
|
||
| --- | --- |
|
||
| API reference | [Elasticsearch](/reference/integrations-elasticsearch) |
|
||
| GitHub link | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/elasticsearch |
|
||
|
||
</div>
|
||
|
||
ElasticsearchDocumentStore is excellent if you want to evaluate the performance of different retrieval options (dense vs. sparse) and aim for a smooth transition from PoC to production.
|
||
|
||
It features the approximate nearest neighbours (ANN) search.
|
||
|
||
### Initialization
|
||
|
||
[Install](https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html) Elasticsearch and then [start](https://www.elastic.co/guide/en/elasticsearch/reference/current/starting-elasticsearch.html) an instance. Haystack supports Elasticsearch 8.
|
||
|
||
If you have Docker set up, we recommend pulling the Docker image and running it.
|
||
|
||
```shell
|
||
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.11.1
|
||
docker run -p 9200:9200 -e "discovery.type=single-node" -e "ES_JAVA_OPTS=-Xms1024m -Xmx1024m" -e "xpack.security.enabled=false" elasticsearch:8.11.1
|
||
```
|
||
|
||
As an alternative, you can go to [Elasticsearch integration GitHub](https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/elasticsearch) and start a Docker container running Elasticsearch using the provided `docker-compose.yml`:
|
||
|
||
```shell
|
||
docker compose up
|
||
```
|
||
|
||
Once you have a running Elasticsearch instance, install the `elasticsearch-haystack` integration:
|
||
|
||
```shell
|
||
pip install elasticsearch-haystack
|
||
```
|
||
|
||
Then, initialize an `ElasticsearchDocumentStore` object that’s connected to the Elasticsearch instance and writes documents to it:
|
||
|
||
```python
|
||
from haystack_integrations.document_stores.elasticsearch import (
|
||
ElasticsearchDocumentStore,
|
||
)
|
||
from haystack import Document
|
||
|
||
document_store = ElasticsearchDocumentStore(hosts="http://localhost:9200")
|
||
document_store.write_documents(
|
||
[Document(content="This is first"), Document(content="This is second")],
|
||
)
|
||
print(document_store.count_documents())
|
||
```
|
||
|
||
### Supported Retrievers
|
||
|
||
[`ElasticsearchBM25Retriever`](../pipeline-components/retrievers/elasticsearchbm25retriever.mdx): A keyword-based Retriever that fetches documents matching a query from the Document Store.
|
||
|
||
[`ElasticsearchEmbeddingRetriever`](../pipeline-components/retrievers/elasticsearchembeddingretriever.mdx): Compares the query and document embeddings and fetches the documents most relevant to the query.
|