Skip to content

Commit c28160b

Browse files
committed
tests
1 parent 3950bb6 commit c28160b

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export interface HybridRetrievalStrategyConfig {
3333
rankConstant?: number;
3434
textField?: string;
3535
/**
36-
* For Elasticsearch 9.x, set to `false` to include vectors in responses.
36+
* For Elasticsearch 9.2, set to `false` to include vectors in responses.
3737
*/
3838
excludeSourceVectors?: boolean;
3939
}

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,101 @@ describe("ElasticVectorSearch - Hybrid Search", () => {
396396
});
397397
});
398398
});
399+
400+
describe("ElasticVectorSearch - ES 9.x Compatibility", () => {
401+
let client: Client;
402+
let embeddings: OpenAIEmbeddings;
403+
404+
beforeEach(() => {
405+
if (!process.env.ELASTIC_URL) {
406+
throw new Error("ELASTIC_URL not set");
407+
}
408+
409+
const config: ClientOptions = {
410+
node: process.env.ELASTIC_URL,
411+
};
412+
if (process.env.ELASTIC_API_KEY) {
413+
config.auth = {
414+
apiKey: process.env.ELASTIC_API_KEY,
415+
};
416+
} else if (process.env.ELASTIC_USERNAME && process.env.ELASTIC_PASSWORD) {
417+
config.auth = {
418+
username: process.env.ELASTIC_USERNAME,
419+
password: process.env.ELASTIC_PASSWORD,
420+
};
421+
}
422+
client = new Client(config);
423+
embeddings = new OpenAIEmbeddings();
424+
});
425+
426+
test.skip("Hybrid search with excludeSourceVectors set to false for ES 9.x", async () => {
427+
const indexName = "test_es9_exclude_false";
428+
const store = new ElasticVectorSearch(embeddings, {
429+
client,
430+
indexName,
431+
strategy: new HybridRetrievalStrategy({
432+
excludeSourceVectors: false,
433+
}),
434+
});
435+
await store.deleteIfExists();
436+
437+
await store.addDocuments([
438+
new Document({ pageContent: "Document for ES 9.x testing" }),
439+
new Document({ pageContent: "Another document for compatibility" }),
440+
]);
441+
442+
const results = await store.similaritySearch("testing", 2);
443+
444+
expect(results).toHaveLength(2);
445+
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");
453+
});
454+
455+
test.skip("Hybrid search with excludeSourceVectors undefined uses ES defaults", async () => {
456+
const indexName = "test_es_default_exclude";
457+
const store = new ElasticVectorSearch(embeddings, {
458+
client,
459+
indexName,
460+
strategy: new HybridRetrievalStrategy(),
461+
});
462+
await store.deleteIfExists();
463+
464+
await store.addDocuments([
465+
new Document({ pageContent: "Test with default settings" }),
466+
]);
467+
468+
const results = await store.similaritySearch("test", 1);
469+
470+
expect(results).toHaveLength(1);
471+
expect(results[0]).toBeInstanceOf(Document);
472+
});
473+
474+
test.skip("Pure vector search with excludeSourceVectors for ES 9.x", async () => {
475+
const indexName = "test_es9_pure_vector";
476+
const store = new ElasticVectorSearch(embeddings, {
477+
client,
478+
indexName,
479+
strategy: new HybridRetrievalStrategy({
480+
excludeSourceVectors: false,
481+
}),
482+
});
483+
await store.deleteIfExists();
484+
485+
await store.addDocuments([
486+
new Document({ pageContent: "ES 9.x pure vector test" }),
487+
]);
488+
489+
const queryVector = await embeddings.embedQuery("vector test");
490+
const results = await store.similaritySearchVectorWithScore(queryVector, 1);
491+
492+
expect(results).toHaveLength(1);
493+
expect(results[0][0]).toBeInstanceOf(Document);
494+
expect(typeof results[0][1]).toBe("number");
495+
});
496+
});

0 commit comments

Comments
 (0)