Search code examples
ruby-on-railsrubysphinxthinking-sphinx

Creating a search with thinking-sphinx in rails with only certain attributes of the index searched for


I have an index in sphinx like this:

ThinkingSphinx::Index.define :publication, :with => :active_record do
  indexes transcript.transcript_text, :as => :transcript
  indexes taggings.tag.name, :as => :tags
end

I want to do a search that ignores transcripts and only searches for tags. Is there any way to structure the search to do that?

I thought it might not be possible on that index so I created a new one just for this purpose:

ThinkingSphinx::Index.define :publication, :name => 'tag_only', :with => :active_record do
  indexes taggings.tag.name, :as => :tags
end

However, I couldn't find any information on how to actually use a named index like this in a search. Where would I specify that I wanted to use the tag_only index in a query like this:

Publication.search(terms, :with => with_conditions, :ranker => :matchany)

Any insight would be appreciated, thank you!


Solution

  • From what I understand each indexes in definition is creating a field in the underlying sphinx index.

    A search query on an index, by default, searches all fields. So in your first example, a search on that index would include both fields.

    A search on the second index, which only has one field, would by definition only search that field. So dont need to specificy particular fields.

    ... To search just one (or other subset) of fields in a particular index, could use Sphinxes Extended syntax directly. http://sphinxsearch.com/docs/current.html#extended-syntax

    Or it appears can use conditions http://freelancing-gods.com/thinking-sphinx/searching.html#conditions (which must map to an automatic Extended query :)

    Something like

    Publication.search :conditions => {:tags => terms}, :ranker => :matchany
    

    where :tags is what you called the field in the defintion (sorry dont know ruby syntax exactly to know if exactly right. )