思考Sphinx应用程序范围的搜索:按仅存在于某些模型中的属性进行过滤

发布于 2024-09-30 16:41:38 字数 127 浏览 3 评论 0原文

我想搜索多个模型并按某些模型具有而某些模型没有的特定属性进行过滤。我希望具有该属性的模型被过滤,但没有该属性的模型则忽略它。

目前只有具有该属性的模型才会返回结果。有没有办法通过某种方式忽略属性过滤器来使其他模型也返回结果?

I want to search on multiple models and filter by a certain attribute that some models have and some do not. I want the models with the attribute to get filtered but the ones without it to just ignore it.

Currently only the models with the attribute will return results. Is there a way to make the other models return results as well by somehow ignoring the attribute filter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉鱼一梦 2024-10-07 16:41:38

找到了一种方法来做到这一点。在不具有此类属性的模型的索引上,可以像这样创建一个虚拟索引:

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

然后在执行应用程序范围搜索时:

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

顺便说一句,这假设具有此属性的模型不允许使用零值属性。
如果零是这些模型中的有效属性,则可以使用另一个值(例如 9999999)。
请注意,属性不能接受负整数。

Found a way to do it. On the indexes of the models that do not have such an attribute, a dummy one can be created like so:

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

Then when performing the application-wide search:

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

Btw, this assumes that a zero value is not allowed on the models that do have this attribute.
If zero is a valid attribute in those model then another value (e.g. 9999999) can be used.
Be aware that attributes cannot accept negative integers.

丑疤怪 2024-10-07 16:41:38

我必须在 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

仅当存在 status 列时才应用 scope。干杯。

I had to do this in a default_sphinx_scope and the application being too big I couldn't check every model and do that for those who have doesn't have the attribute. So I did it with following:

class User

  ...

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

  ...
end

It applied the scope only when the status column was present. Cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文