【问题标题】:Tire (Elastic Search) Nil Comparison轮胎(弹性搜索)无比较
【发布时间】:2012-09-10 17:00:48
【问题描述】:

对于轮胎(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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 elasticsearch tire


    【解决方案1】:

    如果您只想过滤具有现有topic_id 值的文档,我认为您应该使用exists filter

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

    或者您可以使用query string_exists_:topic_id 查询。

    【讨论】:

      【解决方案2】:

      请尝试:

      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 检查。

      【讨论】:

      • 您不能将 null_value 和存在都设置为 false。至少一个或两个都应该设置为 true。
      【解决方案3】:

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

      【讨论】:

      • 抱歉,这是针对轮胎 gem DSL(ElasticSearch 包装器)的。不是 ActiveRecord。
      猜你喜欢
      • 1970-01-01
      • 2013-02-14
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多