索引

如何索引和搜索 JSON 文档

除了索引 Redis 哈希之外,Redis Stack 还可以索引 JSON 文档。

先决条件

在索引和搜索 JSON 文档之前,你需要一个包含以下内容的数据库

  • Redis Stack,它自动包含 JSON 以及搜索和查询功能
  • Redis v6.x 或更高版本,并安装和启用以下模块
    • RediSearch v2.2 或更高版本
    • RedisJSON v2.0 或更高版本

使用 JSON 模式创建索引

使用 FT.CREATE 命令创建索引时,包含 ON JSON 关键字以索引存储在数据库中的任何现有和未来 JSON 文档。

要定义 SCHEMA,你可以提供 JSONPath 表达式。每个 JSONPath 表达式的结果都会被索引,并与一个称为 attribute(以前称为 field)的逻辑名称相关联。你可以在查询中使用这些属性。

注意

注意:对于 FT.CREATEattribute 是可选的。

使用以下语法创建 JSON 索引

FT.CREATE {index_name} ON JSON SCHEMA {json_path} AS {attribute} {type}

例如,此命令创建一个索引,该索引对表示库存项目的每个 JSON 文档的名称、描述、价格和图像向量嵌入进行索引

127.0.0.1:6379> FT.CREATE itemIdx ON JSON PREFIX 1 item: SCHEMA $.name AS name TEXT $.description as description TEXT $.price AS price NUMERIC $.embedding AS embedding VECTOR FLAT 6 DIM 4 DISTANCE_METRIC L2 TYPE FLOAT32

有关 JSON 索引 SCHEMA 限制的更多详细信息,请参阅 索引限制

添加 JSON 文档

创建索引后,Redis Stack 会自动索引存储在数据库中的任何现有、修改或新创建的 JSON 文档。对于现有文档,索引会在后台异步运行,因此文档可用之前可能需要一些时间。修改和新创建的文档会同步索引,因此文档将在添加或修改命令完成时可用。

你可以使用任何 JSON 写入命令(例如 JSON.SETJSON.ARRAPPEND)来创建或修改 JSON 文档。

以下示例使用这些 JSON 文档来表示各个库存项目。

项目 1 JSON 文档

{
  "name": "Noise-cancelling Bluetooth headphones",
  "description": "Wireless Bluetooth headphones with noise-cancelling technology",
  "connection": {
    "wireless": true,
    "type": "Bluetooth"
  },
  "price": 99.98,
  "stock": 25,
  "colors": [
    "black",
    "silver"
  ],
  "embedding": [0.87, -0.15, 0.55, 0.03]
}

项目 2 JSON 文档

{
  "name": "Wireless earbuds",
  "description": "Wireless Bluetooth in-ear headphones",
  "connection": {
    "wireless": true,
    "type": "Bluetooth"
  },
  "price": 64.99,
  "stock": 17,
  "colors": [
    "black",
    "white"
  ],
  "embedding": [-0.7, -0.51, 0.88, 0.14]
}

使用 JSON.SET 将这些文档存储在数据库中

127.0.0.1:6379> JSON.SET item:1 $ '{"name":"Noise-cancelling Bluetooth headphones","description":"Wireless Bluetooth headphones with noise-cancelling technology","connection":{"wireless":true,"type":"Bluetooth"},"price":99.98,"stock":25,"colors":["black","silver"],"embedding":[0.87,-0.15,0.55,0.03]}'
"OK"
127.0.0.1:6379> JSON.SET item:2 $ '{"name":"Wireless earbuds","description":"Wireless Bluetooth in-ear headphones","connection":{"wireless":true,"type":"Bluetooth"},"price":64.99,"stock":17,"colors":["black","white"],"embedding":[-0.7,-0.51,0.88,0.14]}'
"OK"

由于在这种情况下索引是同步的,因此文档将在 JSON.SET 命令返回后立即在索引中可用。任何匹配已编制索引内容的后续查询都将返回文档。

搜索索引

要搜索 JSON 文档的索引,请使用 FT.SEARCH 命令。您可以在 SCHEMA 中定义的任何属性中搜索。

例如,使用此查询搜索名称中包含单词“earbuds”的项目

127.0.0.1:6379> FT.SEARCH itemIdx '@name:(earbuds)'
1) "1"
2) "item:2"
3) 1) "$"
   2) "{\"name\":\"Wireless earbuds\",\"description\":\"Wireless Bluetooth in-ear headphones\",\"connection\":{\"wireless\":true,\"connection\":\"Bluetooth\"},\"price\":64.99,\"stock\":17,\"colors\":[\"black\",\"white\"],\"embedding\":[-0.7,-0.51,0.88,0.14]}"

此查询搜索描述中包含“bluetooth”和“headphones”的所有项目

127.0.0.1:6379> FT.SEARCH itemIdx '@description:(bluetooth headphones)'
1) "2"
2) "item:1"
3) 1) "$"
   2) "{\"name\":\"Noise-cancelling Bluetooth headphones\",\"description\":\"Wireless Bluetooth headphones with noise-cancelling technology\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":99.98,\"stock\":25,\"colors\":[\"black\",\"silver\"], \"embedding\":[0.87,-0.15,0.55,0.03]}"
4) "item:2"
5) 1) "$"
   2) "{\"name\":\"Wireless earbuds\",\"description\":\"Wireless Bluetooth in-ear headphones\",\"connection\":{\"wireless\":true,\"connection\":\"Bluetooth\"},\"price\":64.99,\"stock\":17,\"colors\":[\"black\",\"white\"],\"embedding\":[-0.7,-0.51,0.88,0.14]}"

现在搜索价格低于 70 的蓝牙耳机

127.0.0.1:6379> FT.SEARCH itemIdx '@description:(bluetooth headphones) @price:[0 70]'
1) "1"
2) "item:2"
3) 1) "$"
   2) "{\"name\":\"Wireless earbuds\",\"description\":\"Wireless Bluetooth in-ear headphones\",\"connection\":{\"wireless\":true,\"connection\":\"Bluetooth\"},\"price\":64.99,\"stock\":17,\"colors\":[\"black\",\"white\"],\"embedding\":[-0.7,-0.51,0.88,0.14]}"

最后,搜索与嵌入为 [1.0, 1.0, 1.0, 1.0] 的图像最相似的蓝牙耳机

127.0.0.1:6379> FT.SEARCH itemIdx '@description:(bluetooth headphones)=>[KNN 2 @embedding $blob]' PARAMS 2 blob \x01\x01\x01\x01 DIALECT 2
1) "2"
2) "item:1"
3) 1) "__embedding_score"
   2) "1.08280003071"
   1) "$"
   2) "{\"name\":\"Noise-cancelling Bluetooth headphones\",\"description\":\"Wireless Bluetooth headphones with noise-cancelling technology\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":99.98,\"stock\":25,\"colors\":[\"black\",\"silver\"],\"embedding\":[0.87,-0.15,0.55,0.03]}"
2) "item:2"
3) 1) "__embedding_score"
   2) "1.54409992695"
   3) "$"
   4) "{\"name\":\"Wireless earbuds\",\"description\":\"Wireless Bluetooth in-ear headphones\",\"connection\":{\"wireless\":true,\"connection\":\"Bluetooth\"},\"price\":64.99,\"stock\":17,\"colors\":[\"black\",\"white\"],\"embedding\":[-0.7,-0.51,0.88,0.14]}"

有关搜索查询的更多信息,请参阅 搜索查询语法

注意

FT.SEARCH 查询需要 attribute 修饰符。请勿在查询中使用 JSONPath 表达式,因为查询解析器不支持它们。

将 JSON 数组编制为 TAG

如果您想将字符串或布尔值作为 TAG 在 JSON 数组中编制索引,请使用 JSONPath 通配符运算符。

要编制项目可用颜色列表的索引,请在创建索引期间在 SCHEMA 定义中指定 JSONPath $.colors.*

127.0.0.1:6379> FT.CREATE itemIdx2 ON JSON PREFIX 1 item: SCHEMA $.colors.* AS colors TAG $.name AS name TEXT $.description as description TEXT

现在您可以搜索银色耳机

127.0.0.1:6379> FT.SEARCH itemIdx2 "@colors:{silver} (@name:(headphones)|@description:(headphones))"
1) "1"
2) "item:1"
3) 1) "$"
   2) "{\"name\":\"Noise-cancelling Bluetooth headphones\",\"description\":\"Wireless Bluetooth headphones with noise-cancelling technology\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":99.98,\"stock\":25,\"colors\":[\"black\",\"silver\"]}"

将 JSON 数组编制为 TEXT

从 RediSearch v2.6.0 开始,可以在字符串数组或指向多个字符串的 JSONPath 上执行全文搜索。

如果您想将多个字符串值编制为 TEXT,请使用指向单个字符串数组的 JSONPath,或使用 JSONPath 运算符(例如通配符、过滤器、并集、数组切片和/或递归下降)指向多个字符串值。

要编制项目可用颜色列表的索引,请在创建索引期间在 SCHEMA 定义中指定 JSONPath $.colors

127.0.0.1:6379> FT.CREATE itemIdx3 ON JSON PREFIX 1 item: SCHEMA $.colors AS colors TEXT $.name AS name TEXT $.description as description TEXT
127.0.0.1:6379> JSON.SET item:3 $ '{"name":"True Wireless earbuds","description":"True Wireless Bluetooth in-ear headphones","connection":{"wireless":true,"type":"Bluetooth"},"price":74.99,"stock":20,"colors":["red","light blue"]}'
"OK"

现在您可以对浅色耳机进行全文搜索

127.0.0.1:6379> FT.SEARCH itemIdx3 '@colors:(white|light) (@name|description:(headphones))' RETURN 1 $.colors
1) (integer) 2
2) "item:2"
3) 1) "$.colors"
   2) "[\"black\",\"white\"]"
4) "item:3"
5) 1) "$.colors"
   2) "[\"red\",\"light blue\"]"

限制

  • 当 JSONPath 可能导致多个值而不仅仅是一个数组时,例如,当 JSONPath 包含通配符等时,在 FT.SEARCH 中指定 SLOPINORDER 将返回一个错误,因为匹配 JSONPath 的值的顺序没有明确定义,导致结果可能不一致。

    例如,对如下 JSON 值使用 $..b[*] 等 JSONPath

    {
       "a": [
          {"b": ["first first", "first second"]},
          {"c":
             {"b": ["second first", "second second"]}},
          {"b": ["third first", "third second"]}
       ]
    }
    

    可能会按各种顺序匹配值,具体取决于所用 JSONPath 库的具体实现。

    由于 SLOPINORDER 考虑已编制索引值之间的相对顺序,并且结果可能会在未来版本中发生变化,因此将返回一个错误。

  • 当 JSONPath 导致多个值时

    • 字符串值已编制索引
    • null 值将被跳过
    • 任何其他值类型都会导致索引失败
  • SORTBY 仅按第一个值排序

  • 不支持 HIGHLIGHT

  • RETURN 的模式属性,其 JSONPath 导致多个值,仅返回第一个值(作为 JSON 字符串)

  • 如果 JSONPath 由 RETURN 指定,而不是模式属性,则将返回所有值(作为 JSON 字符串)

处理不同数组槽中的短语

在索引时,使用预定义的增量来增加多个文本值在数组槽之间的位置偏移。此增量控制不同数组槽中短语之间的分隔级别(与 FT.SEARCHSLOP 参数相关)。此预定义值由配置参数 MULTI_TEXT_SLOP(在模块加载时)设置。默认值为 100。

将 JSON 数组索引为 NUMERIC

从 RediSearch v2.6.1 开始,可以在数值数组或导致多个数值的 JSONPath 上进行搜索。

如果要将多个数值作为 NUMERIC 索引,请使用导致单个数字数组的 JSONPath,或使用 JSONPath 运算符(例如通配符、筛选器、并集、数组切片和/或递归下降)导致多个数字的 JSONPath。

例如,将音量(以分贝为单位)的可用 max_level 添加到项目的列表中

