From bb0fbb7427bce9fb02e6479ca4f1a56aeb7840ae Mon Sep 17 00:00:00 2001 From: margaretjgu Date: Mon, 17 Nov 2025 11:14:43 -0500 Subject: [PATCH] docs: Add hybrid search documentation for Elasticsearch vector store --- .../vectorstores/elasticsearch.mdx | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/oss/javascript/integrations/vectorstores/elasticsearch.mdx b/src/oss/javascript/integrations/vectorstores/elasticsearch.mdx index 9ccc4b8b85..578076ef78 100644 --- a/src/oss/javascript/integrations/vectorstores/elasticsearch.mdx +++ b/src/oss/javascript/integrations/vectorstores/elasticsearch.mdx @@ -108,6 +108,62 @@ const clientArgs: ElasticClientArgs = { const vectorStore = new ElasticVectorSearch(embeddings, clientArgs); ``` +## Hybrid search + + +Hybrid search requires Elasticsearch 8.9+ for RRF (Reciprocal Rank Fusion) support. + + +Hybrid search combines kNN vector search with BM25 full-text search using Reciprocal Rank Fusion (RRF) to improve search relevance. This is useful when you want to leverage both semantic similarity and keyword matching. + +To enable hybrid search, pass a `HybridRetrievalStrategy` to the constructor: + +```typescript +import { + ElasticVectorSearch, + HybridRetrievalStrategy, + type ElasticClientArgs, +} from "@langchain/community/vectorstores/elasticsearch"; + +const hybridVectorStore = new ElasticVectorSearch(embeddings, { + client: new Client(config), + indexName: "test_hybrid_search", + strategy: new HybridRetrievalStrategy({ + rankWindowSize: 100, // Number of documents to consider for RRF + rankConstant: 60, // RRF constant for score normalization + textField: "text", // Field to use for BM25 full-text search + }), +}); +``` + +Once configured, hybrid search is automatically used for all similarity searches: + +```typescript +// This now uses hybrid search (vector + BM25 + RRF) +const results = await hybridVectorStore.similaritySearch( + "how to prevent muscle soreness while running", + 5 +); +``` + +### Elasticsearch 9.2+ compatibility + +Elasticsearch 9.2+ excludes vectors from `_source` by default for performance. If you need vector data in search responses, set `includeSourceVectors`: + +```typescript +const hybridVectorStore = new ElasticVectorSearch(embeddings, { + client: new Client(config), + indexName: "test_hybrid_search", + strategy: new HybridRetrievalStrategy({ + includeSourceVectors: true, // Include vectors in responses for ES 9.2+ + }), +}); +``` + + +The `includeSourceVectors` parameter is not supported on Elasticsearch versions earlier than 8.19. + + ## Manage vector store ### Add items to vector store