映射器附件类型的自定义映射与elasticsearch-persistence ruby

时间:2015-12-24 10:51:18

标签: elasticsearch-rails elasticsearch-model elasticsearch-ruby

在我的项目中,我使用mapper-attachments插件将数据存储在elasticsearch中的活动记录模型和索引html文档中。我的文档映射如下所示:

include Elasticsearch::Model

settings index: { number_of_shards: 5 } do
  mappings do
    indexes :alerted
    indexes :title, analyzer: 'english', index_options: 'offsets'
    indexes :summary, analyzer: 'english', index_options: 'offsets'
    indexes :content, type: 'attachment', fields: { 
                                                    author: { index: "no"},
                                                    date: { index: "no"},
                                                    content: { store: "yes",
                                                               type: "string",
                                                               term_vector: "with_positions_offsets"
                                                            }
                                                  }
  end
end

我运行一个查询来仔细检查我的doc映射和结果:

    "mappings": {
          "feed_entry": {
              "properties": {
                  "content": {
                      "type": "attachment",
                      "path": "full",
                      "fields": {
                          "content": {
                              "type": "string",
                              "store": true,
                              "term_vector": "with_positions_offsets"
                          },

效果很好(上面的类型:'附件')。我可以完美地通过html doc进行搜索。

我的activerecord存在性能问题,这是mysql,我真的不需要将它存储在数据库中,因此我决定迁移到elasticsearch中的存储。

我正在使用elasticsearch-persistence gem进行实验。

我将映射配置如下:

include Elasticsearch::Persistence::Model
attribute :alert_id, Integer
attribute :title, String, mapping: { analyzer: 'english' }
attribute :url, String, mapping: { analyzer: 'english' }
attribute :summary, String, mapping: { analyzer: 'english' }
attribute :alerted, Boolean, default: false, mapping: { analyzer: 'english' }
attribute :fingerprint, String, mapping: { analyzer: 'english' }
attribute :feed_id, Integer
attribute :keywords

attribute :content, nil, mapping: { type: 'attachment', fields: { 
                                                      author: { index: "no"},
                                                      date: { index: "no"},
                                                      content: { store: "yes",
                                                                 type: "string",
                                                                 term_vector: "with_positions_offsets"
                                                              }
                                                    }

但是当我对映射进行查询时,我得到了类似的结果:

"mappings": {
        "entry": {
            "properties": {
                "content": {
                    "properties": {
                        "_content": {
                            "type": "string"
                        },
                        "_content_type": {
                            "type": "string"
                        },
                        "_detect_language": {
                            "type": "boolean"
                        },

这是错误的。任何人都可以告诉我如何使用附件类型进行映射?

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

与此同时,我必须以这种方式对其进行硬编码:

  def self.recreate_index!
    mappings = {}
    mappings[FeedEntry::ELASTIC_TYPE_NAME]= {

                "properties": {
                  "alerted": {
                    "type": "boolean"
                  },
                  "title": {
                    #for exact match
                    "index": "not_analyzed",
                    "type": "string"
                  },
                  "url": {
                    "index": "not_analyzed",
                    "type": "string"
                  },                      
                  "summary": {
                    "analyzer": "english",
                    "index_options": "offsets",
                    "type": "string"
                  },
                  "content": {
                    "type": "attachment",
                    "fields": {
                      "author": {
                        "index": "no"
                      },
                      "date": {
                        "index": "no"
                      },
                      "content": {
                        "store": "yes",
                        "type": "string",
                        "term_vector": "with_positions_offsets"
                      }
                    }
                  }
                }
          }
    options = {
      index: FeedEntry::ELASTIC_INDEX_NAME,
    }
    self.gateway.client.indices.delete(options) rescue nil
    self.gateway.client.indices.create(options.merge( body: { mappings: mappings}))   
  end

然后覆盖to_hash方法

  def to_hash(options={})
    hash = self.as_json
    map_attachment(hash) if !self.alerted
    hash
  end

  # encode the content to Base64 formatj
  def map_attachment(hash)
    hash["content"] = {
      "_detect_language": false,
      "_language": "en",
      "_indexed_chars": -1 ,
      "_content_type": "text/html",
      "_content": Base64.encode64(self.content)
    }
    hash
  end

然后我必须致电

FeedEntry.recreate_index! 

之前用手创建弹性搜索的映射。在更新文档时,您可能最终会使用内容字段的双base64编码。在我的场景中,我检查了已警告字段。