Skip to content
Snippets Groups Projects
Unverified Commit ffb195ea authored by shodevacc's avatar shodevacc Committed by GitHub
Browse files

Fix: Metadata filters doesn't seem to work for Qdrant (#623)

parent b4677534
No related branches found
No related tags found
No related merge requests found
# Qdrant Vector Store Example
How to run `examples/qdrantdb/preFilters.ts`:
Add your OpenAI API Key into a file called `.env` in the parent folder of this directory. It should look like this:
```
OPEN_API_KEY=sk-you-key
```
Now, open a new terminal window and inside `examples`, run `npx ts-node qdrantdb/preFilters.ts`.
import * as dotenv from "dotenv";
import {
CallbackManager,
Document,
MetadataMode,
QdrantVectorStore,
VectorStoreIndex,
serviceContextFromDefaults,
storageContextFromDefaults,
} from "llamaindex";
// Load environment variables from local .env file
dotenv.config();
const collectionName = "dog_colors";
const qdrantUrl = "http://127.0.0.1:6333";
async function main() {
try {
const docs = [
new Document({
text: "The dog is brown",
metadata: {
dogId: "1",
},
}),
new Document({
text: "The dog is red",
metadata: {
dogId: "2",
},
}),
];
console.log("Creating QdrantDB vector store");
const qdrantVs = new QdrantVectorStore({ url: qdrantUrl, collectionName });
const ctx = await storageContextFromDefaults({ vectorStore: qdrantVs });
console.log("Embedding documents and adding to index");
const index = await VectorStoreIndex.fromDocuments(docs, {
storageContext: ctx,
serviceContext: serviceContextFromDefaults({
callbackManager: new CallbackManager({
onRetrieve: (data) => {
console.log(
"The retrieved nodes are:",
data.nodes.map((node) => node.node.getContent(MetadataMode.NONE)),
);
},
}),
}),
});
console.log(
"Querying index with no filters: Expected output: Brown probably",
);
const queryEngineNoFilters = index.asQueryEngine();
const noFilterResponse = await queryEngineNoFilters.query({
query: "What is the color of the dog?",
});
console.log("No filter response:", noFilterResponse.toString());
console.log("Querying index with dogId 2: Expected output: Red");
const queryEngineDogId2 = index.asQueryEngine({
preFilters: {
filters: [
{
key: "dogId",
value: "2",
filterType: "ExactMatch",
},
],
},
});
const response = await queryEngineDogId2.query({
query: "What is the color of the dog?",
});
console.log("Filter with dogId 2 response:", response.toString());
} catch (e) {
console.error(e);
}
}
main();
......@@ -289,7 +289,7 @@ export class QdrantVectorStore implements VectorStore {
* @param query The VectorStoreQuery to be used
*/
private async buildQueryFilter(query: VectorStoreQuery) {
if (!query.docIds && !query.queryStr) {
if (!query.docIds && !query.queryStr && !query.filters) {
return null;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment