Open stevedaskam opened 1 month ago
I found a similar unsolved discussion and a related closed issue that might help you:
The issue might be due to the way the metadata filtering is being applied in the similarity search. Ensure that you are using the correct field name and match criteria in your searchOptions
. Here is an example:
const exactValueResult = await store.similaritySearch(query, 4, {
fields: ["metadata.user"],
searchOptions: {
query: { field: "metadata.user", match: "U12345-1234" },
},
});
Additionally, verify that the metadata is correctly indexed and stored in the vector store. If the metadata is not properly indexed, the search might not be able to filter based on the specified criteria.
Make sure your OpenSearch instance is correctly set up and running. You can use the official Docker image to get started with OpenSearch. Here is an example of how to set up and query documents in OpenSearch:
import { Client } from "@opensearch-project/opensearch";
import { Document } from "langchain/document";
import { OpenAIEmbeddings } from "@langchain/openai";
import { OpenSearchVectorStore } from "langchain/vectorstores/opensearch";
const client = new Client({
nodes: [process.env.OPENSEARCH_URL ?? "http://127.0.0.1:9200"],
});
const docs = [
new Document({
metadata: { user: "U12345-1234" },
pageContent: "Amazon Simple Storage Service (Amazon S3) is a cloud storage service that allows users to store and retrieve data of any amount from anywhere.",
}),
new Document({
metadata: { user: "U12345-5678" },
pageContent: "Amazon Elastic Compute Cloud (Amazon EC2) provides on-demand, scalable computing capacity in the Amazon Web Services (AWS) Cloud.",
}),
];
await OpenSearchVectorStore.fromDocuments(docs, new OpenAIEmbeddings(), {
client,
indexName: 'my-index',
});
To query the documents:
const results = await vectorStore.similaritySearch(query, k, {
fields: ["metadata.user"],
searchOptions: {
query: { field: "metadata.user", match: "U12345-1234" },
},
});
console.log(JSON.stringify(results, null, 2));
Make sure your OpenSearch instance is running and properly configured to handle the metadata filtering [3][4][5][6].
To continue talking to Dosu, mention @dosu.
@dosu When i run this, i still don't get any documents back
The issue might be due to the structure or content of the OpenSearch index. The similaritySearch
method with metadata filtering requires a well-structured OpenSearch index where documents are enriched with both text and metadata properties. If the index is not properly structured or the documents do not contain the expected metadata properties, the search may not return any documents.
Here is a sample code snippet that demonstrates how to use the similaritySearch
method with metadata filtering:
import { Client } from "@opensearch-project/opensearch";
import { OpenAIEmbeddings } from "@langchain/openai";
import { OpenSearchVectorStore } from "@langchain/community/vectorstores/opensearch";
const client = new Client({
nodes: ["http://localhost:9200"], // URL for the OpenSearch instance
});
const indexName = "test_index";
const embeddings = new OpenAIEmbeddings();
const store = new OpenSearchVectorStore(embeddings, { client, indexName });
await store.deleteIfExists();
await store.addDocuments([
{ pageContent: "hello", metadata: { a: 2 } },
{ pageContent: "car", metadata: { a: 1 } },
{ pageContent: "adjective", metadata: { a: 1 } },
{ pageContent: "hi", metadata: { a: 1 } },
]);
const filter = { a: 1 };
const results = await store.similaritySearch("hello!", 1, { filter });
console.log(results);
/*
[ Document { pageContent: 'car', metadata: { a: 1 } } ]
*/
await store.close();
Ensure that:
If these conditions are met and the issue persists, further investigation into the index structure and content may be necessary [1][2].
To continue talking to Dosu, mention @dosu.
@dosu The above code does not work as "term" queries no longer seem to return results from OpenSearch for metadata fields. Is there any way to bypass this and pass in my own filter query?
Checked other resources
Example Code