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

TypeError: Cannot read properties of undefined (reading 'nq') #404

Open
iGmainC opened this issue Dec 27, 2024 · 4 comments
Open

TypeError: Cannot read properties of undefined (reading 'nq') #404

iGmainC opened this issue Dec 27, 2024 · 4 comments

Comments

@iGmainC
Copy link

iGmainC commented Dec 27, 2024

Describe the bug:
TypeError: Cannot read properties of undefined (reading 'nq')

Steps to reproduce:

  1. init empty nodejs project
  2. pnpm i -D tsxpnpm i @zilliz/milvus2-sdk-node
  3. create main.ts:
import { DataType, MilvusClient } from "@zilliz/milvus2-sdk-node";

let client = new MilvusClient({
    address: "localhost:19530",
    username: "root",
    password: "123456789",
});

const collection_name = "testTable1";

async function initMilvus() {
    await client.useDatabase({ db_name: "vector_db" });
    let r = await client.createCollection({
        collection_name,
        description: '',
        enableDynamicField: true, // 是否启用动态字段
        fields: [
            {
                name: 'id',
                data_type: DataType.Int64,
                is_primary_key: true,
                autoID: true,
            },
            { name: 'embedding', data_type: DataType.FloatVector, dim: 1024 },
            {
                name: 'chunk',
                data_type: DataType.VarChar,
                max_length: 2048,
                enable_match: true,
                enable_analyzer: true,
                analyzer_params: { type: "chinese" }
            },
        ],
        index_params: [
            {
                field_name: 'embedding',
                index_name: 'embedding_HNSW',
                index_type: 'HNSW',
                metric_type: 'IP',
                params: { efConstruction: 32, M: 64 },
            },
        ],
    });
    console.log(r);
    return r;
}

async function main() {
    await initMilvus()
    await client.useDatabase({ db_name: "vector_db" });
    let r = await client.search({
        collection_name,
        data: ['工作地点在公司内部'],
        anns_field: 'chunk',
        limit: 3,
        params: { 'drop_ratio_search': 0.2 }
    })
    console.log(r);

}

(async () => {
    await main();
})();
  1. tsx main.ts
➜  milvus-demo pnpm start

> [email protected] start /home/xxx/milvus-demo
> tsx src/main.ts

{
  extra_info: {},
  error_code: 'Success',
  reason: '',
  code: 0,
  retriable: false,
  detail: ''
}
/home/xxx/milvus-demo/node_modules/.pnpm/@[email protected]/node_modules/@zilliz/milvus2-sdk-node/dist/milvus/utils/Format.js:705
        nq: requests[0].nq,
                        ^

TypeError: Cannot read properties of undefined (reading 'nq')
    at buildSearchRequest (/home/xxx/milvus-demo/node_modules/.pnpm/@[email protected]/node_modules/@zilliz/milvus2-sdk-node/milvus/utils/Format.ts:909:21)
    at MilvusClient.<anonymous> (/home/xxx/milvus-demo/node_modules/.pnpm/@[email protected]/node_modules/@zilliz/milvus2-sdk-node/milvus/grpc/Data.ts:506:78)
    at Generator.next (<anonymous>)
    at fulfilled (/home/xxx/milvus-demo/node_modules/.pnpm/@[email protected]/node_modules/@zilliz/milvus2-sdk-node/dist/milvus/grpc/Data.js:5:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

Node.js v22.12.0
 ELIFECYCLE  Command failed with exit code 1.

Milvus-node-sdk version: 2.5.3

Milvus version: 2.5.1

这看起来是sdk的内部错误

@shanghaikid
Copy link
Contributor

shanghaikid commented Dec 27, 2024

you need to define functions to enable full text search.

  1. add an sparse field in your schema
{
    name: "sparse",
    data_type: DataType.SparseFloatVector,
 }
  1. add functions in your create collection params
const functions = [
    {
      name: 'text_bm25_emb',
      description: 'bm25 function',
      type: FunctionType.BM25,
      input_field_names: ['chunk'],
      output_field_names: ['sparse'],
      params: {},
    },
]

client. createCollection({
...otherparams,
functions
})
  1. update index using bm25
{
                field_name: 'sparse',
                index_name: 'sparse',
                index_type: 'AUTOINDEX',
                metric_type: 'BM25',
}
  1. the anns field name should be the spare field
let r = await client.search({
        collection_name,
        data: ['工作地点在公司内部'],
        anns_field: 'sparse',
        limit: 3,
        params: { 'drop_ratio_search': 0.2 }
    })

@shanghaikid
Copy link
Contributor

I will optimize the error handling in the next version, thanks.

@shanghaikid
Copy link
Contributor

or you can use attu to create the collection and index for you

https://github.com/zilliztech/attu

@iGmainC
Copy link
Author

iGmainC commented Dec 27, 2024

you need to define functions to enable full text search.

1. add an sparse field in your schema
{
    name: "sparse",
    data_type: DataType.SparseFloatVector,
 }
2. add functions in your create collection params
const functions = [
    {
      name: 'text_bm25_emb',
      description: 'bm25 function',
      type: FunctionType.BM25,
      input_field_names: ['chunk'],
      output_field_names: ['sparse'],
      params: {},
    },
]

client. createCollection({
...otherparams,
functions
})
3. update index using bm25
{
                field_name: 'sparse',
                index_name: 'sparse',
                index_type: 'AUTOINDEX',
                metric_type: 'BM25',
}
4. the anns field name should be the spare field
let r = await client.search({
        collection_name,
        data: ['工作地点在公司内部'],
        anns_field: 'sparse',
        limit: 3,
        params: { 'drop_ratio_search': 0.2 }
    })

This solved my problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants