ruby-on-rails - elasticsearch-rails 空结果

标签 ruby-on-rails elasticsearch

我已经执行了 Comment.import 并且它返回了 0,这意味着导入过程中没有错误。

我已经做了 Comment.__elasticsearch__.refresh_index!

这是我正在使用的模型:

require "datamapper_adapter"

class Comment
  include DataMapper::Resource

  include Elasticsearch::Model

  property :id, Serial
  property :email, String
  property :author, String
  property :description, String
  property :created_at, DateTime

  belongs_to :picture

  validates_presence_of :email, :author, :description

  settings index: { number_of_shards: 1 } do
    mappings dynamic: 'false' do
      indexes :id, analyzer: 'english', index_options: 'offsets'
      indexes :author, analyzer: 'english', index_options: 'offsets'
      indexes :description, analyzer: 'english', index_options: 'offsets'
    end
  end

  def as_indexed_json(options={})
    as_json.except("picture_id", "created_at", "email", "_persistence_state", "_repository", "_id")
  end
end

而且我对 elasticsearch 的所有查询仍然返回一个空数组作为命中。

curl -XPOST 'http://localhost:9200/comments/comment/_search?pretty
{
    "took" : 35,
    "timed_out" : false,
    "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
    "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

我的数据库中有数据,但我不知道为什么它从未被过滤。 任何想法为什么会这样?这让我抓狂

无论我做什么,点击总是空的

最佳答案

你可以尝试将 type:string 添加到 indexes :descriptionindexes :author

我还记得我在使用 as_json 时遇到了问题,对于较旧的 rails 版本,它将包含 root 属性。 尝试将其设置为 false,您可以全局或在 as_json( root: false )

中执行此操作

关于ruby-on-rails - elasticsearch-rails 空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25879959/

相关文章:

css - 如何创建具有不同列宽的动态网格

ruby-on-rails - ruby 方法名称中的变量

ruby-on-rails - 功能定位

使用嵌套过滤器的 ElasticSearch 查询不起作用

elasticsearch - 在elasticsearch中,/doc/_mapping和/doc {“mappings”之间有什么区别……}

docker - 具有持久卷的 kube 上的 Elasticsearch

ruby-on-rails - 迁移不回滚

JavaScript 未在 Rails 回调中执行?

python - Elasticsearch - 不需要完全匹配

Elasticsearch Reindex API 不起作用