Sphinx Thinking for Searching Throughout the Program: Filtering by Attribute That Only Exists in Some Models

I want to search on several models and filter by a specific attribute that some models have, and some do not. I want models with an attribute to be filtered out, and those who don’t have just ignored it.

Currently, only models with an attribute return results. Is there a way to get other models to return results, somehow ignoring the attribute filter?

+3
source share
2 answers

Found a way to do this. In indexes of models that do not have such an attribute, you can create a mannequin:

has "0", :type => :integer, :as => :the_attribute_name

Then, when performing a search throughout the program:

@results = ThinkingSphinx.search(@search_term, 
  :with => {:the_attribute_name => [@the_attribute_value, 0]}
)

Btw, , , . , (, 9999999). , .

+5

default_sphinx_scope, . , . :

class User

  ...

  sphinx_scope(:active_only) do
    if self.respond_to?(:status)
      {:with => {:status => true}}
    else
      {}
    end
  end
  default_sphinx_scope(:active_only)

  ...
end

scope status. .

0

Source: https://habr.com/ru/post/1773334/


All Articles