Skip to content

Commit 2d59bda

Browse files
committed
tests
1 parent c28160b commit 2d59bda

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

libs/langchain-community/src/vectorstores/elasticsearch.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ export interface HybridRetrievalStrategyConfig {
3333
rankConstant?: number;
3434
textField?: string;
3535
/**
36-
* For Elasticsearch 9.2, set to `false` to include vectors in responses.
36+
* Include source vectors in search responses.
37+
*
38+
* Elasticsearch 9.2+ excludes vectors from `_source` by default.
39+
* Set to `true` to include vectors in responses for ES 9.2+.
40+
*
41+
* Note: ES < 8.19 does not support this parameter.
3742
*/
38-
excludeSourceVectors?: boolean;
43+
includeSourceVectors?: boolean;
3944
}
4045

4146
/**
@@ -45,13 +50,13 @@ export class HybridRetrievalStrategy {
4550
public readonly rankWindowSize: number;
4651
public readonly rankConstant: number;
4752
public readonly textField: string;
48-
public readonly excludeSourceVectors?: boolean;
53+
public readonly includeSourceVectors?: boolean;
4954

5055
constructor(config: HybridRetrievalStrategyConfig = {}) {
5156
this.rankWindowSize = config.rankWindowSize ?? 100;
5257
this.rankConstant = config.rankConstant ?? 60;
5358
this.textField = config.textField ?? "text";
54-
this.excludeSourceVectors = config.excludeSourceVectors;
59+
this.includeSourceVectors = config.includeSourceVectors;
5560
}
5661
}
5762

@@ -239,6 +244,9 @@ export class ElasticVectorSearch extends VectorStore {
239244
k,
240245
num_candidates: this.candidates,
241246
},
247+
...(this.strategy?.includeSourceVectors === true && {
248+
_source: { includes: ["*"] },
249+
}),
242250
});
243251

244252
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -291,6 +299,9 @@ export class ElasticVectorSearch extends VectorStore {
291299
},
292300
},
293301
...(filterClauses && { query: filterClauses }),
302+
...(this.strategy?.includeSourceVectors === true && {
303+
_source: { includes: ["*"] },
304+
}),
294305
});
295306

296307
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -427,12 +438,6 @@ export class ElasticVectorSearch extends VectorStore {
427438
},
428439
};
429440

430-
if (this.strategy?.excludeSourceVectors !== undefined) {
431-
request.settings = {
432-
"index.mapping.exclude_source_vectors": this.strategy.excludeSourceVectors,
433-
};
434-
}
435-
436441
const indexExists = await this.doesIndexExist();
437442
if (indexExists) return;
438443

libs/langchain-community/src/vectorstores/tests/elasticsearch.int.test.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -423,37 +423,30 @@ describe("ElasticVectorSearch - ES 9.x Compatibility", () => {
423423
embeddings = new OpenAIEmbeddings();
424424
});
425425

426-
test.skip("Hybrid search with excludeSourceVectors set to false for ES 9.x", async () => {
427-
const indexName = "test_es9_exclude_false";
426+
test.skip("Hybrid search with includeSourceVectors set to true for ES 9.2+", async () => {
427+
const indexName = "test_es9_include_vectors";
428428
const store = new ElasticVectorSearch(embeddings, {
429429
client,
430430
indexName,
431431
strategy: new HybridRetrievalStrategy({
432-
excludeSourceVectors: false,
432+
includeSourceVectors: true,
433433
}),
434434
});
435435
await store.deleteIfExists();
436436

437437
await store.addDocuments([
438-
new Document({ pageContent: "Document for ES 9.x testing" }),
438+
new Document({ pageContent: "Document for ES 9.2+ testing" }),
439439
new Document({ pageContent: "Another document for compatibility" }),
440440
]);
441441

442442
const results = await store.similaritySearch("testing", 2);
443443

444444
expect(results).toHaveLength(2);
445445
expect(results[0]).toBeInstanceOf(Document);
446-
447-
const indexSettings = await client.indices.getSettings({
448-
index: indexName,
449-
});
450-
expect(
451-
indexSettings[indexName].settings?.index?.mapping?.exclude_source_vectors
452-
).toBe("false");
453446
});
454447

455-
test.skip("Hybrid search with excludeSourceVectors undefined uses ES defaults", async () => {
456-
const indexName = "test_es_default_exclude";
448+
test.skip("Hybrid search with includeSourceVectors undefined uses ES defaults", async () => {
449+
const indexName = "test_es_default_include";
457450
const store = new ElasticVectorSearch(embeddings, {
458451
client,
459452
indexName,
@@ -471,13 +464,13 @@ describe("ElasticVectorSearch - ES 9.x Compatibility", () => {
471464
expect(results[0]).toBeInstanceOf(Document);
472465
});
473466

474-
test.skip("Pure vector search with excludeSourceVectors for ES 9.x", async () => {
467+
test.skip("Pure vector search with includeSourceVectors for ES 9.2+", async () => {
475468
const indexName = "test_es9_pure_vector";
476469
const store = new ElasticVectorSearch(embeddings, {
477470
client,
478471
indexName,
479472
strategy: new HybridRetrievalStrategy({
480-
excludeSourceVectors: false,
473+
includeSourceVectors: true,
481474
}),
482475
});
483476
await store.deleteIfExists();

0 commit comments

Comments
 (0)