在 sphinx 中将布尔字段索引为字符串

发布于 2024-08-18 23:48:48 字数 521 浏览 4 评论 0原文

我有 一个 Ruby on Rails 站点,它使用 Thinking sphinx 来搜索 postgres 数据库。

我正在搜索的表中的字段之一是布尔值。 当搜索查询中使用某个关键字时,我希望能够匹配该布尔值。

用一个例子来充分解释:
我的网站是为那些开发自己的黑白胶片的人提供的。
我有一个配方表,人们在其中描述他们如何冲洗电影。该表有一个名为“stand_development”的布尔列(一种冲洗胶片的方法)。
当用户搜索“stand”一词时,我想返回该字段为 true 的结果。

我已经浏览过 sphinx 文档,但没有真正找到是否可能。

我想我可以通过解析查询术语并添加条件来破解我的控制器方法中的某些内容,但是有没有更干净的方法来做到这一点?

I have a Ruby on Rails site that uses thinking sphinx for searching a postgres database.

One of the fields in a table I am searching on is a boolean.
I'd like to be able to match on that boolean when a certain keyword is used in the search query.

To fully explain with an example:
My site is for people who develop their own black and white film.
I have a recipe table where people describe how they develop a film. That table has a boolean column called "stand_developed" (a method of developing film).
I'd like to return results where that field is true when the user searches for the word "stand".

I've been through the sphinx docs and not really found if it's possible.

I guess I could hack something inside my controller method by parsing the query terms and adding a condition but is there a cleaner way of doing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笔芯 2024-08-25 23:48:48

这就是我使用 ThinkingSphinx 搜索布尔字段时所做的事情。通过以下方式将 stand_development 作为 URL 参数与您的 query_string 一起传递:

  1. stand_development 上不进行搜索的一般查询的 URL 将为 http://yoursite.com /search?q=your_query_string
  2. 使用 stand_development == TRUE 进行查询的 URL 将为 http://yoursite.com/search?q=your_query_string&stand_development=1 >
  3. 使用 stand_development == FALSE 进行查询的 URL 将为 http://yoursite.com/search?q=your_query_string&stand_development=0

然后,在您的控制器中,您将这样做:

if params[:stand_developed]  && params[:stand_developed].to_i == 1
  # perform query search with stand_developed == true
  @search_results = YourModel.search(params[:q], :with => {:stand_developed => true})
elsif params[:stand_developed]  && params[:stand_developed].to_i == 0
  # perform query search with stand_developed == false
  @search_results = YourModel.search(params[:q], :with => {:stand_developed => false})
else
  # perform general query search
  @search_results = YourModel.search(params[:q])
end

This is what I've done as far as searching on boolean fields using ThinkingSphinx. Pass stand_developed as a URL parameter along with your query_string in the following ways:

  1. URL for a general query without search on stand_developed will be http://yoursite.com/search?q=your_query_string
  2. URL for query with stand_developed == TRUE will be http://yoursite.com/search?q=your_query_string&stand_developed=1
  3. URL for query with stand_developed == FALSE will be http://yoursite.com/search?q=your_query_string&stand_developed=0

Then, in your controller, you would do this:

if params[:stand_developed]  && params[:stand_developed].to_i == 1
  # perform query search with stand_developed == true
  @search_results = YourModel.search(params[:q], :with => {:stand_developed => true})
elsif params[:stand_developed]  && params[:stand_developed].to_i == 0
  # perform query search with stand_developed == false
  @search_results = YourModel.search(params[:q], :with => {:stand_developed => false})
else
  # perform general query search
  @search_results = YourModel.search(params[:q])
end
独木成林 2024-08-25 23:48:48

您可以刚刚测试 params[:search] 是否包含文本“stand”,然后从那里进行搜索。您的表中不需要额外的列,这只是不需要的开销。

if params[:search].downcase.include?('stand')
  Model.search params[:search], :with => {:stand_developed => true}
else
  Model.search params[:search]
end

You could have just tested if params[:search] included the text 'stand' then done the searching from there. You don't need an extra column in your table, that's just overhead that's not needed.

if params[:search].downcase.include?('stand')
  Model.search params[:search], :with => {:stand_developed => true}
else
  Model.search params[:search]
end
向地狱狂奔 2024-08-25 23:48:48

我现在已经想出了一个解决方案。

我尝试了我在问题中提到的“黑客” - 解析“立场”一词并明确搜索它(保罗·戴维斯答案的变体),但效果不佳。

我没有在我的问题中解释这一点(在我提出问题时没有意识到完整的含义),但如果用户在食谱描述中也使用了“stand”一词,我也需要它匹配。

因此,我试图让 sphinx 添加一个条件,类似于“如果stand_development 为真或注释包含'stand'”,但我似乎找不到正确的语法。
它还必须处理任何其他搜索文本。

最后,我在食谱表中添加了一个名为“search_tags”的额外列,如果用户在添加食谱时选择“stand_development”,我会在其中添加“stand”一词。
然后,我让 Sphinx 对该字段以及其他字段建立索引,一切都完美。

I've come up with a solution to this now.

I tried the "hack" I mentioned in my question - parsing the word "stand" and searching for it explicitly (a variation on the answer from Paul Davis) but it didn't work very well.

I didn't explain this in my question (didn't realise the full implications at the time I asked) but I need it to also match if the user used the word "stand" in their recipe description too.

So, I tried to get sphinx to add a condition along the lines of "if stand_developed is true or notes contains 'stand'" but I couldn't seem to find the right syntax for that.
It also had to deal with any other search text too.

In the end I added an extra column to my recipe table called "search_tags" and I add the word "stand" into it if the user selects "stand_developed" when adding a recipe.
I then get Sphinx to index that field as well as my other fields and it all works perfectly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文