milvus-logo
LFAI
Home
  • Benutzerhandbuch

Metadata Filtering

A filter expression can be used to filter a specific scalar field during a search or query to obtain precisely matched results. This guide will introduce how to use filter expressions in Zilliz through an example dataset. For demonstration purposes, this guide will only provide examples of query operations.

Example Dataset

Suppose the example dataset is stored in a collection named “my_collection” and includes 10 entities of electronic products. The following is the example dataset.

id

color

vector

price

inventory

sales_volume

description

1

pink_8682

[0.3580376395471989, -0.6023495712049978, ...]

593

{"brand": "Sony", "quantity": 310}

[161, 81, 51]

Sony Xperia 1 VI is a flagship Android smartphone released in 2024 with a 6.5-inch LTPO OLED display

2

red_7025

[0.43742130801983836, -0.5597502546264526, ....]

196

{"brand": "Samsung", "quantity": 274}

[126, 126, 125, 96, 155]

Galaxy S24 Ultra, Samsung’s latest flagship smartphone.

3

orange_6781

[0.19886812562848388, 0.06023560599112088, ...]

862

{"brand": "Samsung", "quantity": 103}

[124, 117, 90, 188]

Galaxy Fold features the world’s first 7.3-inch Infinity Flex Display.

4

pink_9298

[0.3172005263489739, 0.9719044792798428, ...]

991

{"brand": "Microsoft", "quantity": 175,}

[133, 92, 181, 61, 193]

Surface Duo 2, now with lightning-fast 5G(Footnote1) and dynamic triple lens camera.

5

red_4794

[0.4452349528804562, -0.8757026943054742, ...]

327

{"brand": "Apple", "quantity": 193}

[155, 161, 106, 86, 99]

iPhone 15 Pro, A new chip designed for better gaming and other 'pro' features.

6

yellow_4222

[0.985825131989184, -0.8144651566660419, ...]

996

{"brand": "Microsoft", "quantity": 376}

[173, 151, 148]

The Microsoft Surface Duo seems at first like the perfect little device for this new work-from-home world.

7

red_9392

[0.8371977790571115, -0.015764369584852833, ...]

848

{"brand": "Apple", "quantity": 61}

[59, 156, 126, 60, 177]

The iPhone 14 is a smartphone from Apple Inc. that comes in various colors and sizes.

8

grey_8510

[-0.33445148015177995, -0.2567135004164067, ...]

241

{"brand": "Dell", "quantity": 248}

[105, 126, 114, 132]

The Dell Inspiron 15 3000 laptop is equipped with a powerful Intel Core i5-1135G7 Quad-Core Processor, 12GB RAM and 256GB SSD storage.

9

white_9381

[0.39524717779832685, 0.4000257286739164, ...]

597

{"brand": "Apple", "quantity": 351}

[150, 150, 73]

The iPhone 16 features a 6.1-inch OLED display, is powered by Apple's A18 processor, and has dual cameras at the back.

10

purple_4976

[0.5718280481994695, 0.24070317428066512, ...]

450

{"brand": "Apple", "quantity": 268}

[190, 149, 85, 79, 80]

The iPad is a brand of iOS- and iPadOS-based tablet computers that are developed and marketed by Apple.

  • id: The ID of the product. The data type of this field is INT64.

  • vector: The embedding vector of the product image that can represent different features of the product (such as product size, style, pattern, etc.). For convenience, this field is omitted in the demonstration.

  • color: The color of the product. The data type of this field is VARCHAR. The numeric value in this field indicates the hue, which helps differentiate various shades of colors.

  • price: The price of the product. The data type of this field is INT64.

  • inventory: The inventory of the product. The data type of this field is JSON and contains two keys: the key brand represents the brand of the product and the key quantity represents the number of items in stock.

  • sales_volume: The sales volume of products in different countries. The data type of this field is Array. The values in this array contain 3 to 5 integers.

  • description: The description of the product. The data type of this field is VARCHAR. It offers a summary of the product features, functionality, and intended users.

Single-condition filtering

The following types of operators can be used in filters with single condition:

Comparison operators

Comparison operators include:

  • >: Greater than

  • <: Less than

  • ==: Equal

  • <=: Less than or equal

  • >=: Greater than or equal

  • !=: Not equal

Example 1: Apply filter on scalar field

The following example demonstrates how to filter products with prices ranging from 500 to 900:

from pymilvus import MilvusClient

client = MilvusClient(
    uri="http://localhost:19530",
    token="root:Milvus"
)

results = client.query(
    collection_name="my_collection",
    filter="500 < price < 900",
    output_fields=["id", "color", "price"]
)

# Output
# {'id': 1, 'color': 'pink_8682', 'price': np.float32(593.0)}
# {'id': 3, 'color': 'orange_6781', 'price': np.float32(862.0)}
# {'id': 7, 'color': 'red_9392', 'price': np.float32(848.0)}
# {'id': 9, 'color': 'white_9381', 'price': np.float32(597.0)}

import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