127.0.0.1:6379> JSON.SET item:1 $ '{"name":"Noise-cancelling Bluetooth headphones","description":"Wireless Bluetooth headphones with noise-cancelling technology","connection":{"wireless":true,"type":"Bluetooth"},"price":99.98,"stock":25,"colors":["black","silver"], "max_level":[60, 70, 80, 90, 100]}'
OK

127.0.0.1:6379> JSON.SET item:2 $ '{"name":"Wireless earbuds","description":"Wireless Bluetooth in-ear headphones","connection":{"wireless":true,"type":"Bluetooth"},"price":64.99,"stock":17,"colors":["black","white"], "max_level":[80, 100, 120]}'
OK

127.0.0.1:6379> JSON.SET item:3 $ '{"name":"True Wireless earbuds","description":"True Wireless Bluetooth in-ear headphones","connection":{"wireless":true,"type":"Bluetooth"},"price":74.99,"stock":20,"colors":["red","light blue"], "max_level":[90, 100, 110, 120]}'
OK

要索引 max_level 数组,请在索引创建期间在 SCHEMA 定义中指定 JSONPath $.max_level

127.0.0.1:6379> FT.CREATE itemIdx4 ON JSON PREFIX 1 item: SCHEMA $.max_level AS dB NUMERIC
OK

您现在可以搜索具有特定最大音量级别的耳机,例如介于 70 和 80(包括)之间,返回 max_level 数组中至少有一个值在请求范围内且该值在请求范围内的项目

127.0.0.1:6379> FT.SEARCH itemIdx4 '@dB:[70 80]'
1) (integer) 2
2) "item:1"
3) 1) "$"
   2) "{\"name\":\"Noise-cancelling Bluetooth headphones\",\"description\":\"Wireless Bluetooth headphones with noise-cancelling technology\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":99.98,\"stock\":25,\"colors\":[\"black\",\"silver\"],\"max_level\":[60,70,80,90,100]}"
4) "item:2"
5) 1) "$"
   2) "{\"name\":\"Wireless earbuds\",\"description\":\"Wireless Bluetooth in-ear headphones\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":64.99,\"stock\":17,\"colors\":[\"black\",\"white\"],\"max_level\":[80,100,120]}"

您还可以搜索特定范围内所有值的所有项目。例如,所有值都在范围 [90, 120](包括)内

127.0.0.1:6379> FT.SEARCH itemIdx4 '-@dB:[-inf (90] -@dB:[(120 +inf]'
1) (integer) 1
2) "item:3"
3) 1) "$"
   2) "{\"name\":\"True Wireless earbuds\",\"description\":\"True Wireless Bluetooth in-ear headphones\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":74.99,\"stock\":20,\"colors\":[\"red\",\"light blue\"],\"max_level\":[90,100,110,120]}"

限制

当 JSONPath 导致多个数值时

  • 数值已编制索引
  • null 值将被跳过
  • 任何其他值类型都会导致索引失败

将 JSON 数组索引为 GEO

从 RediSearch v2.6.1 开始,可以在地理(geo)值数组或指向多个地理值 JSONPath 上进行搜索。

在 RediSearch v2.6.1 之前,每个 GEO 属性仅支持一个地理值。使用逗号分隔的字符串以“经度,纬度”的形式指定地理值。例如,“15.447083,78.238306”。

使用 RediSearch v2.6.1,还支持此类地理值的 JSON 数组。

为了对多个地理值编制索引,用户可以使用指向单个地理值数组的 JSONPath,或使用 JSONPath 运算符(例如通配符、筛选器、并集、数组切片和/或递归下降)指向多个地理值的 JSONPath。

  • null 值将被跳过
  • 其他值将导致索引失败(布尔值、数字、对象、数组、格式错误的 GEO 字符串、无效坐标)

例如,将 vendor_id(即可以实际购买商品的位置)添加到商品列表中

127.0.0.1:6379> JSON.SET item:1 $ '{"name":"Noise-cancelling Bluetooth headphones","description":"Wireless Bluetooth headphones with noise-cancelling technology","connection":{"wireless":true,"type":"Bluetooth"},"price":99.98,"stock":25,"colors":["black","silver"], "max_level":[60, 70, 80, 90, 100], "vendor_id": [100,300]}'
OK

