Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/oss/javascript/integrations/vectorstores/elasticsearch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,62 @@ const clientArgs: ElasticClientArgs = {
const vectorStore = new ElasticVectorSearch(embeddings, clientArgs);
```

## Hybrid search

<Tip>
Hybrid search requires Elasticsearch 8.9+ for RRF (Reciprocal Rank Fusion) support.
</Tip>

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+
}),
});
```

<Note>
The `includeSourceVectors` parameter is not supported on Elasticsearch versions earlier than 8.19.
</Note>

## Manage vector store

### Add items to vector store
Expand Down