【问题标题】:How do you index attachment in Elasticsearch with Tire?你如何用轮胎在 Elasticsearch 中索引附件?
【发布时间】:2012-06-30 09:34:08
【问题描述】:

难以通过 Tire gem 在 elasticsearch 中索引附件类型。无法正确设置附件type

我采用了从轮胎 gem 中引用的 ActiveModel Integration example 并添加了一个字段 filename 来引用本地文件系统上我想用记录索引的 PDF 的名称。

#app/models/article.rb
class Article < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks

  attr_accessible :title, :content, :published_on, :filename

  mapping do
    indexes :id, :type =>'integer'
    indexes :title
    indexes :content
    indexes :published_on, :type => 'date'
    indexes :attachment, :type => 'attachment' 
  end

  def to_indexed_json
    to_json(:methods => [:attachment])
  end

  def attachment
    if filename.present?
       path_to_pdf = "/Volumes/Disk41402/test_proj/sample_pdfs/#{filename}.pdf"
       Base64.encode64(open(path_to_pdf) { |pdf| pdf.read })
    end
  end
end

FWIW - PDF 似乎已添加到索引中:

$ curl -XGET 'http://localhost:9200/articles/_all/2?pretty=true'  
{
  "_index" : "articles",
  "_type" : "article",
  "_id" : "2",
  "_version" : 1,
  "exists" : true, "_source" : {"content":"Consectetur adipisicing elit, sed do eiusmod tempor incididunt. working?","created_at":"2012-06-21T17:19:03Z","filename":"sample2","id":2,"published_on":"2012-06-20","title":"Two","updated_at":"2012-06-28T00:00:59Z","attachment":"JVBERi0xLjMKJcTl8uXrp/Og0MTGCjQgMCBvYmoKPDwgL0xlbmd0aCA1IDAg\nUiAvRmlsdG...  ... ...4+CnN0YXJ0eHJlZgo4\nNTAzCiUVPRgo=\n"
}
}

但是附件类型仍然是"type" : "string",而应该是"type" : "attachment"

$ curl -XGET 'http://localhost:9200/articles/_mapping?pretty=true'
{
  "articles" : {
    "article" : {
      "properties" : {
        "attachment" : {
          "type" : "string"
        },
        "content" : {
          "type" : "string"
        },
        "created_at" : {
          "type" : "date",
          "format" : "dateOptionalTime"
        },
        "filename" : {
          "type" : "string"
        },
        "id" : {
          "type" : "long"
        },
        "published_on" : {
          "type" : "date",
          "format" : "dateOptionalTime"
        },
        "title" : {
          "type" : "string"
        },
        "updated_at" : {
          "type" : "date",
          "format" : "dateOptionalTime"
        }
      }
    }
  }
}

我尝试重建索引rake environment tire:import CLASS=Article FORCE=true,但类型仍然是字符串。有人知道我在哪里搞砸了吗?

日志(不确定“无处理程序”是什么意思!?):

[2012-06-28 17:30:58,711][INFO ][cluster.metadata         ] [Kofi Whitemane] [articles] deleting index
[2012-06-28 17:30:58,765][WARN ][cluster.metadata         ] [Kofi Whitemane] [articles] failed to create
org.elasticsearch.index.mapper.MapperParsingException: mapping [article]
    at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService$1.execute(MetaDataCreateIndexService.java:263)
    at org.elasticsearch.cluster.service.InternalClusterService$2.run(InternalClusterService.java:211)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)
Caused by: org.elasticsearch.index.mapper.MapperParsingException: No handler for type [attachment] declared on field [attachment]
    at org.elasticsearch.index.mapper.object.ObjectMapper$TypeParser.parseProperties(ObjectMapper.java:259)
    at org.elasticsearch.index.mapper.object.ObjectMapper$TypeParser.parse(ObjectMapper.java:217)
    at org.elasticsearch.index.mapper.DocumentMapperParser.parse(DocumentMapperParser.java:161)
    at org.elasticsearch.index.mapper.MapperService.parse(MapperService.java:271)
    at org.elasticsearch.index.mapper.MapperService.add(MapperService.java:174)
    at org.elasticsearch.cluster.metadata.MetaDataCreateIndexService$1.execute(MetaDataCreateIndexService.java:260)
    ... 4 more
[2012-06-28 17:30:58,805][INFO ][cluster.metadata         ] [Kofi Whitemane] [articles] creating index, cause [auto(bulk api)], shards [5]/[1], mappings []
[2012-06-28 17:30:58,891][INFO ][cluster.metadata         ] [Kofi Whitemane] [articles] update_mapping [article] (dynamic)

【问题讨论】:

    标签: ruby-on-rails attachment elasticsearch tire


    【解决方案1】:

    您是否安装了mapper attachment 插件?

    the tutorial at elasticsearch.org

    【讨论】:

    • 是的!就是这样!我已经安装了它,但是当我更新到 0.19.6 时,插件文件夹并没有出现。傻我!
    猜你喜欢
    • 2012-03-22
    • 2012-03-24
    • 2012-10-19
    • 2014-10-24
    • 2012-09-24
    • 2013-09-26
    • 2013-07-23
    • 2012-06-08
    • 1970-01-01
    相关资源
    最近更新 更多