【问题标题】:Thinking sphinx - exclude classes if field isn't present in index思考狮身人面像 - 如果索引中不存在字段,则排除类
【发布时间】:2012-11-22 10:55:56
【问题描述】:

我正在做一个应用程序搜索,它可能会过滤几个不同的字段——其中一些字段并不存在于每个索引中。

目前,使用复杂的 if elsif elsif 事物来整理要在控制器中搜索的正确类。

如果一个条件字段不存在,有没有办法让思维狮身人面像自动不搜索模型,而不是像目前似乎发生的那样忽略条件。

例如

 ThinkingSphinx.search(@query, :conditions => {:genres =>"classical-music"}, :match_mode => :extended, :classes => [Performer, Promoter, Tour, Venue, User], :order => :name_sort, :sort_mode => :asc)

用户没有流派,但包含在搜索结果中


更新

这绝对不能如我所愿。我的用户模型在索引中没有流派字段,因此我希望在搜索流派时将其排除在外。

搜索示例

User.search("anne")
  Sphinx Query (3.6ms)  anne
  Sphinx  Found 1 result
  User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` IN (80)
 => [#<User id: 80, first_name: "Anne", last_name: "Bowers", bio: nil, email: "anne@bowers.com", phone: nil, created_at: "2012-11-20 09:36:05", updated_at: "2012-11-20 09:36:05", role: nil, promoter_id: nil, performer_id: nil, job_title: nil>] 


ThinkingSphinx.search("anne", :conditions => {:genres => "music"}, :match_mode => :extended, :classes => [User], :order => :name_sort, :sort_mode => :asc)
  Sphinx Query (3.2ms)  anne @genres music
  Sphinx  Found 1 result
  User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` IN (80)
 => [#<User id: 80, first_name: "Anne", last_name: "Bowers", bio: nil, email: "anne@bowers.com", phone: nil, created_at: "2012-11-20 09:36:05", updated_at: "2012-11-20 09:36:05", role: nil, promoter_id: nil, performer_id: nil, job_title: nil>] 

如果用户被排除在外就太好了。

【问题讨论】:

    标签: ruby-on-rails thinking-sphinx


    【解决方案1】:

    您可以尝试传递类以在参数中进行搜索。

    但我认为你的方式也应该有效。

    对我来说

    ThinkingSphinx.search(conditions: { type: "text" })
    

    它只是写一个错误

    Sphinx 守护程序返回警告:索引 bulletin_core、bulletin_delta、user_delta ...:查询错误:在架构中找不到字段“类型”

    但它不会崩溃。它只返回具有索引字段type的类的实例@

    你也可以试试这里写的https://groups.google.com/forum/?fromgroups=#!topic/thinking-sphinx/rrAPXtxUMjg,不过我还没试过

    【讨论】:

    • 如果您添加字符串查询以及条件,条件将被忽略 - 请参阅我的更新。
    • 这很奇怪,因为当我尝试ThinkingSphinx.search('Savanna', conditions: { type: "Text" }, classes: [User]) 时它会写入错误Sphinx Daemon returned error: index user_core,user_delta: query error: no field 'type' found in schema 并且不返回任何结果
    • 所以我认为使用参数传递类是个好主意,例如,您可以将此参数存储在隐藏字段中,并在某些 javascript 事件中通过 javascript 更改它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多