在 sphinx 中将布尔字段索引为字符串
我有 一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是我使用 ThinkingSphinx 搜索布尔字段时所做的事情。通过以下方式将
stand_development
作为 URL 参数与您的 query_string 一起传递:stand_development
上不进行搜索的一般查询的 URL 将为http://yoursite.com /search?q=your_query_string
stand_development == TRUE
进行查询的 URL 将为http://yoursite.com/search?q=your_query_string&stand_development=1
>stand_development == FALSE
进行查询的 URL 将为http://yoursite.com/search?q=your_query_string&stand_development=0
然后,在您的控制器中,您将这样做:
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:stand_developed
will behttp://yoursite.com/search?q=your_query_string
stand_developed == TRUE
will behttp://yoursite.com/search?q=your_query_string&stand_developed=1
stand_developed == FALSE
will behttp://yoursite.com/search?q=your_query_string&stand_developed=0
Then, in your controller, you would do this:
您可以刚刚测试 params[:search] 是否包含文本“stand”,然后从那里进行搜索。您的表中不需要额外的列,这只是不需要的开销。
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.
我现在已经想出了一个解决方案。
我尝试了我在问题中提到的“黑客” - 解析“立场”一词并明确搜索它(保罗·戴维斯答案的变体),但效果不佳。
我没有在我的问题中解释这一点(在我提出问题时没有意识到完整的含义),但如果用户在食谱描述中也使用了“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.