2012-09-10 1 views
0

Tire (ElasticSearch wrapper gem)의 경우 특정 속성에 대해 nil/null 값을 갖는 인덱싱 된 레코드를 어떻게 쿼리하고 필터링합니까? 나는이 관계Tire (Elastic Search) Nil 비교

class Article < ActiveRecord::Base 
    belongs_to :topic 
end 

이있는 경우 예를 들어, 나는 기사 색인을하지만 난 topic_id = 전무 레코드를 후퇴하지 않도록합니다. 나는이 코드 타격을 시도했지만 작동하지 않았다.

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 

답변

-3

@articles = Article.where ('topic_id이 아닌가?', 전무)

+0

미안하지만 이것은 타이어 보석 DSL (ElasticSearch wrapper) 용입니다. ActiveRecord가 아닙니다. – RubyFanatic

2

난 당신이 topic_id 값을 기존 만 문서를 필터링 할 경우 exists filter를 사용한다고 생각합니다.

class Article 
    belongs_to :topic 

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

또는 당신은 _exists_:topic_id 쿼리 query string를 사용할 수 있습니다.

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 검사 만하면되는 것 같습니다.

+0

null_value와 exist를 모두 false로 설정할 수는 없습니다. 최소한 하나 또는 둘 모두를 true로 설정해야합니다. –

관련 문제