vector-js icon indicating copy to clipboard operation
vector-js copied to clipboard

QueryMany type error

Open as-celegen opened this issue 1 year ago • 5 comments

If single query list is given to queryMany like index.queryMany([{vector: [1, 0, 0], topK: 6}]), return value is one dimensional list just as normal query function. Return type of queryMany is defined as two dimensional list.

as-celegen avatar Jun 29 '24 15:06 as-celegen

Hello there, this is not an error maybe a misunderstading. Suppose you have something like this:

const res = await index.queryMany<{
      animal: string;
      tags: string[];
      diet: string;
    }>([
      {
        vector: initialData[0].vector,
        topK: 2,
        filter: "tags[0] = 'mammal' AND diet = 'herbivore'",
        includeMetadata: true,
      },
      {
        vector: initialData[1].vector,
        topK: 1,
        filter: "tags[0] = 'mammal' AND diet = 'carnivore'",
        includeMetadata: true,
      },
    ]);

When you say topK=2 and topK=1, you will get this:

[
      [
        {
          id: `id1`,
          score: 1,
          metadata: { animal: "elephant", tags: ["mammal"], diet: "herbivore" },
        },
        {
          id: `id2`,
          score: 1,
          metadata: { animal: "elephant", tags: ["mammal"], diet: "herbivore" },
        },
      ],
      [
        {
          id: `id2-1`,
          score: 1,
          metadata: { animal: "tiger", tags: ["mammal"], diet: "carnivore" },
        },
      ],
    ]

Your inner array has to match topK thats why we return list of lists.

ogzhanolguncu avatar Jul 05 '24 12:07 ogzhanolguncu

Sorry about initial phrasing, i meant if a list of queries with only one query is given to queryMany, response is same as if i send that query with query function. Example:

const res = await index.queryMany<
{ animal: string; tags: string[]; diet: string; }
>([
 { 
vector: initialData[0].vector, 
topK: 2,
 filter: "tags[0] = 'mammal' AND diet = 'herbivore'"
, includeMetadata: true,
 }, ]);

Response: [ { id: id1, score: 1, metadata: { animal: "elephant", tags: ["mammal"], diet: "herbivore" }, }, { id: id2, score: 1, metadata: { animal: "elephant", tags: ["mammal"], diet: "herbivore" }, }, ]

as-celegen avatar Jul 05 '24 14:07 as-celegen

In this case, what you want to do is use query instead of queryMany. To prevent this misunderstanding, I think we should improve the types a little bit. I agree.

ogzhanolguncu avatar Jul 05 '24 14:07 ogzhanolguncu

+1, the return type is wrong when there's only one entry in queryMany().

gongruya avatar Mar 03 '25 02:03 gongruya

how to fix it?

liangxiwei avatar Aug 14 '25 15:08 liangxiwei