条件过滤检索

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

免费体验阿里云高性能向量检索服务https://www.aliyun.com/product/ai/dashvector


背景介绍

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

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

条件过滤检索示例

说明

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


插入带有Field的数据

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


说明

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


通过filter进行条件过滤检索

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)


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)



向量检索服务 DashVector 免费试用进行中,玩转大模型搜索,快来试试吧~

了解更多信息,请点击:https://www.aliyun.com/product/ai/dashvector

相关文章
|
10月前
接口数据多条件搜索(模糊查询)
接口数据多条件搜索(模糊查询)
163 0
|
自然语言处理 索引
ES 匹配多个搜索条件和精确查询
ES 匹配多个搜索条件和精确查询
|
11天前
|
缓存 索引
7.过滤查询
7.过滤查询
|
5月前
有关筛选条件的问题
有关筛选条件的问题
23 0
|
10月前
|
JavaScript
js多条件筛选(可单条件搜索还可以模糊查询)
js多条件筛选(可单条件搜索还可以模糊查询)
179 0
|
10月前
|
数据采集 数据可视化 数据挖掘
如何筛选和过滤ARWU网站上的大学排名数据
ARWU网站(ShanghaiRanking's Academic Ranking of World Universities)是一个公认的全球大学排名的先驱和最值得信赖的大学排名之一。它每年发布世界前1000所研究型大学的排名,基于透明的方法论和客观的第三方数据。ARWU网站上的大学排名数据可以为高考考生、专业选择、就业指导、行业发展等提供有价值的参考信息。
如何筛选和过滤ARWU网站上的大学排名数据
odoo 为可编辑列表视图字段搜索添加查询过滤条件
odoo 为可编辑列表视图字段搜索添加查询过滤条件
167 0
|
关系型数据库 MySQL
ES复杂操作-布尔值查询(多条件精确查询)
ES复杂操作-布尔值查询(多条件精确查询)
|
Python
一日一技:如何对数据进行过滤
一日一技:如何对数据进行过滤
340 0
一日一技:如何对数据进行过滤