背景介绍

在大多数业务场景中,单纯使用向量进行相似性检索并无法满足业务需求,通常需要在满足特定过滤条件、或者特定的“标签”的前提下,再进行相似性检索。

向量检索服务DashVector支持条件过滤和向量相似性检索相结合,在精确满足过滤条件的前提下进行高效的向量检索。


条件过滤检索示例

说明

  1. 需要使用您的api-key替换示例中的 YOUR_API_KEY、您的Cluster Endpoint替换示例中的YOUR_CLUSTER_ENDPOINT,代码才能正常运行。
  2. 本示例需要参考 新建Collection-使用示例提前创建好名称为quickstart的Collection。


插入带有Field的数据

Python:

import dashvector
import numpy as np

client = dashvector.Client(
    api_key='YOUR_API_KEY',
    endpoint='YOUR_CLUSTER_ENDPOINT'
)
collection = client.get(name='quickstart')

ret = collection.insert([
    ('1', np.random.rand(4), {'name':'zhangsan', 'age': 10, 'male': True, 'weight': 35.0}),
    ('2', np.random.rand(4), {'name':'lisi', 'age': 20, 'male': False, 'weight': 45.0}),
    ('3', np.random.rand(4), {'name':'wangwu', 'age': 30, 'male': True, 'weight': 75.0}),
    ('4', np.random.rand(4), {'name':'zhaoliu', 'age': 5, 'male': False, 'weight': 18.0}),
    ('5', np.random.rand(4), {'name':'sunqi', 'age': 40, 'male': True, 'weight': 70.0})
])
assert ret
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

说明

 新建Collection-使用示例中,创建了名称为quickstart的Collection,该Collection定义了3个Field({'name': str, 'weight': float, 'age': int})。DashVector具有 Schema Free的特性,因此可以在 插入Doc时,随意指定创建Collection时未定义的Field,如上述示例中的maleField。

通过filter进行条件过滤检索

Python:

import dashvector

client = dashvector.Client(
    api_key='YOUR_API_KEY',
    endpoint='YOUR_CLUSTER_ENDPOINT'
)
collection = client.get(name='quickstart')

# 要求年龄(age)大于18,并且体重(weight)大于65.0的男性(male=true)
docs = collection.query(
  [0.1, 0.1, 0.1, 0.1],
  topk=10,
  filter = 'age > 18 and weight > 65.0 and male = true'
)
print(docs)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

DashVector支持的数据类型

当前DashVector支持Python的4种基础数据类型:

  • str
  • float
  • int
  • bool

重要

Python的int类型可表达无限大小的整数,当前DashVector仅支持32位整数,范围为-2,147,483,648~2,147,483,647,需要用户自行保证数据未溢出。

比较运算符

通过Field 比较运算符 常量的组合生成比较表达式,说明及示例如下:

符号

描述

支持数据类型

表达式示例

示例解释

<

小于

  • int
  • float
  • age < 10
  • weight < 60.0
  • age小于10则为True
  • weight小于60.0则为True

<=

小于或等于

  • int
  • float
  • age <= 10
  • weight <= 60.0
  • age小于或等于10则为True
  • weight小于或等于60.0则为True

=

等于

  • int
  • float
  • bool
  • str
  • age = 10
  • weight = 60.0
  • male = true
  • name = 'lisi'
  • age等于10则为True
  • weight等于60.0则为True
  • male等于true则为True
  • name等于lisi则为True

!=

不等于

  • int
  • float
  • bool
  • str
  • age != 10
  • weight != 60.0
  • male != true
  • name != 'lisi'
  • age不等于10则为True
  • weight不等于60.0则为True
  • male不等于true则为True
  • name不等于lisi则为True

>=

大于或等于

  • int
  • float
  • age >= 10
  • weight >= 60.0
  • age大于或等于10则为True
  • weight大于或等于60.0则为True

>

大于

  • int
  • float
  • age > 10
  • weight > 60.0
  • age大于10则为True
  • weight大于60.0则为True

字符串运算符

通过Field 字符串运算符 常量的组合生成匹配表达式,说明及示例如下:

符号

描述

支持数据类型

表达式示例

示例解释

like

前缀匹配

  • str
  • name like 'li%'
  • name以li开头则为True

逻辑运算符

逻辑运算符用于组合多个表达式。

符号

描述

示例

示例解释

and

expr1 and expr2

expr1、expr2同时为True时则为True,否则False

or

expr1 or expr2

expr1、expr2同时为False时则为False,否则True

说明

可通过括号()组合逻辑运算符,()拥有更高优先级,如:expr1 and (expr2 or expr3),会优先计算(expr2 or expr3)