2014-08-30 67 views
0

该数据结构是一个Post其has_many Post_text。以https://github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-model/examples/activerecord_associations.rb为例。我已经定义的映射为以下几点:Elasticsearch返回的结果比SQL少吗?

include SearchableModule 
mapping do 
    indexes :country 
    indexes :post_texts do 
    indexes :subject, type: 'string', boost: 10, analyzer: 'snowball' 
    indexes :description, type: 'string', analyzer: 'snowball' 
    end 
end 

当然并且,在searchable_module.rb我只是复制什么是在与as_index_json()一些变化,例如:

def as_indexed_json(options={}) 
    self.as_json(
    include: { post_texts: { only: [:subject, :description]} 
      }) 
end 

,事情似乎确定。我已经重新导入数据:

Post.import 
Post.__elasticsearch__. 

然后我试图通过检查SQL的LIKE和Elasticsearch结果:

SQL LIKE:

PostText.where("subject LIKE '%Testing%' OR description LIKE '%Testing%'").each do |r| 
    puts r.post_id 
end 

有12个独特的POST_ID本做法。

Elasticsearch:

Post.search("Testing").results.count 
=> 10 

有什么我错过了什么?谢谢!!!!

回答

2

你可以尝试Post.search("Testing").total应该返回结果的汇总数,如果有results.count你只指望返回的记录数量有限的假设per_page

+0

'Post.search(“测试”)。results.total显示12个!感谢您的帮助! – Quin 2014-08-30 16:28:53