You need to enable JavaScript to run this app.
导航

search_by_text

最近更新时间2024.04.16 13:11:51

首次发布时间2023.11.09 18:59:55

概述

search_by_text 用于非结构化数据检索。非结构化数据检索是指向量数据库支持非结构化原始数据,可以直接通过文本搜索文本。当用户通过文本搜索时,向量数据库通过测量文本之间的距离来确定两段文本的相似程度,返回文本的相似度。该功能适用于重复识别、文本搜索与匹配、问答等场景。

说明

  • 当前仅支持文本类型的非结构化数据。
  • Collection 数据写入/删除后,Index 数据更新时间最长滞后 20s,不能立即在 Index 检索到。

前提条件
  • 通过 create_collection 接口创建数据集时,定义字段 fields 已添加带 pipeline_name 的 text 字段。
  • 通过 upsert_data 接口写入数据时,已写入带 pipeline_name 的 text 类型的字段名称和字段值。
  • 通过 create_index 创建索引时,已创建 vector_index 向量索引。

请求参数

参数

类型

是否必选

默认值

参数说明

text

string

检索的输入文本。

filter

map

过滤条件,详见 filter 表达式说明。

  • 默认为空,不做过滤。
  • 过滤条件包含 must、must_not、range、range_out、georange 五类查询算子,包含 and 和 or 两种对查询算子的组合。

limit

int

10

检索结果数量,最大5000个。

output_fields

list<string>

过滤字段,指定要返回的标量或向量字段列表。

  • output_fields 不传时,返回所有的标量字段,不返回向量字段。
  • output_fields 为空列表时,不返回 fields 字段。
  • output_fields 格式错误或者过滤字段不是 collection 里的字段时, 接口返回错误。

如果索引的距离方式为cosine,向量字段返回的向量是归一化后的向量。

partition

string/int

"default"

子索引名称,类型与 partition_by 的 field_type 一致,字段值对应 partition_by 的 field_value。

  • field_type 为 int64,list<int64> 时,partition 输入类型为 int64。
  • field_type 为 string,list<string> 时,partition 输入类型为 string,格式要求 "^[a-zA-Z0-9._]+$"。

dense_weight

float

0.5

混合检索中稠密向量的权重,1 表示纯稠密检索 ,0表示纯字面检索。
只有索引是混合索引时有效。范围 [0.2, 1]

filter 表达式

算子

算子说明

示例

must

针对指定字段名生效,语义为必须在 [...] 之中,即 "must in"。

{
  "op": "must",
  "field": "region",
  "conds": ["cn", "sg"]
}

must_not

针对指定字段名生效,语义为必须不在 [...] 之中,即 "must not in"。

{
  "op": "must_not",
  "field": "data_type",
  "conds": [1,2,3]
}

range

针对指定字段名生效,语义为必须在指定范围内。
配置使用gte(大于等于), gt(大于), lte(小于等于), lt(小于),用以圈定一维范围。
另外,支持用 centerradius 表示二维圆内范围。

// price 在 [100.0, 500.0)
{
  "op": "range",
  "field": "price",
  "gte": 100.0,
  "lt": 500.0
}

//price >= 100.0
{
  "op": "range",
  "field": "price",
  "gte": 100.0
}

// 以 center 为中心,半径为50的圆内
{
  "op": "range",
  "field": ["pos_x", "pos_y"],
  "center": [100.0, 123.4],
  "radius": 50.0
}

range_out

针对指定字段名生效,语义为必须在指定范围外。配置使用gte(大于等于), gt(大于), lte(小于等于), lt(小于),用以圈定一维范围。

// 筛选价格低于100或高于500的商品
{
  "op": "range_out",
  "field": "price",
  "gt": 500.0,
  "lt": 100.0
}

georange

支持地理距离范围筛选 。
指定经纬度字段,以center为中心,筛选出地表距离在radius范围内的数据。

// 距离center地表距离 radius 内
{
  "op": "georange",
  "field": ["longitude", "latitude"],
  "center": [100.12312, 22.4324],
  "radius": 50.0
}

and

逻辑算子,针对逻辑查询需求,对多个条件取交集。

{
  "op": "and",   // 算子名
  "conds": [     // 条件列表,支持嵌套逻辑算子和 must/must_not 算子
    {
      "op": "must",
      "field": "type",
      "conds": [1]
    },
    {
        ...         // 支持>=1的任意数量的条件进行组合
    }
  ]
}

or

逻辑算子,针对逻辑查询需求,对多个条件取并集。

{
  "op": "or",   // 算子名
  "conds": [    // 条件列表,支持嵌套逻辑算子和 must/must_not 算子
    {
      "op": "must",
      "field": "type",
      "conds": [1]
    },
    {
        ...      // 支持>=1的任意数量的条件进行组合
    }
  ]
}

示例

请求参数

index = vikingdb_service.get_index("example_text", "example_index_text")
res = index.search_by_text(Text(text="this is five"), filter={"op": "range", "field": "price", "lt": 4}, limit=3, output_fields=["doc_id", "text", "price", "like"], partition=1)

返回值

Python 调用执行上面的任务,返回 List<Data> 。Data 实例包含的属性如下表所示。

属性

说明

id

主键 id。

fields

请求返回中的 fields 字段,是具体的数据,字典类型。

score

表示找到的向量和输入的向量的匹配程度。

text

文本非结构化检索时返回。