127.0.0.1:6379> JSON.SET item:2 $ '{"name":"Wireless earbuds","description":"Wireless Bluetooth in-ear headphones","connection":{"wireless":true,"type":"Bluetooth"},"price":64.99,"stock":17,"colors":["black","white"], "max_level":[80, 100, 120], "vendor_id": [100,200]}'
OK

127.0.0.1:6379> JSON.SET item:3 $ '{"name":"True Wireless earbuds","description":"True Wireless Bluetooth in-ear headphones","connection":{"wireless":true,"type":"Bluetooth"},"price":74.99,"stock":20,"colors":["red","light blue"], "max_level":[90, 100, 110, 120], "vendor_id": [100]}'
OK

现在添加一些供应商及其地理位置

127.0.0.1:6379> JSON.SET vendor:1 $ '{"id":100, "name":"Kwik-E-Mart", "location":["35.213,31.785", "35.178,31.768", "35.827,31.984"]}'
OK

127.0.0.1:6379> JSON.SET vendor:2 $ '{"id":200, "name":"Cypress Creek", "location":["34.638,31.79", "34.639,31.793"]}'
OK

127.0.0.1:6379> JSON.SET vendor:3 $ '{"id":300, "name":"Barneys", "location":["34.648,31.817", "34.638,31.806", "34.65,31.785"]}'
OK

要对 vendor_id 数字数组编制索引,请在创建索引期间在 SCHEMA 定义中指定 JSONPath $.vendor_id

127.0.0.1:6379> FT.CREATE itemIdx5 ON JSON PREFIX 1 item: SCHEMA $.vendor_id AS vid NUMERIC
OK

要对 location 地理数组编制索引,请在创建索引期间在 SCHEMA 定义中指定 JSONPath $.location

127.0.0.1:6379> FT.CREATE vendorIdx ON JSON PREFIX 1 vendor: SCHEMA $.location AS loc GEO
OK

现在搜索特定位置附近的供应商。例如,客户位于地理坐标 34.5,31.5,您希望获取距我们位置 40 公里范围内的供应商

127.0.0.1:6379> FT.SEARCH vendorIdx '@loc:[34.5 31.5 40 km]' return 1 $.id
1) (integer) 2
2) "vendor:2"
3) 1) "$.id"
   1) "200"
4) "vendor:3"
5) 1) "$.id"
   1) "300"

现在查找这些供应商提供的产品

127.0.0.1:6379> FT.SEARCH itemIdx5 '@vid:[200 300]'
1) (integer) 2
2) "item:2"
3) 1) "$"
   2) "{\"name\":\"Wireless earbuds\",\"description\":\"Wireless Bluetooth in-ear headphones\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":64.99,\"stock\":17,\"colors\":[\"black\",\"white\"],\"max_level\":[80,100,120],\"vendor_id\":[100,200]}"
4) "item:1"
5) 1) "$"
   2) "{\"name\":\"Noise-cancelling Bluetooth headphones\",\"description\":\"Wireless Bluetooth headphones with noise-cancelling technology\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":99.98,\"stock\":25,\"colors\":[\"black\",\"silver\"],\"max_level\":[60,70,80,90,100],\"vendor_id\":[100,300]}"

将 JSON 数组编制为 VECTOR 索引

从 RediSearch 2.6.0 开始,您可以将指向数字值数组的 JSONPath 作为 VECTOR 类型编制到索引架构中。

例如,假设您的 JSON 项目包含一个矢量嵌入数组,其中每个矢量表示一个产品的图像。要对这些矢量编制索引,请在创建索引期间在架构定义中指定 JSONPath $.embedding

127.0.0.1:6379> FT.CREATE itemIdx5 ON JSON PREFIX 1 item: SCHEMA $.embedding AS embedding VECTOR FLAT 6 DIM 4 DISTANCE_METRIC L2 TYPE FLOAT32
OK
127.0.0.1:6379> JSON.SET item:1 $ '{"name":"Noise-cancelling Bluetooth headphones","description":"Wireless Bluetooth headphones with noise-cancelling technology","price":99.98,"stock":25,"colors":["black","silver"],"embedding":[0.87,-0.15,0.55,0.03]}'
OK
127.0.0.1:6379> JSON.SET item:2 $ '{"name":"Wireless earbuds","description":"Wireless Bluetooth in-ear headphones","price":64.99,"stock":17,"colors":["black","white"],"embedding":[-0.7,-0.51,0.88,0.14]}'
OK

