{"id":7351,"library":"langchain-elasticsearch","title":"LangChain Elasticsearch Integration","description":"LangChain Elasticsearch is an integration package that connects LangChain with Elasticsearch. It provides components for vector storage (`ElasticsearchStore`), chat message history (`ElasticsearchChatMessageHistory`), and embedding caching (`ElasticsearchEmbeddingsCache`). The current version is 1.0.0, and it follows a frequent release cadence, often aligning with LangChain Core updates.","status":"active","version":"1.0.0","language":"en","source_language":"en","source_url":"https://github.com/langchain-ai/langchain-elastic","tags":["langchain","elasticsearch","vectorstore","chat history","cache","embeddings"],"install":[{"cmd":"pip install langchain-elasticsearch","lang":"bash","label":"Install core package"},{"cmd":"pip install langchain-elasticsearch elasticsearch langchain-openai","lang":"bash","label":"Install with dependencies (e.g., OpenAI for embeddings)"}],"dependencies":[{"reason":"Required for connecting to Elasticsearch.","package":"elasticsearch","optional":false},{"reason":"Underpins all LangChain integrations.","package":"langchain-core","optional":false},{"reason":"Optional, for using OpenAI embeddings. Substitute with any other embedding provider.","package":"langchain-openai","optional":true}],"imports":[{"note":"The Elasticsearch vectorstore was moved to its own dedicated package in LangChain 0.1.0+.","wrong":"from langchain.vectorstores.elasticsearch import ElasticsearchStore","symbol":"ElasticsearchStore","correct":"from langchain_elasticsearch import ElasticsearchStore"},{"symbol":"ElasticsearchChatMessageHistory","correct":"from langchain_elasticsearch import ElasticsearchChatMessageHistory"},{"symbol":"ElasticsearchEmbeddingsCache","correct":"from langchain_elasticsearch import ElasticsearchEmbeddingsCache"}],"quickstart":{"code":"import os\nfrom langchain_elasticsearch import ElasticsearchStore\nfrom langchain_openai import OpenAIEmbeddings # Or any other Embedding class\nfrom langchain_core.documents import Document\n\n# Set up Elasticsearch client URL and OpenAI API Key\nELASTICSEARCH_URL = os.environ.get(\"ELASTICSEARCH_URL\", \"http://localhost:9200\")\nOPENAI_API_KEY = os.environ.get(\"OPENAI_API_KEY\", \"\")\n\nif not OPENAI_API_KEY:\n    print(\"Warning: OPENAI_API_KEY not set. Using a placeholder for demonstration.\")\n    # In a real application, you would ensure this is set or use a mock.\n    embeddings = None # Prevent actual API calls\nelse:\n    embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)\n\nif embeddings:\n    # Initialize ElasticsearchStore\n    # Ensure Elasticsearch is running and accessible at ELASTICSEARCH_URL\n    vectorstore = ElasticsearchStore(\n        es_url=ELASTICSEARCH_URL,\n        index_name=\"langchain-test-index\",\n        embedding=embeddings,\n        # num_dimensions is crucial for correct vector mapping\n        num_dimensions=1536 # For OpenAI embeddings\n    )\n\n    # Add documents\n    docs = [\n        Document(page_content=\"The quick brown fox jumps over the lazy dog\", metadata={\"source\": \"lorem\"}),\n        Document(page_content=\"A dog barks at the moon\", metadata={\"source\": \"nature\"}),\n    ]\n    vectorstore.add_documents(docs)\n\n    # Perform a similarity search\n    query = \"What is a fox?\"\n    results = vectorstore.similarity_search(query, k=1)\n\n    print(f\"\\nSimilarity search results for '{query}':\")\n    for doc in results:\n        print(f\"- Content: {doc.page_content}, Metadata: {doc.metadata}\")\nelse:\n    print(\"Embeddings not initialized due to missing API key. Skipping vector store example.\")\n","lang":"python","description":"This quickstart demonstrates how to initialize `ElasticsearchStore` with OpenAI embeddings, add documents, and perform a similarity search. Ensure you have Elasticsearch running and the `ELASTICSEARCH_URL` and `OPENAI_API_KEY` environment variables set. Install `langchain-openai` for OpenAI embeddings."},"warnings":[{"fix":"Update your imports from `from langchain.vectorstores.elasticsearch import ElasticsearchStore` to `from langchain_elasticsearch import ElasticsearchStore` (and similar for other components).","message":"The Elasticsearch integration was extracted from the main `langchain` package into `langchain-elasticsearch`. This changes import paths.","severity":"breaking","affected_versions":"langchain<0.1.0 to langchain-elasticsearch>=0.1.0"},{"fix":"Initialize `ElasticsearchStore(..., num_dimensions=YOUR_EMBEDDING_DIMENSIONS)`.","message":"When creating a new vector index in Elasticsearch, it is highly recommended to explicitly provide the `num_dimensions` parameter for your embeddings to `ElasticsearchStore` to ensure correct vector field mapping.","severity":"gotcha","affected_versions":">=0.4.0"},{"fix":"Upgrade `langchain-elasticsearch` to version `0.3.1` or higher to use asynchronous functionalities.","message":"Asynchronous methods (e.g., `aadd_documents`, `asimilarity_search`) were introduced in version `0.3.1`. Attempting to use them on earlier versions will result in an `AttributeError`.","severity":"gotcha","affected_versions":"<0.3.1"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install `langchain-elasticsearch` and update your import to `from langchain_elasticsearch import ElasticsearchStore`.","cause":"You are trying to import the Elasticsearch vectorstore from the old `langchain` path, but the integration has moved to its own package.","error":"ModuleNotFoundError: No module named 'langchain.vectorstores.elasticsearch'"},{"fix":"Ensure your Elasticsearch instance is running and accessible from where your Python code is executed. Verify `ELASTICSEARCH_URL` is correct (e.g., `http://localhost:9200`).","cause":"The Elasticsearch client cannot connect to the specified URL, likely because Elasticsearch is not running, is on a different port, or behind a firewall.","error":"elasticsearch.exceptions.ConnectionError: Connection refused"},{"fix":"Ensure your embedding model is correctly instantiated and provides valid embeddings. If creating a new index, specify `num_dimensions` in `ElasticsearchStore` initialization.","cause":"This often occurs during vector search if embeddings are not properly initialized or `num_dimensions` was not set, leading to issues with vector processing in Elasticsearch.","error":"TypeError: 'NoneType' object is not subscriptable"}]}