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

feat: generate expr by pk type when delete entities by ids #242

Conversation

yuyicai
Copy link
Contributor

@yuyicai yuyicai commented Sep 14, 2023

The id on query and search result always is string type, even if the pk field type is int64.
So, we should generate the expr by the type of pk field when delete data by ids.
image

example:
index.ts

import { MilvusClient, InsertReq, DataType } from '@zilliz/milvus2-sdk-node';

const COLLECTION_NAME = 'data_query_example';

(async () => {
  // build client
  const milvusClient = new MilvusClient({
    address: 'localhost:19530',
    username: 'username',
    password: 'Aa12345!!',
  });

  console.log('Node client is initialized.');
  // create collection
  const create = await milvusClient.createCollection({
    collection_name: COLLECTION_NAME,
    fields: [
      {
        name: 'id',
        description: 'ID field',
        data_type: DataType.Int64,
        is_primary_key: true,
      },
      {
        name: 'vector',
        description: 'Vector field',
        data_type: DataType.FloatVector,
        dim: 8,
      },
      { name: 'height', description: 'int64 field', data_type: DataType.Int64 },
      {
        name: 'name',
        description: 'VarChar field',
        data_type: DataType.VarChar,
        max_length: 128,
      },
    ],
  });
  console.log('Create collection is finished.', create);

  // build example data
  const vectorsData = [
    {
      id: 1,
      vector: [
        0.11878310581111173, 0.9694947902934701, 0.16443679307243175,
        0.5484226189097237, 0.9839246709011924, 0.5178387104937776,
        0.8716926129208069, 0.5616972243831446,
      ],
      height: 20405,
      name: 'zlnmh',
    },
    {
      id: 2,
      vector: [
        0.9992090731236536, 0.8248790611809487, 0.8660083940881405,
        0.09946359318481224, 0.6790698063908669, 0.5013786801063624,
        0.795311915725105, 0.9183033261617566,
      ],
      height: 93773,
      name: '5lr9y',
    },
    {
      id: 3,
      vector: [
        0.8761291569818763, 0.07127366044153227, 0.775648976160332,
        0.5619757601304878, 0.6076543120476996, 0.8373907516027586,
        0.8556140171597648, 0.4043893119391049,
      ],
      height: 85122,
      name: 'nes0j',
    }
  ];
  const params: InsertReq = {
    collection_name: COLLECTION_NAME,
    fields_data: vectorsData,
  };
  // insert data into collection
  await milvusClient.insert(params);
  console.log('Data is inserted.');

  // create index
  const createIndex = await milvusClient.createIndex({
    collection_name: COLLECTION_NAME,
    field_name: 'vector',
    metric_type: 'L2',
  });

  console.log('Index is created', createIndex);

  // need load collection before search
  const load = await milvusClient.loadCollectionSync({
    collection_name: COLLECTION_NAME,
  });
  console.log('Collection is loaded.', load);

  // do the query
  console.time('Query time');
  const query = await milvusClient.query({
    collection_name: COLLECTION_NAME,
    filter: 'height > 31400',
    output_fields: ['id', 'height'],
    limit: 100,
  });
  console.timeEnd('Query time');
  console.log('query result', query);

  // get ids for query result
  // and type of id, the id always is string type
  const ids = query.data.map((item: any) => {
    console.log(item, "id type:", typeof item.id);
    return item.id;
  });
  
  // delete data by ids that query result returned
  const deleteRet = await milvusClient.delete({
    collection_name: COLLECTION_NAME,
    ids,
  })
  console.log('delete result', deleteRet);

  // drop collection
  await milvusClient.dropCollection({
    collection_name: COLLECTION_NAME,
  });
})();

Signed-off-by: Yicai Yu <yuyicai@hotmail.com>
@codecov
Copy link

codecov bot commented Sep 14, 2023

Codecov Report

Merging #242 (0f7351c) into main (e614e8d) will decrease coverage by 0.03%.
The diff coverage is 93.75%.

❗ Current head 0f7351c differs from pull request most recent head 2faf56b. Consider uploading reports for the commit 2faf56b to get more accurate results

@@            Coverage Diff             @@
##             main     #242      +/-   ##
==========================================
- Coverage   95.76%   95.74%   -0.03%     
==========================================
  Files          32       32              
  Lines        1631     1645      +14     
  Branches      425      429       +4     
==========================================
+ Hits         1562     1575      +13     
- Misses         66       67       +1     
  Partials        3        3              
Files Changed Coverage Δ
milvus/grpc/Data.ts 96.40% <85.71%> (-0.34%) ⬇️
milvus/grpc/Collection.ts 98.78% <100.00%> (+0.06%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@sre-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: shanghaikid, yuyicai

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@shanghaikid shanghaikid merged commit 500b899 into milvus-io:main Sep 14, 2023
2 of 3 checks passed
@yuyicai yuyicai deleted the generate-expr-by-pk-type-when-delete-entities-by-ids branch September 14, 2023 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants