How to filter metadata with OpenSearch similaritySearch #5478
Replies: 4 comments 2 replies
-
To filter documents by filename in your similarity search with LangChain and OpenSearch, you need to use the // Define the filename you want to filter by
const filenameFilter = "example_filename.txt";
// Construct the filter object
const filter = {
filename: filenameFilter // Ensure this key matches the actual metadata field name for filenames
};
// Use the filter in the similaritySearchVectorWithScore method
const queryVector = [/* your query vector here */];
const k = 10; // Number of similar documents to return
const filteredResults = await vectorStore.similaritySearchVectorWithScore(queryVector, k, filter); Make sure It's important to note that the filtering capabilities have specific requirements regarding the metadata structure and types, and there are limitations on filtering based on array elements or complex data types. The filtering mechanism uses JSON serialization for comparison, so ensure your filter values are compatible. Additionally, consider the performance implications of filtering on deeply nested or large JSON objects. If you encounter any issues or have further questions, feel free to ask for more assistance.
|
Beta Was this translation helpful? Give feedback.
-
@dosu yes, I understand there is a similaritySearchVectorWithScore, but I was looking for the similaritySearch only |
Beta Was this translation helpful? Give feedback.
-
@dosu This does not work for me. If I add documents and then filter with the code below, I get no documents back. What should the filter format be?
|
Beta Was this translation helpful? Give feedback.
-
Any update on this ? |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
For example, I want to filter/pre-filter documents with similaritySearch based on filename. This is the way of filter in OpenSearch "term": {
"metadata.source.keyword":
"wikipedia.txt",
}. But it seems like Langchain ignore this or I'm doing it wrong. Please help me.
System Info
Node.js v18.16.0
yarn version v1.22.19
Beta Was this translation helpful? Give feedback.
All reactions