现在,您可以使用矢量搜索 KNN 查询搜索与图像嵌入最相似的两个耳机。(请注意,矢量查询从方言 2 开始支持。)例如

127.0.0.1:6379> FT.SEARCH itemIdx5 '*=>[KNN 2 @embedding $blob AS dist]' SORTBY dist PARAMS 2 blob \x01\x01\x01\x01 DIALECT 2
1) (integer) 2
2) "item:1"
3) 1) "dist"
   2) "1.08280003071"
   3) "$"
   4) "{\"name\":\"Noise-cancelling Bluetooth headphones\",\"description\":\"Wireless Bluetooth headphones with noise-cancelling technology\",\"price\":99.98,\"stock\":25,\"colors\":[\"black\",\"silver\"],\"embedding\":[0.87,-0.15,0.55,0.03]}"
4) "item:2"
5) 1) "dist"
   2) "1.54409992695"
   3) "$"
   4) "{\"name\":\"Wireless earbuds\",\"description\":\"Wireless Bluetooth in-ear headphones\",\"price\":64.99,\"stock\":17,\"colors\":[\"black\",\"white\"],\"embedding\":[-0.7,-0.51,0.88,0.14]}"

如果您想将多个数字数组编制为 VECTOR,请使用 JSONPath,并使用 JSONPath 运算符(例如通配符、筛选器、并集、数组切片和/或递归下降)指向多个数字数组。

例如,假设您的 JSON 项包含一个矢量嵌入数组,其中每个矢量表示同一产品的不同图像。要索引这些矢量,请在索引创建期间在模式定义中指定 JSONPath $.embeddings[*]

127.0.0.1:6379> FT.CREATE itemIdx5 ON JSON PREFIX 1 item: SCHEMA $.embeddings[*] AS embeddings VECTOR FLAT 6 DIM 4 DISTANCE_METRIC L2 TYPE FLOAT32
OK
127.0.0.1:6379> JSON.SET item:1 $ '{"name":"Noise-cancelling Bluetooth headphones","description":"Wireless Bluetooth headphones with noise-cancelling technology","price":99.98,"stock":25,"colors":["black","silver"],"embeddings":[[0.87,-0.15,0.55,0.03]]}'
OK
127.0.0.1:6379> JSON.SET item:2 $ '{"name":"Wireless earbuds","description":"Wireless Bluetooth in-ear headphones","price":64.99,"stock":17,"colors":["black","white"],"embeddings":[[-0.7,-0.51,0.88,0.14],[-0.8,-0.15,0.33,-0.01]]}'
OK
重要提示

与 NUMERIC 类型不同,在 VECTOR 类型的模式中设置静态路径(如 $.embedding)不允许您索引存储在该字段下的多个矢量。因此,如果您将 $.embedding 设置为索引模式的路径,则在 JSON 中的 embedding 字段中指定矢量数组将导致索引失败。

现在,您可以使用矢量搜索 KNN 查询来搜索与图像嵌入最相似的两个耳机。(请注意,从方言 2 开始支持矢量查询。)文档到查询矢量的距离被定义为查询矢量到与模式中指定的 JSONPath 匹配的矢量之间的最小距离。例如

127.0.0.1:6379> FT.SEARCH itemIdx5 '*=>[KNN 2 @embeddings $blob AS dist]' SORTBY dist PARAMS 2 blob \x01\x01\x01\x01 DIALECT 2
1) (integer) 2
2) "item:2"
3) 1) "dist"
   2) "0.771500051022"
   3) "$"
   4) "{\"name\":\"Wireless earbuds\",\"description\":\"Wireless Bluetooth in-ear headphones\",\"price\":64.99,\"stock\":17,\"colors\":[\"black\",\"white\"],\"embeddings\":[[-0.7,-0.51,0.88,0.14],[-0.8,-0.15,0.33,-0.01]]}"
