Searchkick 和正则表达式

Searchkick and Regular Expressions

我正在尝试构建高级搜索选项(类似于 Twitter's). Users can enter the words included, words excluded, words containing, exact phrase, and such in a search query.

我正在使用 Searchkick 来执行此操作。特殊性 Searckick's regexp 搜索。

这是我正在做的事情,以找到在其口号中包含 "Facebook or less" 字样的公司。

Company.search("Be", where: { short_desc: /.*(#{ar}).*/ })

这很好用。但是,我要如何否定此搜索?

做类似 Company.search("Be", where: { short_desc: /.*(?!(#{ar})).*/ }) 的事情不会产生结果。另外,我可以结合包含要包含的词和要排除的词的搜索吗?

也许这对你有帮助,试试这个:

Business.search("Be", where: { short_desc: {not: /.*(#{ar}).*/} })

并检查结果。您可以将其与 ANDOR 运算符结合使用。使用 where 子句的任何地方。从 gem 自述文件中提取:

where: {
  expires_at: {gt: Time.now}, # lt, gte, lte also available
  orders_count: 1..10,        # equivalent to {gte: 1, lte: 10}
  aisle_id: [25, 30],         # in
  store_id: {not: 2},         # not
  aisle_id: {not: [25, 30]},  # not in
  user_ids: {all: [1, 3]},    # all elements in array
  category: /frozen .+/,      # regexp
  or: [
    [{in_stock: true}, {backordered: true}]
  ]
}

这是将它与查询结合起来的方法。

希望这对您有所帮助。