学习

如教程前面所述,Redis Stack 中搜索和查询的目标之一是提供丰富的查询功能,例如

  • 简单和复杂条件
  • 排序
  • 分页
  • 计数

条件#

开始使用 Redis Stack 查询功能的最佳方法是查看各种条件选项。

> FT.SEARCH "idx:movie" "heat" RETURN 2 title plot

1) (integer) 4
2) "movie:1141"
3) 1) "title"
   2) "Heat"
   3) "plot"
   4) "A group of professional bank robbers start to feel the heat from police when they unknowingly leave a clue at their latest heist."
4) "movie:818"
5) 1) "title"
   2) "California Heat"
   3) "plot"
   4) "A lifeguard bets he can be true to just one woman."
6) "movie:736"
7) 1) "title"
   2) "Chicago Justice"
   3) "plot"
   4) "The State's Attorney's dedicated team of prosecutors and investigators navigates heated city politics and controversy head-on,while fearlessly pursuing justice."
8) "movie:1109"
9) 1) "title"
   2) "Love & Hip Hop: Miami"
   3) "plot"
   4) "'Love and Hip Hop Miami' turns up the heat and doesn't hold back in making the 305 the place to be. Multi-platinum selling hip-hop legend Trick Daddy is back in the studio collaborating ..."

第一行包含匹配查询条件的文档数量(4),然后是电影列表。

此查询是“无字段”条件,这意味着查询引擎已

  • 在索引的所有 TEXT 字段(titleplot)中搜索
  • 搜索词 heat 及相关词,这就是返回 movie:736 的原因,因为它在 plot 中包含词 heated词干提取
  • 返回的结果按分数排序,请记住 title 的权重为 1.0,plot 的权重为 0.5。因此,当在 title 中找到该词或相关词时,分数会更高。

在这种情况下,您必须使用 @title 标记将条件设置为 title 字段。

> FT.SEARCH "idx:movie" "@title:heat" RETURN 2 title plot
1) (integer) 2
2) "movie:1141"
3) 1) "title"
   2) "Heat"
   3) "plot"
   4) "A group of professional bank robbers start to feel the heat from police when they unknowingly leave a clue at their latest heist."
4) "movie:818"
5) 1) "title"
   2) "California Heat"
   3) "plot"
   4) "A lifeguard bets he can be true to just one woman."

因此只返回 2 部电影。

查找所有标题包含 'heat' 但不包含 'california' 的电影#

为此,您需要在字段条件周围加上括号,并在 'california' 前加上 - 符号。

> FT.SEARCH "idx:movie" "@title:(heat -california)" RETURN 2 title plot
1) (integer) 1
2) "movie:1141"
3) 1) "title"
   2) "Heat"
   3) "plot"
   4) "A group of professional bank robbers start to feel the heat from police when they unknowingly leave a clue at their latest heist."

只返回了一部电影。

如果您不加上 ( .. ) ,则 -california 条件将应用于所有文本字段。

您可以使用以下查询进行测试

> FT.SEARCH "idx:movie" "@title:(heat -woman)" RETURN 2 title plot
> FT.SEARCH "idx:movie" "@title:heat -woman" RETURN 2 title plot

如您所见,第一个查询只在标题中搜索 woman,返回了两部电影“Heat”和“California Heat”,而第二个查询从列表中排除了“California Heat”,因为情节中包含词 woman

最后更新于 2024 年 2 月 27 日