4) "item:1"
5) 1) "dist"
   2) "1.08280003071"
   3) "$"
   4) "{\"name\":\"Noise-cancelling Bluetooth headphones\",\"description\":\"Wireless Bluetooth headphones with noise-cancelling technology\",\"price\":99.98,\"stock\":25,\"colors\":[\"black\",\"silver\"],\"embeddings\":[[0.87,-0.15,0.55,0.03]]}"

请注意,0.771500051022 是查询矢量与 [-0.8,-0.15,0.33,-0.01](嵌入数组中的第二个元素)之间的 L2 距离,它低于查询矢量与 [-0.7,-0.51,0.88,0.14](嵌入数组中的第一个元素)之间的 L2 距离。

有关矢量相似性语法的更多信息,请参阅矢量字段

索引 JSON 对象

您无法索引 JSON 对象。如果 JSONPath 表达式返回一个对象,它将被忽略。

要索引 JSON 对象的内容,您需要在单独的属性中索引对象内的各个元素。

例如,要索引 connection JSON 对象,请在创建索引时将 $.connection.wireless$.connection.type 字段定义为单独的属性

127.0.0.1:6379> FT.CREATE itemIdx3 ON JSON SCHEMA $.connection.wireless AS wireless TAG $.connection.type AS connectionType TEXT
"OK"

创建新索引后,您可以搜索将无线 TAG 设置为 true 的项目

127.0.0.1:6379> FT.SEARCH itemIdx3 '@wireless:{true}'
1) "2"
2) "item:2"
3) 1) "$"
   2) "{\"name\":\"Wireless earbuds\",\"description\":\"Wireless Bluetooth in-ear headphones\",\"connection\":{\"wireless\":true,\"connection\":\"Bluetooth\"},\"price\":64.99,\"stock\":17,\"colors\":[\"black\",\"white\"]}"
4) "item:1"
5) 1) "$"
   2) "{\"name\":\"Noise-cancelling Bluetooth headphones\",\"description\":\"Wireless Bluetooth headphones with noise-cancelling technology\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":99.98,\"stock\":25,\"colors\":[\"black\",\"silver\"]}"

您还可以搜索具有蓝牙连接类型的项目

127.0.0.1:6379> FT.SEARCH itemIdx3 '@connectionType:(bluetooth)'
1) "2"
2) "item:1"
3) 1) "$"
   2) "{\"name\":\"Noise-cancelling Bluetooth headphones\",\"description\":\"Wireless Bluetooth headphones with noise-cancelling technology\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":99.98,\"stock\":25,\"colors\":[\"black\",\"silver\"]}"
4) "item:2"
5) 1) "$"
   2) "{\"name\":\"Wireless earbuds\",\"description\":\"Wireless Bluetooth in-ear headphones\",\"connection\":{\"wireless\":true,\"type\":\"Bluetooth\"},\"price\":64.99,\"stock\":17,\"colors\":[\"black\",\"white\"]}"

字段投影

FT.SEARCH 默认返回整个 JSON 文档。如果您想将返回的搜索结果限制为特定属性,可以使用字段投影。

返回特定属性

运行搜索查询时,可以使用 RETURN 关键字指定要在搜索结果中包含哪些属性。您还需要指定要返回的字段数。

例如,此查询仅返回每组耳机的nameprice

127.0.0.1:6379> FT.SEARCH itemIdx '@description:(headphones)' RETURN 2 name price
1) "2"
2) "item:1"
3) 1) "name"
   2) "Noise-cancelling Bluetooth headphones"
   3) "price"
   4) "99.98"
4) "item:2"
5) 1) "name"
   2) "Wireless earbuds"
   3) "price"
   4) "64.99"

使用 JSONPath 进行投影

可以在RETURN语句中使用JSONPath表达式来提取 JSON 文档的任何部分,即使该字段未在索引SCHEMA中定义。

例如,以下查询使用 JSONPath 表达式$.stock来返回每个商品的库存,以及名称和价格属性。

