思考 Sphinx:matching_fields 方法返回 nil
我在 Rails 3.0 应用程序中使用 Thinking Sphinx,并尝试利用“excerpts”和“matching_fields”方法来呈现搜索结果。假设我有以下模型:
class Journal < ActiveRecord::Base
has_many :entries
define_index do
indexes description # This is an attribute of the Journal class
indexes entries.note, :as => :entry_note
# ...additional indexes
set_property :delta => true
end
end
在搜索控制器中,我有以下内容:
class SearchResultsController < ApplicationController
def index
@search_results = Journal.search params[:q], :star => true, :match_mode => :fieldmask
respond_with(@search_results)
end
end
在我看来,我想构建一个搜索结果,其中仅包含与搜索词匹配的字段的摘录。例如,如果搜索词与 :description 字段匹配,我想显示描述的摘录,并突出显示搜索词。但是,如果搜索与期刊条目的注释之一(:entry_note 字段)匹配,我希望搜索结果显示该注释的摘录,并突出显示搜索词。
我已阅读有关摘录的内容,以及这关于matching_fields,但是,matching_fields方法总是返回nil,我还没有能够找到它的其他文档(甚至在源代码中)。 matching_fields 应该返回什么?
谢谢你!
I'm using Thinking Sphinx in a Rails 3.0 application and am trying to take advantage of the "excerpts" and "matching_fields" methods in rendering the search results. Say I have the following model:
class Journal < ActiveRecord::Base
has_many :entries
define_index do
indexes description # This is an attribute of the Journal class
indexes entries.note, :as => :entry_note
# ...additional indexes
set_property :delta => true
end
end
In a search controller I have the following:
class SearchResultsController < ApplicationController
def index
@search_results = Journal.search params[:q], :star => true, :match_mode => :fieldmask
respond_with(@search_results)
end
end
In my view I would like to construct a search result that includes an excerpt of only the field which matched the search term. For example, if the search term matched the :description field, I would like to display an excerpt of the description with the search term highlighted. However, if the search matched one of the journal's entry's note (:entry_note field), I would like the search result to display an excerpt of that note with the search term highlighted.
I've read this regarding excerpts, and this regarding matching_fields, however, the matching_fields methods is always returning nil and I haven't been able to find other documentation for it (even in the source code). What is matching_fields supposed to return?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Matching_fields 返回包含字段名称的字符串数组。应该对每个特定的搜索结果调用它。要使其正常工作,您需要将 :rank_mode 设置为 :fieldmask,而不是 :match_mode。
Matching_fields returns an array of strings with the field names. It should be called on each particular search result. To get it to work, you need to set :rank_mode to :fieldmask, not :match_mode.