elasticsearch-rails空结果

ren*_*ojf 5 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
Run Code Online (Sandbox Code Playgroud)

而且我对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" : [ ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我的数据库中有数据,但我不知道它为什么永远不会被过滤掉.任何想法为什么会这样?这让我疯了

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

Chr*_*ris 1

你可以尝试添加type:stringindexes :descriptionindexes :author

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