MilvusClientV2 client = new MilvusClientV2(ConnectConfig.builder()
        .uri("http://localhost:19530")
        .token("root:Milvus")
        .build());

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("500 < price < 900")
        .outputFields(Arrays.asList("id", "color", "price"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=pink_8682, price=593.0, id=1}
// {color=orange_6781, price=862.0, id=3}
// {color=red_9392, price=848.0, id=7}
// {color=white_9381, price=597.0, id=9}

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

const address = "http://localhost:19530";
const token = "root:Milvus";
const client = new MilvusClient({address, token});

var res = client.query({
    collection_name="my_collection",
    filter="500 < price < 900",
    output_fields=["id", "color", "price"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "500 < price < 900",
    "outputFields": ["id", "color", "price"]
}'

The filtered results are as follows:

[
    {"id": 1, "color": "pink_8682" "price":593},
    {"id": 3, "color": "orange_6781" "price":862},
    {"id": 7, "color": "red_9392" "price":848},
    {"id": 9, "color": "white_9381" "price":597}
]

Example 2: Apply filter on JSON field

The following example demonstrates how to filter products with an inventory quantity of 250 or more.

results = client.query(
    collection_name="my_collection",
    filter='inventory["quantity"] >= 250',
    output_fields=["id", "color", "price", "inventory"]
)

# Output
# {'id': 1, 'color': 'pink_8682', 'price': np.float32(593.0), 'inventory': {'brand': 'Sony', 'quantity': 310}}
# {'id': 2, 'color': 'red_7025', 'price': np.float32(196.0), 'inventory': {'brand': 'Samsung', 'quantity': 274}}
# {'id': 6, 'color': 'yellow_4222', 'price': np.float32(996.0), 'inventory': {'brand': 'Microsoft', 'quantity': 376}}
# {'id': 9, 'color': 'white_9381', 'price': np.float32(597.0), 'inventory': {'brand': 'Apple', 'quantity': 351}}
# {'id': 10, 'color': 'purple_4976', 'price': np.float32(450.0), 'inventory': {'brand': 'Apple', 'quantity': 268}}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("inventory[\"quantity\"] >= 250")
        .outputFields(Arrays.asList("id", "color", "price", "inventory"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=pink_8682, price=593.0, id=1, inventory={"brand":"Sony","quantity":310}}
// {color=red_7025, price=196.0, id=2, inventory={"brand":"Samsung","quantity":274}}
// {color=yellow_4222, price=996.0, id=6, inventory={"brand":"Microsoft","quantity":376}}
// {color=white_9381, price=597.0, id=9, inventory={"brand":"Apple","quantity":351}}
// {color=purple_4976, price=450.0, id=10, inventory={"brand":"Apple","quantity":268}}

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

var res = client.query({
    collection_name="my_collection",
    filter='inventory["quantity"] >= 250',
    output_fields=["id", "color", "price", "inventory"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "inventory[\"quantity\"] >=250",
    "outputFields": ["id", "color", "price", "inventory"]
}'

The filtered results are as follows:

[
  {
    "id": 1,
    "color": "pink_8682",
    "price": 593,
    "inventory": {
      "brand": "Sony",
      "quantity": 310
    },
    "sales_volume": [
      161,
      81,
      51
    ]
  },
  {
    "id": 2,
    "color": "red_7025",
    "price": 196,
    "inventory": {
      "brand": "Samsung",
      "quantity": 274
    },
    "sales_volume": [
      126,
      126,
      125,
      96,
      155
    ]
  },
  {
    "id": 6,
    "color": "yellow_4222",
    "price": 996,
    "inventory": {
      "brand": "Microsoft",
      "quantity": 376
    },
    "sales_volume": [
      173,
      151,
      148
    ]
  },
  {
    "id": 9,
    "color": "white_9381",
    "price": 597,
    "inventory": {
      "brand": "Apple",
      "quantity": 351
    },
    "sales_volume": [
      150,
      150,
      73
    ]
  },
  {
    "id": 10,
    "color": "purple_4976",
    "price": 450,
    "inventory": {
      "brand": "Apple",
      "quantity": 268
    },
    "sales_volume": [
      190,
      149,
      85,
      79,
      80
    ]
  }
]

Example 3: Apply filter on Array field

The following example demonstrates how to filter products whose sales volume in the first country is 150 or more.

results = client.query(
    collection_name="my_collection",
    filter="sales_volume[0] >= 150",
    output_fields=["id", "color", "price", "sales_volume"]
)

# Output
# {'id': 1, 'color': 'pink_8682', 'price': np.float32(593.0), 'sales_volume': [161, 81, 51]}
# {'id': 5, 'color': 'red_4794', 'price': np.float32(327.0), 'sales_volume': [155, 161, 106, 86, 99]}
# {'id': 6, 'color': 'yellow_4222', 'price': np.float32(996.0), 'sales_volume': [173, 151, 148]}
# {'id': 9, 'color': 'white_9381', 'price': np.float32(597.0), 'sales_volume': [150, 150, 73]}
# {'id': 10, 'color': 'purple_4976', 'price': np.float32(450.0), 'sales_volume': [190, 149, 85, 79, 80]}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("sales_volume[0] >= 150")
        .outputFields(Arrays.asList("id", "color", "price", "sales_volume"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=pink_8682, price=593.0, id=1, sales_volume=[161, 81, 51]}
// {color=red_4794, price=327.0, id=5, sales_volume=[155, 161, 106, 86, 99]}
// {color=yellow_4222, price=996.0, id=6, sales_volume=[173, 151, 148]}
// {color=white_9381, price=597.0, id=9, sales_volume=[150, 150, 73]}
// {color=purple_4976, price=450.0, id=10, sales_volume=[190, 149, 85, 79, 80]}

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

var res = client.query({
    collection_name="my_collection",
    filter="sales_volume[0] >= 150",
    output_fields=["id", "color","price", "sales_volume"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "sales_volume[0] >= 150",
    "outputFields": ["id", "color", "price", "sales_volume"]
}'

The filtered results are as follows:

[
  {
    "id": 1,
    "color": "pink_8682",
    "price": 593,
    "sales_volume": [
      161,
      81,
      51
    ]
  },
  {
    "id": 5,
    "color": "red_4794",
    "price": 327,
    "sales_volume": [
      155,
      161,
      106,
      86,
      99
    ]
  },
  {
    "id": 6,
    "color": "yellow_4222",
    "price": 996,
    "sales_volume": [
      173,
      151,
      148
    ]
  },
  {
    "id": 9,
    "color": "white_9381",
    "price": 597,
    "sales_volume": [
      150,
      150,
      73
    ]
  },
  {
    "id": 10,
    "color": "purple_4976",
    "price": 450,
    "sales_volume": [
      190,
      149,
      85,
      79,
      80
    ]
  }
]

Term operators

Term operators include:

  • in: Filter results that match the condition

  • not in: Filter results that do not match the condition

Example 1: Apply filter on scalar field

The following example demonstrates how to filter products whose color is not red.

results = client.query(
    collection_name="my_collection",
    filter='color not in ["red_7025","red_4794","red_9392"]',
    output_fields=["id", "color", "price"]
)

# Output
# {'id': 1, 'color': 'pink_8682', 'price': np.float32(593.0)}
# {'id': 3, 'color': 'orange_6781', 'price': np.float32(862.0)}
# {'id': 4, 'color': 'pink_9298', 'price': np.float32(991.0)}
# {'id': 6, 'color': 'yellow_4222', 'price': np.float32(996.0)}
# {'id': 8, 'color': 'grey_8510', 'price': np.float32(241.0)}
# {'id': 9, 'color': 'white_9381', 'price': np.float32(597.0)}
# {'id': 10, 'color': 'purple_4976', 'price': np.float32(450.0)}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("color not in [\"red_7025\",\"red_4794\",\"red_9392\"]")
        .outputFields(Arrays.asList("id", "color", "price"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=pink_8682, price=593.0, id=1}
// {color=orange_6781, price=862.0, id=3}
// {color=pink_9298, price=991.0, id=4}
// {color=yellow_4222, price=996.0, id=6}
// {color=grey_8510, price=241.0, id=8}
// {color=white_9381, price=597.0, id=9}
// {color=purple_4976, price=450.0, id=10}

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

var res = client.query({
    collection_name="my_collection",
    filter='color not in ["red_7025","red_4794","red_9392"]',
    output_fields=["id", "color", "price"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "color not in [\"red_7025\",\"red_4794\",\"red_9392\"]",
    "outputFields": ["id", "color", "price"]
}'

The filtered results are as follows:

[
    {"id": 1, "color": "pink_8682", "price":593},
    {"id": 3, "color": "orange_6781", "price":863},
    {"id": 4, "color": "pink_9298" "price":991},
    {"id": 6, "color": "yellow_4222" "price":996},
    {"id": 8, "color": "grey_8510" "price":241},
    {"id": 9, "color": "white_9381" "price":597},
    {"id": 10, "color": "purple_4976" "price":450}
]

Example 2: Apply filter on JSON field

The following example demonstrates how to filter products whose brand is Apple.

results = client.query(
    collection_name="my_collection",
    filter='inventory["brand"] in ["Apple"]',
    output_fields=["id", "color", "price", "inventory"]
)

# Output
# {'id': 5, 'color': 'red_4794', 'price': np.float32(327.0), 'inventory': {'brand': 'Apple', 'quantity': 193}}
# {'id': 7, 'color': 'red_9392', 'price': np.float32(848.0), 'inventory': {'brand': 'Apple', 'quantity': 61}}
# {'id': 9, 'color': 'white_9381', 'price': np.float32(597.0), 'inventory': {'brand': 'Apple', 'quantity': 351}}
# {'id': 10, 'color': 'purple_4976', 'price': np.float32(450.0), 'inventory': {'brand': 'Apple', 'quantity': 268}}} 

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("inventory[\"brand\"] in [\"Apple\"]")
        .outputFields(Arrays.asList("id", "color", "price", "inventory"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {'id': 5, 'color': 'red_4794', 'price': np.float32(327.0), 'inventory': {'brand': 'Apple', 'quantity': 193}}
// {'id': 7, 'color': 'red_9392', 'price': np.float32(848.0), 'inventory': {'brand': 'Apple', 'quantity': 61}}
// {'id': 9, 'color': 'white_9381', 'price': np.float32(597.0), 'inventory': {'brand': 'Apple', 'quantity': 351}}
// {'id': 10, 'color': 'purple_4976', 'price': np.float32(450.0), 'inventory': {'brand': 'Apple', 'quantity': 268}}

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

var res = client.query({
    collection_name="my_collection",
    filter='inventory["brand"] in ["Apple"]',
    output_fields=["id", "color", "price", "inventory"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "inventory[\"brand\"] in [\"Apple\"]",
    "outputFields": ["id", "color","price", "inventory"]
}'

The filtered results are as follows:

[
  {
    "id": 5,
    "color": "red_4794",
    "price": 327,
    "inventory": {
      "brand": "Apple",
      "quantity": 193
    }
  },
  {
    "id": 7,
    "color": "red_9392",
    "price": 848,
    "inventory": {
      "brand": "Apple",
      "quantity": 61
    }
  },
  {
    "id": 9,
    "color": "white_9381",
    "price": 597,
    "inventory": {
      "brand": "Apple",
      "quantity": 351
    }
  },
  {
    "id": 10,
    "color": "purple_4976",
    "price": 450,
    "inventory": {
      "brand": "Apple",
      "quantity": 268
    }
  }
]

Match operators

Match operators include:

  • like: Match constants or prefixes (prefix%), infixes (%infix%), and suffixes (%suffix) within constants. It relies on a brute-force search mechanism using wildcards and does not involve text tokenization. While it can achieve exact matches, its query efficiency is relatively low, making it suitable for simple matching tasks or queries on smaller datasets.

  • TEXT_MATCH: Match specific terms or keywords on VARCHAR fields, using tokenization and inverted index to enable efficient text search. Compared to like, TEXT_MATCH offers more advanced text tokenization and filtering capabilities. It is suited for large-scale datasets where higher query performance is required for complex text search scenarios.

    To use the TEXT_MATCH filter expression, you must enable text matching for the target VARCHAR field when creating the collection. For details, refer to Text Match.

Example 1: Apply filter on scalar field

The following example demonstrates how to filter products whose color is red. In this case, you can quickly filter all red products by matching the prefix 'red%’. Similarly, you can use the expression color in ['red_7025’, 'red_4794’, ‘red_9392’] to filter all red products. However, when the data is more complex, we recommend using the like operator for more efficient filtering.

results = client.query(
    collection_name="my_collection",
    filter='color like "red%"',
    output_fields=["id", "color", "price"]
)

# Output
# {'id': 2, 'color': 'red_7025', 'price': np.float32(196.0)}
# {'id': 5, 'color': 'red_4794', 'price': np.float32(327.0)}
# {'id': 7, 'color': 'red_9392', 'price': np.float32(848.0)}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("color like \"red%\"")
        .outputFields(Arrays.asList("id", "color", "price"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=red_7025, price=196.0, id=2}
// {color=red_4794, price=327.0, id=5}
// {color=red_9392, price=848.0, id=7}

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

var res = client.query({
    collection_name="my_collection",
    filter='color like "red%"',
    output_fields=["id", "color", "price"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "color like \"red%\"",
    "outputFields": ["id", "color", "price"]
}'

The filtered results are as follows:

[
    {"id": 2, "color": "red_7025", "price":196},
    {"id": 5, "color": "red_4794" "price":327},
    {"id": 7, "color": "red_9392" "price":848}
]

Example 2: Apply filter on JSON field

The following example demonstrates how to filter products whose brand name starts with the letter 'S’.

results = client.query(
    collection_name="my_collection",
    filter='inventory["brand"] like "S%"',
    output_fields=["id", "color", "price", "inventory"]
)

# Output
# {'id': 1, 'color': 'pink_8682', 'price': np.float32(593.0), 'inventory': {'brand': 'Sony', 'quantity': 310}}
# {'id': 2, 'color': 'red_7025', 'price': np.float32(196.0), 'inventory': {'brand': 'Samsung', 'quantity': 274}}
# {'id': 3, 'color': 'orange_6781', 'price': np.float32(862.0), 'inventory': {'brand': 'Samsung', 'quantity': 103}}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("inventory[\"brand\"] like \"S%\"")
        .outputFields(Arrays.asList("id", "color", "price", "inventory"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=pink_8682, price=593.0, id=1, inventory={"brand":"Sony","quantity":310}}
// {color=red_7025, price=196.0, id=2, inventory={"brand":"Samsung","quantity":274}}
// {color=orange_6781, price=862.0, id=3, inventory={"brand":"Samsung","quantity":103}}

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

var res = client.query({
    collection_name="my_collection",
    filter='inventory["brand"] like "S%"',
    output_fields=["id", "color", "price", "inventory"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "inventory[\"brand\"] like \"S%\"",
    "outputFields": ["id", "color", "price", "inventory"]
}'

The filtered results are as follows:

[
  {
    "id": 1,
    "color": "pink_8682",
    "price": 593,
    "inventory": {
      "brand": "Sony",
      "quantity": 310
    }
  },
  {
    "id": 2,
    "color": "red_7025",
    "price": 196,
    "inventory": {
      "brand": "Samsung",
      "quantity": 274
    }
  },
  {
    "id": 3,
    "color": "orange_6781",
    "price": 862,
    "inventory": {
      "brand": "Samsung",
      "quantity": 103
    }
  }
]

Example 3: Text match on VARCHAR fields

The TEXT_MATCH expression is used for text match on VARCHAR fields. By default, it applies an OR logic, but you can combine it with other logical operators to create more complex query conditions. For details, refer to Text Match.

The following example demonstrates how to use the TEXT_MATCH expression to filter products where the description field contains either the term "Apple" or "iPhone":

results = client.query(
    collection_name="my_collection",
    filter='TEXT_MATCH(description, "Apple iPhone")',
    output_fields=["id", "description"],
)

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("TEXT_MATCH(description, \"Apple iPhone\")")
        .outputFields(Arrays.asList("id", "description"))
        .build();

QueryResp getResp = client.query(queryReq);

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

const results = client.query({
    collection_name: "my_collection",
    filter: "TEXT_MATCH(description, 'Apple iPhone')",
    output_fields: ["id", "description"]
});

The filtered results are as follows:

[
    {'id': 5, 'description': "iPhone 15 Pro, A new chip designed for better gaming and other 'pro' features."}
    {'id': 7, 'description': "The iPhone 14 is a smartphone from Apple Inc. that comes in various colors and sizes."}
    {'id': 9, 'description': "The iPhone 16 features a 6.1-inch OLED display, is powered by Apple's A18 processor, and has dual cameras at the back."}
    {'id': 10, 'description': "The iPad is a brand of iOS- and iPadOS-based tablet computers that are developed and marketed by Apple."}
]

To filter for descriptions containing multiple keywords simultaneously, you can use the and operator. The following example demonstrates how to filter products where the description field contains both "chip" and "iPhone":

results = client.query(
    collection_name="my_collection",
    filter='TEXT_MATCH(description, "chip") and TEXT_MATCH(description, "iPhone")',
    output_fields=["id", "description"],
)

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("TEXT_MATCH(description, \"chip\") and TEXT_MATCH(description, \"iPhone\")")
        .outputFields(Arrays.asList("id", "description"))
        .build();

QueryResp getResp = client.query(queryReq);

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

const results = client.query({
    collection_name: "my_collection",
    filter: "TEXT_MATCH(description, 'chip') and TEXT_MATCH(description, 'iPhone')",
    output_fields: ["id", "description"]
});

The filtered results are as follows:

[
    {'id': 5, 'description': "iPhone 15 Pro, A new chip designed for better gaming and other 'pro' features."}
]

Arithmetic operators

Arithmetic operators include:

  • +: Addition

  • -: Subtraction

  • *: Multiplication

  • /: Division

  • **: Power

  • %: Modulo

Example 1: Apply filter on scalar field

The following example demonstrates how to filter products whose price, after a 50% discount, is between 200 and 300 (both inclusive).

results = client.query(
    collection_name="my_collection",
    filter="200 <= price*0.5 and price*0.5 <= 300",
    output_fields=["id", "price"]
)

# Output
# {'id': 1, 'price': np.float32(593.0)}
# {'id': 9, 'price': np.float32(597.0)}
# {'id': 10, 'price': np.float32(450.0)}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

ueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("200 <= price*0.5 and price*0.5 <= 300")
        .outputFields(Arrays.asList("id", "price"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {price=593.0, id=1}
// {price=597.0, id=9}
// {price=450.0, id=10}

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

var res = client.query({
    collection_name="my_collection",
    filter="200 <= price*0.5 <= 300",
    output_fields=["id", "price"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "200 <= price*0.5 <= 300",
    "outputFields": ["id", "price"]
}'

The filtered results are as follows:

[
    {"id": 1, "color": "pink_8682", "price":593},
    {"id": 9, "color": "white_9381", "price":597},
    {"id": 10, "color": "purple_4976" "price":450}
]

Example 2: Apply filter on JSON field

The following example demonstrates how to filter products whose inventory, when doubled, exceeds 600 items.

results = client.query(
    collection_name="my_collection",
    filter='inventory["quantity"] * 2 > 600',
    output_fields=["id", "color", "price", "inventory"]
)

# Output
# {'id': 1, 'color': 'pink_8682', 'price': np.float32(593.0), 'inventory': {'brand': 'Sony', 'quantity': 310}}
# {'id': 6, 'color': 'yellow_4222', 'price': np.float32(996.0), 'inventory': {'brand': 'Microsoft', 'quantity': 376}}
# {'id': 9, 'color': 'white_9381', 'price': np.float32(597.0), 'inventory': {'brand': 'Apple', 'quantity': 351}}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("inventory[\"quantity\"] * 2 > 600")
        .outputFields(Arrays.asList("id", "color", "price", "inventory"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=pink_8682, price=593.0, id=1, inventory={"brand":"Sony","quantity":310}}
// {color=yellow_4222, price=996.0, id=6, inventory={"brand":"Microsoft","quantity":376}}
// {color=white_9381, price=597.0, id=9, inventory={"brand":"Apple","quantity":351}}

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

var res = client.query({
    collection_name="my_collection",
    filter='inventory["quantity"] * 2 > 600',
    output_fields=["id", "color", "price", "inventory"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "inventory[\"quantity\"] * 2 > 600",
    "outputFields": ["id", "color", "price", "inventory"]
}'

The filtered results are as follows:

[
  {
    "id": 1,
    "color": "pink_8682",
    "price": 593,
    "inventory": {
      "brand": "Sony",
      "quantity": 310
    }
  },
  {
    "id": 6,
    "color": "yellow_4222",
    "price": 996,
    "inventory": {
      "brand": "Microsoft",
      "quantity": 376
    }
  },
  {
    "id": 9,
    "color": "white_9381",
    "price": 597,
    "inventory": {
      "brand": "Apple",
      "quantity": 351
    }
  }
]

Example 3: Apply filter on Array field

The following example demonstrates how to filter products whose combined sales in the first and second countries exceed 300.

results = client.query(
    collection_name="my_collection",
    filter="sales_volume[0]*2 > 300",
    output_fields=["id", "color", "price", "sales_volume"]
)

# Output
# {'color': 'pink_8682', 'price': np.float32(593.0), 'sales_volume': [161, 81, 51], 'id': 1}
# {'color': 'red_4794', 'price': np.float32(327.0), 'sales_volume': [155, 161, 106, 86, 99], 'id': 5}
# {'color': 'yellow_4222', 'price': np.float32(996.0), 'sales_volume': [173, 151, 148], 'id': 6}
# {'color': 'purple_4976', 'price': np.float32(450.0), 'sales_volume': [190, 149, 85, 79, 80], 'id': 10}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("sales_volume[0]*2 > 300")
        .outputFields(Arrays.asList("id", "color", "price", "sales_volume"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=pink_8682, price=593.0, id=1, sales_volume=[161, 81, 51]}
// {color=red_4794, price=327.0, id=5, sales_volume=[155, 161, 106, 86, 99]}
// {color=yellow_4222, price=996.0, id=6, sales_volume=[173, 151, 148]}
// {color=purple_4976, price=450.0, id=10, sales_volume=[190, 149, 85, 79, 80]}

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

var res = client.query({
    collection_name="my_collection",
    filter="sales_volume[0]*2 > 300",
    output_fields=["id", "color","price", "sales_volume"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "sales_volume[0]*2 > 300",
    "outputFields": ["id", "color", "price", "sales_volume"]
}'

The filtered results are as follows:

[
  {
    "id": 5,
    "color": "red_4794",
    "price": 327,
    "sales_volume": [
      155,
      161,
      106,
      86,
      99
    ]
  },
  {
    "id": 6,
    "color": "yellow_4222",
    "price": 996,
    "sales_volume": [
      173,
      151,
      148
    ]
  },
  {
    "id": 10,
    "color": "purple_4976",
    "price": 450,
    "sales_volume": [
      190,
      149,
      85,
      79,
      80
    ]
  }
]

Advanced JSON operators

JSON operators include:

  • JSON_CONTAINS: Filter entities whose JSON field contains elements from a specific list.

  • JSON_CONTAINS_ALL: Filter entities whose JSON field contains all elements from a specific list in the same order.

  • JSON_CONTAINS_ANY: Filter all entities whose JSON field contains any one element from a specific list.

When using JSON opertors, the JSON field must contain at least one key whose value is a list.

To demonstrate how to use advanced filtering operators on JSON fields, we make a slight adjustment to the example dataset in this section. A new key named ‘previous_sales’ has been added to the JSON ‘inventory’ field, which represents the previous sales of the product in three countries. The value of this key is a list of numbers. Below is the modified new example dataset:

Example 1: JSON_CONTAINS

JSON_CONTAINS(identifier, JsonExpr)identifier is the key name in the JSON field and JsonExpr is the list of filtering conditions.

The following example demonstrates how to filter products that previously had sales of 232 items in a specific country.

results = client.query(
    collection_name="my_collection",
    filter='JSON_CONTAINS(inventory[\"previous_sales\"], 232)',
    output_fields=["id", "color", "price", "inventory"]
)

# Output
# {'inventory': {'brand': 'Samsung', 'quantity': 103, 'previous_sales': [232, 254, 275]}, 'id': 3, 'color': 'orange_6781', 'price': np.float32(862.0)}
# {'inventory': {'brand': 'Microsoft', 'quantity': 376, 'previous_sales': [254, 275, 232]}, 'id': 6, 'color': 'yellow_4222', 'price': np.float32(996.0)}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("JSON_CONTAINS(inventory[\"previous_sales\"], 232)")
        .outputFields(Arrays.asList("id", "color", "price", "inventory"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=orange_6781, price=862.0, id=3, inventory={"brand":"Samsung","quantity":103,"previous_sales":[232,254,275]}}
// {color=yellow_4222, price=996.0, id=6, inventory={"brand":"Microsoft","quantity":376,"previous_sales":[254,275,232]}}

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

var res = client.query({
    collection_name="my_collection",
    filter='JSON_CONTAINS(inventory[\"previous_sales\"], 232)',
    output_fields=["id", "color","price", "inventory"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "JSON_CONTAINS(inventory[\"previous_sales\"], 232)",
    "outputFields": ["id", "color", "price", "inventory"]
}'

The filtered results are as follows:

[
  {
    "id": 3,
    "color": "orange_6781",
    "price": 862,
    "inventory": {
      "brand": "Samsung",
      "quantity": 103,
      "previous_sales": [
        232,
        254,
        275
      ]
    }
  }
]

Example 2: JSON_CONTAINS_ALL

JSON_CONTAINS_ALL(identifier, JsonExpr)identifier is the key name in the JSON field and JsonExpr is the list of filtering conditions.

The following example demonstrates how to filter products that had previous sales of 232, 254, and 275 items in three different countries.

results = client.query(
    collection_name="my_collection",
    filter='JSON_CONTAINS_ALL(inventory["previous_sales"], [232, 254, 275])',
    output_fields=["id", "color", "price", "inventory"]
)

# Output
# {'id': 3, 'color': 'orange_6781', 'price': np.float32(862.0), 'inventory': {'brand': 'Samsung', 'quantity': 103, 'previous_sales': [232, 254, 275]}}
# {'id': 6, 'color': 'yellow_4222', 'price': np.float32(996.0), 'inventory': {'brand': 'Microsoft', 'quantity': 376, 'previous_sales': [254, 275, 232]}}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("JSON_CONTAINS_ALL(inventory[\"previous_sales\"], [232, 254, 275])")
        .outputFields(Arrays.asList("id", "color", "price", "inventory"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=orange_6781, price=862.0, id=3, inventory={"brand":"Samsung","quantity":103,"previous_sales":[232,254,275]}}
// {color=yellow_4222, price=996.0, id=6, inventory={"brand":"Microsoft","quantity":376,"previous_sales":[254,275,232]}}

const { MilvusClient, DataType } = require("@zilliz/milvus2-sdk-node")

var res = client.query({
    collection_name="my_collection",
    filter='JSON_CONTAINS_ALL(inventory["previous_sales"], [232, 254, 275])',
    output_fields=["id", "color","price", "inventory"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "JSON_CONTAINS_ALL(inventory[\"previous_sales\"], [232, 254, 275])",
    "outputFields": ["id", "color", "price", "inventory"]
}'

The filtered results are as follows:

[
  {
    "id": 3,
    "color": "orange_6781",
    "price": 862,
    "inventory": {
      "brand": "Samsung",
      "quantity": 103,
      "previous_sales": [
        232,
        254,
        275
      ]
    }
  },
  {
    "id": 6,
    "color": "yellow_4222",
    "price": 996,
    "inventory": {
      "brand": "Microsoft",
      "quantity": 376,
      "previous_sales": [
        254,
        275,
        232
      ]
    }
  }
]

Example 3: JSON_CONTAINS_ANY

JSON_CONTAINS_ANY(identifier, JsonExpr)identifier is the key name in the JSON field and JsonExpr is the list of filtering conditions.

The following example demonstrates how to filter products that had previous sales of either 232, 254, or 275 items in any one of the three countries.

results = client.query(
    collection_name="my_collection",
    filter='JSON_CONTAINS_ANY(inventory["previous_sales"], [232, 254, 275])',
    output_fields=["id", "color", "price", "inventory"]
)

# Output
# {'id': 3, 'color': 'orange_6781', 'price': np.float32(862.0), 'inventory': {'brand': 'Samsung', 'quantity': 103, 'previous_sales': [232, 254, 275]}}
# {'id': 6, 'color': 'yellow_4222', 'price': np.float32(996.0), 'inventory': {'brand': 'Microsoft', 'quantity': 376, 'previous_sales': [254, 275, 232]}}
# {'id': 7, 'color': 'red_9392', 'price': np.float32(848.0), 'inventory': {'brand': 'Apple', 'quantity': 61, 'previous_sales': [312, 254, 367]}}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("JSON_CONTAINS_ANY(inventory[\"previous_sales\"], [232, 254, 275])")
        .outputFields(Arrays.asList("id", "color", "price", "inventory"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=orange_6781, price=862.0, id=3, inventory={"brand":"Samsung","quantity":103,"previous_sales":[232,254,275]}}
// {color=yellow_4222, price=996.0, id=6, inventory={"brand":"Microsoft","quantity":376,"previous_sales":[254,275,232]}}
// {color=red_9392, price=848.0, id=7, inventory={"brand":"Apple","quantity":61,"previous_sales":[312,254,367]}}

const { MilvusClient, DataType } = require("@zilliz/milvus2-sdk-node")

var res = client.query({
    collection_name="my_collection",
    filter='JSON_CONTAINS_ANY(inventory["previous_sales"], [232, 254, 275])',
    output_fields=["id", "color","price", "inventory"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "JSON_CONTAINS_ANY(inventory[\"previous_sales\"], [232, 254, 275])",
    "outputFields": ["id", "color", "price", "inventory"]
}'

The filtered results are as follows:

[
  {
    "id": 3,
    "color": "orange_6781",
    "price": 862,
    "inventory": {
      "brand": "Samsung",
      "quantity": 103,
      "previous_sales": [
        232,
        254,
        275
      ]
    }
  },
  {
    "id": 6,
    "color": "yellow_4222",
    "price": 996,
    "inventory": {
      "brand": "Microsoft",
      "quantity": 376,
      "previous_sales": [
        254,
        275,
        232
      ]
    }
  },
  {
    "id": 7,
    "color": "red_9392",
    "price": 848,
    "inventory": {
      "brand": "Apple",
      "quantity": 61,
      "previous_sales": [
        312,
        254,
        367
      ]
    }
  }
]

Advanced Array operators

Array operators include:

  • ARRAY_CONTAINS: Filter all entities whose Array field contains a specific element.

  • ARRAY_CONTAINS_ALL: Filter all entities whose Array field contains all specified elements.

  • ARRAY_CONTAINS_ANY: Filter all entities whose Array field contains any one of the specified elements.

  • ARRAY_LENGTH: Check the number of elements in the list.

Example 1: ARRAY_CONTAINS

ARRAY_CONTAINS(identifier, ArrayExpr)identifier is the name of the Array field, and ArrayExpr is the array of filtering conditions.

The following example demonstrates how to filter products with current sales of 161 items in a specific country.

results = client.query(
    collection_name="my_collection",
    filter='ARRAY_CONTAINS(sales_volume, 161)',
    output_fields=["id", "color", "price", "sales_volume"]
)

# Output
# {'id': 1, 'color': 'pink_8682', 'price': np.float32(593.0), 'sales_volume': [161, 81, 51]}
# {'id': 5, 'color': 'red_4794', 'price': np.float32(327.0), 'sales_volume': [155, 161, 106, 86, 99]}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("ARRAY_CONTAINS(sales_volume, 161)")
        .outputFields(Arrays.asList("id", "color", "price", "sales_volume"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=pink_8682, price=593.0, id=1, sales_volume=[161, 81, 51]}
// {color=red_4794, price=327.0, id=5, sales_volume=[155, 161, 106, 86, 99]}

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

var res = client.query({
    collection_name="my_collection",
    filter="ARRAY_CONTAINS(sales_volume, 161)",
    output_fields=["id", "color","price", "sales_volume"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "ARRAY_CONTAINS(sales_volume, 161)",
    "outputFields": ["id", "color", "price", "sales_volume"]
}'

The filtered results are as follows:

[
  {
    "id": 1,
    "color": "pink_8682",
    "price": 593,
    "sales_volume": [
      161,
      81,
      51
    ]
  },
  {
    "id": 5,
    "color": "red_4794",
    "price": 327,
    "sales_volume": [
      155,
      161,
      106,
      86,
      99
    ]
  }
]

Example 2: ARRAY_CONTAINS_ALL

ARRAY_CONTAINS_ALL(identifier, ArrayExpr)identifier is the name of the Array field, andArrayExpr is the array of filtering conditions.

The following example demonstrates how to filter products with current sales of 150 items in both the first and second countries.

results = client.query(
    collection_name="my_collection",
    filter='ARRAY_CONTAINS_ALL(sales_volume, [150, 150])',
    output_fields=["id", "color", "price", "sales_volume"]
)

# Output
# {'price': np.float32(597.0), 'sales_volume': [150, 150, 73], 'id': 9, 'color': 'white_9381'}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("ARRAY_CONTAINS_ALL(sales_volume, [150, 150])")
        .outputFields(Arrays.asList("id", "color", "price", "sales_volume"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=white_9381, price=597.0, id=9, sales_volume=[150, 150, 73]}

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

var res = client.query({
    collection_name="my_collection",
    filter="ARRAY_CONTAINS_ALL(sales_volume, [150, 150])",
    output_fields=["id", "color","price", "sales_volume"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "ARRAY_CONTAINS_ALL(sales_volume, [150, 150])",
    "outputFields": ["id", "color", "price", "sales_volume"]
}'

The filtered results are as follows:

[
  {
    "id": 9,
    "color": "white_9381",
    "price": 597,
    "sales_volume": [
      150,
      150,
      73
    ]
  }
]

Example 3: ARRAY_CONTAINS_ANY

ARRAY_CONTAINS_ANY(identifier, ArrayExpr)identifier is the name of the Array field, and ArrayExpr is the array of filtering conditions.

The following example demonstrates how to filter products with current sales of either 150, 190, or 90 items in any country.

results = client.query(
    collection_name="my_collection",
    filter='ARRAY_CONTAINS_ANY(sales_volume, [150, 190, 90])',
    output_fields=["id", "color", "price", "sales_volume"]
)

# Output
# {'id': 3, 'color': 'orange_6781', 'price': np.float32(862.0), 'sales_volume': [124, 117, 90, 188]}
# {'id': 9, 'color': 'white_9381', 'price': np.float32(597.0), 'sales_volume': [150, 150, 73]}
# {'id': 10, 'color': 'purple_4976', 'price': np.float32(450.0), 'sales_volume': [190, 149, 85, 79, 80]}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("ARRAY_CONTAINS_ANY(sales_volume, [150, 190, 90])")
        .outputFields(Arrays.asList("id", "color", "price", "sales_volume"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=orange_6781, price=862.0, id=3, sales_volume=[124, 117, 90, 188]}
// {color=white_9381, price=597.0, id=9, sales_volume=[150, 150, 73]}
// {color=purple_4976, price=450.0, id=10, sales_volume=[190, 149, 85, 79, 80]}

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

var res = client.query({
    collection_name="my_collection",
    filter="ARRAY_CONTAINS_ANY(sales_volume, [150, 190, 90])",
    output_fields=["id", "color","price", "sales_volume"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "ARRAY_CONTAINS_ANY(sales_volume, [150, 190, 90])",
    "outputFields": ["id", "color", "price", "sales_volume"]
}'

The filtered results are as follows:

[
  {
    "id": 3,
    "color": "orange_6781",
    "price": 862,
    "sales_volume": [
      124,
      117,
      90,
      188
    ]
  },
  {
    "id": 9,
    "color": "white_9381",
    "price": 597,
    "sales_volume": [
      150,
      150,
      73
    ]
  },
  {
    "id": 10,
    "color": "purple_4976",
    "price": 450,
    "sales_volume": [
      190,
      149,
      85,
      79,
      80
    ]
  }
]

Example 4: ARRAY_LENGTH

The following example demonstrates how to filter products that are sold in only three countries.

results = client.query(
    collection_name="my_collection",
    filter='ARRAY_LENGTH(sales_volume) == 3',
    output_fields=["id", "color", "price", "sales_volume"]
)

# Output
# {'id': 1, 'color': 'pink_8682', 'price': np.float32(593.0), 'sales_volume': [161, 81, 51]}
# {'id': 6, 'color': 'yellow_4222', 'price': np.float32(996.0), 'sales_volume': [173, 151, 148]}
# {'id': 9, 'color': 'white_9381', 'price': np.float32(597.0), 'sales_volume': [150, 150, 73]}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("ARRAY_LENGTH(sales_volume) == 3")
        .outputFields(Arrays.asList("id", "color", "price", "sales_volume"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=pink_8682, price=593.0, id=1, sales_volume=[161, 81, 51]}
// {color=yellow_4222, price=996.0, id=6, sales_volume=[173, 151, 148]}
// {color=white_9381, price=597.0, id=9, sales_volume=[150, 150, 73]}

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

var res = client.query({
    collection_name="my_collection",
    filter="ARRAY_LENGTH(sales_volume) == 3",
    output_fields=["id", "color","price", "sales_volume"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "ARRAY_LENGTH(sales_volume) == 3",
    "outputFields": ["id", "color", "price", "sales_volume"]
}'

The filtered results are as follows:

[
  {
    "id": 1,
    "color": "pink_8682",
    "price": 593,
    "sales_volume": [
      161,
      81,
      51
    ]
  },
  {
    "id": 6,
    "color": "yellow_4222",
    "price": 996,
    "sales_volume": [
      173,
      151,
      148
    ]
  },
  {
    "id": 9,
    "color": "white_9381",
    "price": 597,
    "sales_volume": [
      150,
      150,
      73
    ]
  }
]

Multi-condition filtering

The logical operators that can be used to combine multiple filtering conditions include:

  • and or &&: Both conditions should be satisfied.

  • or or ||: Only one of the two conditions should be satisfied.

When a lower precedence operation should be processed first, it should be enclosed within parentheses. Innermost parenthetical expressions are evaluated first.

Example

The following example demonstrates how to filter products that are red in color, priced below 500, branded as Apple, and have sales over 100 items in the first country

results = client.query(
    collection_name="my_collection",
    filter='color like "red%" and price < 500 and inventory["brand"] in ["Apple"] and sales_volume[0] > 100',
    output_fields=["id", "color", "price", "inventory", "sales_volume"]
)

# Output
# {'id': 5, 'color': 'red_4794', 'price': np.float32(327.0), 'inventory': {'brand': 'Apple', 'quantity': 193, 'previous_sales': [225, 286, 202]}, 'sales_volume': [155, 161, 106, 86, 99]}

import io.milvus.v2.service.vector.request.QueryReq
import io.milvus.v2.service.vector.request.QueryResp

QueryReq queryReq = QueryReq.builder()
        .collectionName("my_collection")
        .filter("color like \"red%\" and price < 500 and inventory[\"brand\"] in [\"Apple\"] and sales_volume[0] > 100")
        .outputFields(Arrays.asList("id", "color", "price", "inventory", "sales_volume"))
        .build();

QueryResp getResp = client.query(queryReq);

// Output
// {color=red_4794, price=327.0, id=5, inventory={"brand":"Apple","quantity":193,"previous_sales":[225,286,202]}, sales_volume=[155, 161, 106, 86, 99]}

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

var res = client.query({
    collection_name="my_collection",
    filter='color like "red%" and price < 500 and inventory["brand"] in ["Apple"] and sales_volume[0] > 100',
    output_fields=["id", "color", "price", "inventory", "sales_volume"]
})

export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/entities/query" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
    "collectionName": "my_collection",
    "filter": "color like \"red%\" and price < 500 and inventory[\"brand\"] in [\"Apple\"] and sales_volume[0] > 100",
    "outputFields": ["id", "color", "price", "inventory", "sales_volume"]
}'

The filtered results are as follows:

[
  {
    "id": 5,
    "color": "red_4794",
    "price": 327,
    "inventory": {
      "brand": "Apple",
      "quantity": 193
    },
    "sales_volume": [
      155,
      161,
      106,
      86,
      99
    ]
  }
]

Operator precedence

The following table lists the precedence of operators. Operators are listed top to bottom, in descending precedence.

PrecedenceOperator
1+, -
2not
3**
4*, /, %
5<, <=, >, >=
6==, !=
7like
8JSON_CONTAINS
9ARRAY_CONTAINS
10JSON_CONTAINS_ALL
11ARRAY_CONTAINS_ALL
12JSON_CONTAINS_ANY
13ARRAY_CONTAINS_ANY
14ARRAY_LENGTH
15and (&&)
16or (\\)

Expressions are normally evaluated from left to right. Complex expressions are evaluated one at a time. The order in which the expressions are evaluated is determined by the precedence of the operators used.

If an expression contains two or more operators with the same precedence, the operator to the left is evaluated first. When a lower precedence operation should be processed first, it should be enclosed within parentheses.

Übersetzt vonDeepL

Try Managed Milvus for Free

Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.

Get Started
Feedback

War diese Seite hilfreich?