数据结构是Post
has_many Post_text
。在https://github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-model/examples/activerecord_associations.rb举了一个很好的例子。我已将映射定义如下:
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
我有什么遗漏的吗?谢谢!!!!
答案 0 :(得分:2)
你可以尝试Post.search("Testing").total
,它应该返回结果的摘要数量,如果results.count
你只计算返回记录的数量,假设有限的per_page