ruby-on-rails - 无法在Elasticsearch中根据 “missing”字段过滤出结果

标签 ruby-on-rails mongodb mongoid elasticsearch tire

我在Rails应用程序中使用Tire-0.4.2与elasticsearch进行交互(使用mongodb数据库和mongoid与mongdb进行交互)。我有一个帖子模型,其中嵌入了一个垃圾邮件文档。

post.rb

include Mongoid::Document
include Mongoid::Timestamps 

..
embeds one :spam, as: :spammable
...

spam.rb
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :spammable, polymorphic: true
field :needs_approval, type: Boolean, default: false
field :is_spam, type: Time

has_and_belongs_to_many :request_spam_by, :class_name => "User"

field :request_spam, type: Boolean, default: false

我想获取没有垃圾邮件文档的所有帖子:这是轮胎查询
Post.tire.search(:load => :true, page: self.page, per_page: Post::PER_PAGE) do |pf|
    pf.query{ |query| query.string self.search_text } unless search_text.blank?
    pf.filter(:missing, :field => 'spam')
    pf.filter(:term, :college_id => self.college.id)
    pf.filter(:term, :user_id => self.user.id)
    pf.filter(:missing, :field => 'spam' )
    pf.filter(:terms, :user_type => self.user_type) unless self.user_type.blank?
    pf.filter(:range, :created_at => {:gte => self.from_time}) unless self.from_time.blank?
    pf.filter(:range, :created_at => {:lte => self.to_time}) unless self.to_time.blank?
    pf.sort{|s| s.by :updated_at, self.sort_order}
end

生成的elasticsearch查询:
curl -X GET "http://localhost:9200/development_posts/post/_search?     from=0&load=true&page=1&per_page=10&size=10&pretty=true" -d '{"sort":[{"updated_at":"desc"}],"filter":{"and":[{"missing":{"field":"spam"}},{"term":{"college_id":"4fb424a5addf32296f00013a"}},{"missing":{"field":"spam"}},{"range":{"created_at":{"gte":"2012-06-05T00:00:00+05:30"}}},{"range":{"created_at":{"lte":"2012-06-05T23:59:59+05:30"}}}]},"size":10,"from":0}'

查询的结果为我提供了存在垃圾邮件的文档,即使我仅搜索缺少垃圾邮件文档的文档也是如此。我不知道我在做什么错。有人能指出我正确的方向吗?

最佳答案

您不能在对象级别使用missing,而只能使用已建立索引的实际字段。

如果垃圾邮件对象中总是存在一个字段,则可以在“spam.always_there”上设置缺少的过滤器,虽然它不太好但是应该可以工作

 pf.filter(:or, [
    {:missing => { :field => 'spam.needs_approval'}},
    {:term => {'spam.needs_approval' => false}}])

应该选择该字段为false或missing的文档(如果默认情况下我没记错的话,null和missing是同一件事,请当心。)

关于ruby-on-rails - 无法在Elasticsearch中根据 “missing”字段过滤出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10893643/

相关文章:

ruby-on-rails - sidekiq getaddrinfo错误(没有地址)

ruby-on-rails - 生产中的 MongoDB 和 Mongoid

node.js - 如何使用 Fixie Socks(Heroku 附加组件)通过 mongoose 连接到 mongodb

javascript - Meteorjs : How can I use variables which are defined in template events inside my template helpers?

delphi - 如何在delphi中编写mongodb代码

ruby-on-rails - Mongoid 没有在parent.save 上保存我的embeds_many child

mongoid - 如何使用 Mongoid 重新索引集合?

ruby-on-rails - Ruby on Rails : how do you add a class in a link_to helper?

ruby-on-rails - SearchKick search_data 与可搜索

ruby-on-rails - ruby rails : models that do not have a table