【问题标题】:Rails ElasticSearch model with fields带有字段的 Rails ElasticSearch 模型
【发布时间】:2015-07-26 21:52:13
【问题描述】:

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

"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 的工作示例。

谢谢

【问题讨论】:

  • 请注意,multi fields 并没有被弃用,它们很好而且还活着。在 1.0 版中弃用的是 multi-field type,它现在可以简单地成为具有嵌入式 fields 结构的 string 类型,即正是您上面所拥有的。
  • 好的,谢谢,你有 Rails 的例子吗?
  • 是的,它显示在我下面的答案中。
  • 看我的评论,它会抛出异常......

标签: ruby-on-rails elasticsearch mongoid


【解决方案1】:

您应该能够通过模型中的以下属性定义来实现这一点:

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

【讨论】:

  • 我正在使用 Mongoid,这段代码抛出异常:“未定义的方法 `attribute' for ArticlesEvent:Class”
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-10-19
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 2012-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多