ruby-on-rails - Elasticsearch返回的结果少于SQL?

标签 ruby-on-rails elasticsearch

数据结构是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类:
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

有什么我想念的吗?谢谢!!!!

最佳答案

您可以尝试Post.search("Testing").total,它应该返回结果的摘要数,如果使用results.count,您只需计算返回记录的数量(假设per_page有限)

关于ruby-on-rails - Elasticsearch返回的结果少于SQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25584147/

相关文章:

ruby-on-rails - Ruby on Rails - 未定义的局部变量或方法 `signin_path'

ruby-on-rails - 非常长的 html 属性的 Haml 多行

ruby-on-rails - 无法使用数组的计数方法来处理 block

elasticsearch - 缓冲区刷新花费的时间比slow_flush_log_threshold长

elasticsearch - 如何获取Elasticsearch实际结果大小

node.js - 我无法过滤也无法聚合通过 LogStash 保存到 ElasticSearch 的文档

asp.net-mvc - Serilog 不会将日志写入 AWS Elasticsearch Service

ruby-on-rails - 思考狮身人面像,联想不起作用

ruby-on-rails - Ruby——使用父实例变量的子类

elasticsearch - 你如何在 Kibana 中导出/导入 "index-pattern"类型