【问题标题】:How to prevent attachments from being stored in _source with Elasticsearch and Tire?如何使用 Elasticsearch 和 Tire 防止附件存储在 _source 中?
【发布时间】:2017-01-03 18:45:38
【问题描述】:

我使用 Tire gem 在 Elasticsearch 中索引了一些 PDF 附件。这一切都很好,但我将拥有许多 GB 的 PDF,我们可能会将 PDF 存储在 S3 中以供访问。目前 base64 编码的 PDF 存储在 Elasticsearch _source 中,这将使索引变得巨大。我想让附件被索引,但不存储,我还没有找到正确的咒语来放入轮胎的“映射”块以防止它。块现在是这样的:

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

我尝试了一些变体,例如:

indexes :attachment, :type => 'attachment', :_source => { :enabled => false }

当我运行轮胎:import rake 任务时它看起来不错,但它似乎没有任何区别。有谁知道A)如果这是可能的? B) 怎么做?

提前致谢。

【问题讨论】:

  • 您要完全禁用源还是只排除该特定字段?
  • 最好只排除这个字段,以便突出显示/等在其他字段上仍然可用。我想我可以存储我们想要突出显示和完全禁用源的特定字段,但我还不清楚这会产生什么整体效果。

标签: elasticsearch attachment tire


【解决方案1】:

_source field settings 包含应从源中排除的字段列表。我猜在轮胎的情况下,应该这样做:

mapping :_source => { :excludes => ['attachment'] } do
  indexes :id, :type => 'integer'
  indexes :title
  indexes :last_update, :type => 'date'
  indexes :attachment, :type => 'attachment'
end

【讨论】:

  • 看起来是这样做的!非常感谢您的回答——希望这将被添加到轮胎的文档中,因为这是一个很好的选择。
【解决方案2】:

@imotov 的解决方案对我不起作用。当我执行 curl 命令时

curl -X GET "http://localhost:9200/user_files/user_file/_search?pretty=true" -d '{"query":{"query_string":{"query":"rspec"}}}'

我仍然可以看到搜索结果中包含的附件​​文件的内容。

"_source" : {"user_file":{"id":5,"folder_id":1,"updated_at":"2012-08-16T11:32:41Z","attachment_file_size":179895,"attachment_updated_at":"2012-08-16T11:32:41Z","attachment_file_name":"hw4.pdf","attachment_content_type":"application/pdf","created_at":"2012-08-16T11:32:41Z","attachment_original":"JVBERi0xL .....

这是我的实现:

include Tire::Model::Search
include Tire::Model::Callbacks

def self.search(folder, params)
  tire.search() do
    query { string params[:query], default_operator: "AND"} if params[:query].present?
    filter :term, folder_id: folder.id
    highlight :attachment_original, :options => {:tag => "<em>"}
  end
end

mapping :_source => { :excludes => ['attachment_original'] } do
  indexes :id, :type => 'integer'
  indexes :folder_id, :type => 'integer'
  indexes :attachment_file_name
  indexes :attachment_updated_at, :type => 'date'
  indexes :attachment_original, :type => 'attachment'
end

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

def attachment_original
  if attachment_file_name.present?
    path_to_original = attachment.path
    Base64.encode64(open(path_to_original) { |f| f.read })
  end    
end

【讨论】:

  • 这听起来很明显,但我只是想仔细检查一下:添加“排除”后,您确实删除了索引并进行了完整的重新索引?我问是因为当我测试时我忘了做一次,花了几分钟才意识到它,所以检查不会有什么坏处。您的代码看起来正确,所以...
  • 是的,我确实运行了:rake environment Tire:import CLASS='Article' FORCE=true 来重新索引。我还从轮胎.search() 中删除了突出显示,但它没有帮助。我仍然看到 _source 中包含的附件​​内容:(
  • 嗯,我刚刚注意到,在搜​​索结果中,所有字段,包括未映射的字段,都包含在 _source 中。这不应该发生对吗?我想我会发布另一个关于此的问题。谢谢!
  • 嗯,我误解了 to_indexed_json 的工作原理。看到这个问题stackoverflow.com/questions/12002069/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-20
  • 1970-01-01
  • 2012-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
相关资源
最近更新 更多