0

对于轮胎(ElasticSearch 包装器 gem),您如何查询和过滤出某个属性具有 nil/null 值的索引记录。例如,如果我有这种关系

class Article < ActiveRecord::Base
  belongs_to :topic
end

我已将文章编入索引,但我想避免使用topic_id = nil拉回记录。我尝试了这段代码,但没有奏效。

class Article
  belongs_to :topic

  def search(q)
    tire.search do
       ...
         filter :missing, :field => :topic_id, { :existence => false, :null_value => false }
         ...
         ### filter :range, :topic_id => { :gt => 0 } # ==> I tried this as well but didn't work
       ...
    end
  end
end
4

3 回答 3

2

如果您只想过滤具有现有值的文档,我认为您应该使用存在过滤器。topic_id

class Article
  belongs_to :topic

  def search(q)
     tire.search do
       ...
       filter :exists, :field => :topic_id
       ...
    end
  end
end

或者您可以将查询字符串_exists_:topic_id查询一起使用。

于 2012-09-12T09:27:48.747 回答
1

请试试:

class Article
  belongs_to :topic

  def search(q)
    tire.search do
         ...
         filter :not, :missing => {:field => :topic_id, { :null_value => true } }
         ...
    end
  end
end

这应该有效。如果要检查该字段是否存在,则需要存在。我猜你只需要 null_value 检查。

于 2013-01-10T06:16:14.280 回答
-3

@articles = Article.where('topic_id is not ?' , nil)

于 2012-09-10T17:40:57.020 回答