【问题标题】:Postgres search forPostgres 搜索
【发布时间】:2016-02-28 04:30:45
【问题描述】:

如果提供了文本字符串,例如“Tennis Account A33 Sydney AU” 我需要找到与之匹配的数据库记录。 “A33”部分可以是任何字符。

我为我的 Rails 应用程序创建了一个模型,名为 Descriptor,其中包含名为 pattern 的字符串列。该模式可以包含 % 字符,以表明任何字符都可以在字符串的这个位置。例如"网球账户 % Sydney AU"

因此,如果表中的数据如下,并且我想找到匹配“网球帐户 A33 悉尼 AU”的描述符,它应该返回描述符 1。

#<Descriptor id: 1, pattern: "Tennis Account % Sydney AU", ... >
#<Descriptor id: 2, pattern: "Tennis Account % Melbourne AU", ... >
#<Descriptor id: 3, pattern: "Tennis Account Sydney AU", ... >
#<Descriptor id: 4, pattern: "Jazz Flute Minions dney AU", ... >

......还有数千个描述符

如何在 postgres 中编写这个搜索查询?

【问题讨论】:

    标签: ruby-on-rails regex postgresql search


    【解决方案1】:

    我现在意识到这只是一个相反的 LIKE 查询:

    Descriptor.where('? LIKE pattern', "Tennis Account A33 Sydney AU").first
    

    【讨论】:

      猜你喜欢
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 2017-04-21
      相关资源
      最近更新 更多