127.0.0.1:6379> FT.SEARCH itemIdx '@description:(headphones)' RETURN 3 name price $.stock
1) "2"
2) "item:1"
3) 1) "name"
   2) "Noise-cancelling Bluetooth headphones"
   3) "price"
   4) "99.98"
   5) "$.stock"
   6) "25"
4) "item:2"
5) 1) "name"
   2) "Wireless earbuds"
   3) "price"
   4) "64.99"
   5) "$.stock"
   6) "17"

请注意,返回的属性名称是 JSONPath 表达式本身:"$.stock"

可以使用AS选项来指定返回的属性的别名

127.0.0.1:6379> FT.SEARCH itemIdx '@description:(headphones)' RETURN 5 name price $.stock AS stock
1) "2"
2) "item:1"
3) 1) "name"
   2) "Noise-cancelling Bluetooth headphones"
   3) "price"
   4) "99.98"
   5) "stock"
   6) "25"
4) "item:2"
5) 1) "name"
   2) "Wireless earbuds"
   3) "price"
   4) "64.99"
   5) "stock"
   6) "17"

此查询将字段作为别名"stock"返回,而不是 JSONPath 表达式"$.stock"

突出显示搜索词

可以在任何已编入索引的TEXT属性中突出显示相关的搜索词。

对于FT.SEARCH,必须在RETURNHIGHLIGHT参数之后明确设置要突出的属性。

使用可选的TAGS关键字来指定将包围(或突出显示)匹配搜索词的字符串。

例如,使用粗体 HTML 标签突出显示商品名称和描述中的单词“bluetooth”

127.0.0.1:6379> FT.SEARCH itemIdx '(@name:(bluetooth))|(@description:(bluetooth))' RETURN 3 name description price HIGHLIGHT FIELDS 2 name description TAGS '<b>' '</b>'
1) "2"
2) "item:1"
3) 1) "name"
   2) "Noise-cancelling <b>Bluetooth</b> headphones"
   3) "description"
   4) "Wireless <b>Bluetooth</b> headphones with noise-cancelling technology"
   5) "price"
   6) "99.98"
4) "item:2"
5) 1) "name"
   2) "Wireless earbuds"
   3) "description"
   4) "Wireless <b>Bluetooth</b> in-ear headphones"
   5) "price"
   6) "64.99"

使用 JSONPath 聚合

可以使用聚合来生成统计信息或构建分面查询。

LOAD选项接受JSONPath表达式。可以使用管道中的任何值,即使该值未编入索引。

此示例使用聚合来计算每个商品的 10% 价格折扣,并按从最便宜到最贵的顺序对商品进行排序

127.0.0.1:6379> FT.AGGREGATE itemIdx '*' LOAD 4 name $.price AS originalPrice APPLY '@originalPrice - (@originalPrice * 0.10)' AS salePrice SORTBY 2 @salePrice ASC
1) "2"
2) 1) "name"
   2) "Wireless earbuds"
   3) "originalPrice"
   4) "64.99"
   5) "salePrice"
   6) "58.491"
3) 1) "name"
   2) "Noise-cancelling Bluetooth headphones"
   3) "originalPrice"
   4) "99.98"
   5) "salePrice"
   6) "89.982"
注意

FT.AGGREGATE查询需要attribute修饰符。不要在查询中使用 JSONPath 表达式,除非使用LOAD选项,因为查询解析器不支持它们。

索引限制

模式映射

在创建索引期间,需要将 JSON 元素映射到SCHEMA字段,如下所示

  • 字符串作为TEXTTAGGEO
  • 数字作为NUMERIC
  • 布尔值作为TAG
  • JSON 数组
    • 字符串数组作为TAGTEXT
    • 数字数组作为NUMERICVECTOR
    • GEO 形式表示的地理坐标数组。
    • 此类数组中的 null 值将被忽略。
  • 无法对 JSON 对象进行索引。应将各个元素作为单独的属性进行索引。
  • null 值将被忽略。

可排序标签

如果为具有指向数组或多个值的 JSONPath 的 JSON 文档创建索引,则排序时仅考虑第一个值。

对本页进行评分