Rails 带字段的 ElasticSearch 模型

Rails ElasticSearch model with fields

我正在使用 elasticsearch-rails and mongoid 我有以下简单的字段映射:

"title": {
              "type": "string",
              "fields": {
                 "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                 }
              }
           }

我的模型是这样的:

class ArticlesEvent
include Mongoid::Document
include Elasticsearch::Model

field :title, type: String

attr_accessible :title

def as_indexed_json(options={})
  as_json(except: [:id, :_id])
end

谁能举例说明如何使用 title.raw 字段定义 rails 模型,以及如何访问该字段?由于多字段已被弃用,因此很难找到 rails 和 mongoid 的工作示例。

谢谢

您应该能够在您的模型中使用以下属性定义来实现此目的:

attribute :title, String, mapping: { 
    fields: {
       raw:  { type: 'string', index: 'not_analyzed' }
    } 
}

看起来这仍然是 elasticsearch-rails 的未解决问题:https://github.com/elastic/elasticsearch-rails/issues/79

您可以在模型中定义以下映射

indexes :name_of_field,type: :keyword, fields: {
    raw: {
      type: :text
    }
  }