Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH]: Support for $in and $nin metadata filters #1151

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions clients/js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ export type IDs = ID[];

export type PositiveInteger = number;

type LiteralValue = string | number;
type LiteralValue = string | number | boolean;
type ListLiteralValue = LiteralValue[];
type LiteralNumber = number;
type LogicalOperator = "$and" | "$or";
type InclusionOperator = "$in" | "$nin";
type WhereOperator = "$gt" | "$gte" | "$lt" | "$lte" | "$ne" | "$eq";

type OperatorExpression = {
[key in WhereOperator | LogicalOperator]?: LiteralValue | LiteralNumber;
[key in WhereOperator | InclusionOperator | LogicalOperator ]?: LiteralValue | ListLiteralValue;
};

type BaseWhere = {
Expand Down Expand Up @@ -77,4 +79,4 @@ export type CollectionMetadata = Record<string, unknown>;
// see all options here: https://www.jsdocs.io/package/@types/node-fetch#RequestInit
export type ConfigOptions = {
options?: RequestInit;
};
};
68 changes: 68 additions & 0 deletions clients/js/test/query.collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,71 @@ test("it should query a collection with text", async () => {
expect.arrayContaining(results.documents[0])
);
})


test("it should query a collection with text and where", async () => {
await chroma.reset();
let embeddingFunction = new TestEmbeddingFunction();
const collection = await chroma.createCollection({ name: "test", embeddingFunction: embeddingFunction });
await collection.add({ ids: IDS, embeddings: EMBEDDINGS, metadatas: METADATAS, documents: DOCUMENTS });

const results = await collection.query({
queryTexts: ["test"],
nResults: 3,
where: { "float_value" : 2 }
});

expect(results).toBeDefined();
expect(results).toBeInstanceOf(Object);
expect(results.ids.length).toBe(1);
expect(["test3"]).toEqual(expect.arrayContaining(results.ids[0]));
expect(["test2"]).not.toEqual(expect.arrayContaining(results.ids[0]));
expect(["This is a third test"]).toEqual(
expect.arrayContaining(results.documents[0])
);
})


test("it should query a collection with text and where in", async () => {
await chroma.reset();
let embeddingFunction = new TestEmbeddingFunction();
const collection = await chroma.createCollection({ name: "test", embeddingFunction: embeddingFunction });
await collection.add({ ids: IDS, embeddings: EMBEDDINGS, metadatas: METADATAS, documents: DOCUMENTS });

const results = await collection.query({
queryTexts: ["test"],
nResults: 3,
where: { "float_value" : { '$in': [2,5,10] }}
});

expect(results).toBeDefined();
expect(results).toBeInstanceOf(Object);
expect(results.ids.length).toBe(1);
expect(["test3"]).toEqual(expect.arrayContaining(results.ids[0]));
expect(["test2"]).not.toEqual(expect.arrayContaining(results.ids[0]));
expect(["This is a third test"]).toEqual(
expect.arrayContaining(results.documents[0])
);
})

test("it should query a collection with text and where nin", async () => {
await chroma.reset();
let embeddingFunction = new TestEmbeddingFunction();
const collection = await chroma.createCollection({ name: "test", embeddingFunction: embeddingFunction });
await collection.add({ ids: IDS, embeddings: EMBEDDINGS, metadatas: METADATAS, documents: DOCUMENTS });

const results = await collection.query({
queryTexts: ["test"],
nResults: 3,
where: { "float_value" : { '$nin': [-2,0] }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this says float, but they are ints?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I picked up the examples from clients/js/test/data.ts

const METADATAS = [
  { test: "test1", float_value: -2 },
  { test: "test2", float_value: 0 },
  { test: "test3", float_value: 2 },
];

});

expect(results).toBeDefined();
expect(results).toBeInstanceOf(Object);
expect(results.ids.length).toBe(1);
expect(["test3"]).toEqual(expect.arrayContaining(results.ids[0]));
expect(["test2"]).not.toEqual(expect.arrayContaining(results.ids[0]));
expect(["This is a third test"]).toEqual(
expect.arrayContaining(results.documents[0])
);
})