Query

In addition to vectors, Milvus supports data types such as boolean, integers, floating-point numbers, and more.

A query is a search on all existing data. In Milvus, you can run a query which will return all the results that meet your specified requirements. Use boolean expression to specify the requirements.

Parameters marked with `*` are specific to Python SDK, and those marked with `**` are specific to Node.js SDK.
  1. Connect to the Milvus server:
>>> from pymilvus import connections
>>> connections.connect("default", host='localhost', port='19530')
import { MilvusClient } from "@zilliz/milvus2-sdk-node";
const milvusClient = new MilvusClient("localhost:19530");
Detailed Description
Parameter Description Note
alias* Alias for the Milvus server Data type: String
Mandatory
host* IP address of the Milvus server Mandatory
port* Port of the Milvus server Mandatory
address** Address of the Milvus server. "server_IP:server_port"
Mandatory
  1. Prepare collection parameters and create a collection:
>>> from pymilvus import Collection, FieldSchema, CollectionSchema, DataType
>>> collection_name = "test_collection_search"
>>> schema = CollectionSchema([
...     FieldSchema("film_id", DataType.INT64, is_primary=True),
...     FieldSchema("film_date", DataType.INT64),
...     FieldSchema("films", dtype=DataType.FLOAT_VECTOR, dim=2)
... ])
>>> collection = Collection(collection_name, schema, using='default', shards_num=2)
const COLLECTION_NAME = "example_collection";
const FIELD_NAME = "example_field";

const params = {
  collection_name: COLLECTION_NAME,
  fields: [
    {
      name: "films",
      description: "vector field",
      data_type: DataType.FloatVector,

      type_params: {
        dim: "8",
      },
    },
    {
      name: "film_id",
      data_type: DataType.Int64,
      autoID: false,
      is_primary_key: true,
      description: "",
    },
  ],
};

await milvusClient.collectionManager.createCollection(params);
Detailed Description
Parameter Description Note
collection_name Name of the collection to create Data type: String
field_name Name of the field in the collection Data type: String
Schema Schema used to create a collection and the fields within. Refer to field schema and collection schema for detailed description.  
description Description of the collection Data type: String
using* By specifying the srever alias here, you can decide in which Milvus server you create a collection. Optional
shards_num* Number of the shards for the collection to create Optional
  1. Insert random vectors to the newly created collection:
>>> import random
>>> data = [
...     [i for i in range(10)],
...     [1990 + i for i in range(10)],
...     [[random.random() for _ in range(2)] for _ in range(10)],
... ]
>>> collection.insert(data)
>>> collection.num_entities
10
let id = 1;
const entities = Array.from({ length: 10 }, () => ({
  films: Array.from({ length: 2 }, () => Math.random() * 10),
  film_id: id++,
}));

await milvusClient.dataManager.insert({{
  collection_name: COLLECTION_NAME,
  fields_data: entities,
});
Detailed Description
Parameter Description Note
data Data to insert into Milvus Mandatory
partition_name Name of the partition to insert data into Optional
timeout* Timeout (in seconds) to allow for RPC. Clients wait until server responds or error occurs when it is set to None. Optional
  1. Load the collection to memory and run a query:
>>> collection.load()
>>> expr = "film_id in [2,4,6,8]"
>>> output_fields = ["film_id", "film_date"]
>>> res = collection.query(expr, output_fields)
await milvusClient.collectionManager.loadCollection({
  collection_name: COLLECTION_NAME,
});

await milvusClient.dataManager.query({
  collection_name: COLLECTION_NAME,
  expr: "film_id in [2,4,6,8]",
  output_fields: ["film_id"],
});
Detailed Description
Parameter Description Note
collection_name** Name of the collection to load and query Mandatory
expr Boolean expression used to filter attribute Find more expression details in Boolean Expression Rules.
Optional
output_fields Name of the field to return (vector field not support in current release) Mandatory
  1. Check the returned results:
>>> sorted_res = sorted(res, key=lambda k: k['film_id'])
>>> sorted_res
[{'film_id': 2, 'film_date': 1992},
 {'film_id': 4, 'film_date': 1994},
 {'film_id': 6, 'film_date': 1996},
 {'film_id': 8, 'film_date': 1998}]
// query result
[{ film_id: "2" }, { film_id: "4" }, { film_id: "6" }, { film_id: "8" }];
Is this page helpful?
Scored Successfully!