Thinking Sphinx: matching_fields method returning nil
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 resul开发者_Go百科t 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!
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.
精彩评论