索引
如何索引和搜索 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
)的逻辑名称关联。您可以在查询中使用这些属性。
使用以下语法创建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.SET
和JSON.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]}"
有关搜索查询的更多信息,请参见搜索查询语法。
将JSON数组索引为TAG
索引具有多值项的JSON字段的首选方法是使用JSON数组。数组的每个值都会被索引,这些值必须是标量。如果要将字符串或布尔值作为JSON数组中的TAG索引,请使用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运算符(例如通配符、过滤器、联合、数组切片和/或递归下降)指向多个字符串值的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
中指定SLOP
或INORDER
将返回错误,因为与JSONPath匹配的值的顺序没有明确定义,可能导致结果不一致。例如,对JSON值使用诸如
$..b[*]
之类的JSONPath{ "a": [ {"b": ["first first", "first second"]}, {"c": {"b": ["second first", "second second"]}}, {"b": ["third first", "third second"]} ] }
可能匹配各种顺序的值,具体取决于所使用的JSONPath库的具体实现。
由于
SLOP
和INORDER
考虑索引值之间的相对顺序,并且结果可能在未来版本中发生变化,因此将返回错误。 -
当JSONPath导致多个值时
- 字符串值被索引
null
值将被跳过- 任何其他值类型都将导致索引失败
-
SORTBY
仅按第一个值排序 -
不支持
HIGHLIGHT
-
RETURN
一个其JSONPath导致多个值的Schema属性,只返回第一个值(作为JSON字符串) -
如果
RETURN
指定了JSONPath,而不是Schema属性,则将返回所有值(作为JSON字符串)
处理不同数组槽中的短语
索引时,将使用预定义的增量来增加多个文本值之间数组槽的位移偏移量。此增量控制不同数组槽中短语之间的分离程度(与FT.SEARCH
的SLOP
参数相关)。此预定义值由配置参数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开始,可以在地理(地理)值数组或指向多个地理值的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
$.embedding
)对于VECTOR类型不允许您索引存储在该字段下的多个向量。因此,如果您将$.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
关键字指定要包含在搜索结果中的属性。您还需要指定要返回的字段数量。
例如,此查询仅返回每组耳机的name
和price
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
,您必须在RETURN
和HIGHLIGHT
参数之后明确设置要突出显示的属性。
使用可选的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"
索引缺失或空值
您可以搜索缺失的属性(即在给定文档中不存在的属性),方法是将INDEXMISSING
选项与FT.CREATE
结合使用,并结合FT.SEARCH
中的ISMISSING
查询函数。您可以搜索具有空值的现有属性(即空),方法是将INDEXEMPTY
选项与FT.CREATE
结合使用。两种查询类型都需要 DIALECT 2。以下是一些示例
JSON.SET key:1 $ '{"propA": "foo"}'
JSON.SET key:2 $ '{"propA": "bar", "propB":"abc"}'
FT.CREATE idx ON JSON PREFIX 1 key: SCHEMA $.propA AS propA TAG $.propB AS propB TAG INDEXMISSING
> FT.SEARCH idx 'ISMISSING(@propB)' DIALECT 2
1) "1"
2) "key:1"
3) 1) "$"
2) "{\"propA\":\"foo\"}"
JSON.SET key:1 $ '{"propA": "foo", "propB":""}'
JSON.SET key:2 $ '{"propA": "bar", "propB":"abc"}'
FT.CREATE idx ON JSON PREFIX 1 key: SCHEMA $.propA AS propA TAG $.propB AS propB TAG INDEXEMPTY
> FT.SEARCH idx '@propB:{""}' DIALECT 2
1) "1"
2) "key:1"
3) 1) "$"
2) "{\"propA\":\"foo\",\"propB\":\"\"}"
索引限制
模式映射
在创建索引期间,您需要将 JSON 元素映射到SCHEMA
字段,如下所示
- 字符串作为
TEXT
、TAG
或GEO
。 - 数字作为
NUMERIC
。 - 布尔值作为
TAG
。 - JSON 数组
- 字符串数组作为
TAG
或TEXT
。 - 数字数组作为
NUMERIC
或VECTOR
。 - 地理坐标数组作为
GEO
。 - 此类数组中的
null
值将被忽略。
- 字符串数组作为
- 您无法索引 JSON 对象。请改为将各个元素作为单独的属性进行索引。
null
值将被忽略。
可排序标签
如果您为包含指向数组或多个值的 JSONPath 的 JSON 文档创建索引,则排序将只考虑第一个值。