@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
你想完全禁用源或僅排除這一特定領域? – imotov 2012-08-09 15:06:46
最好只是排除這一個字段,以便突出顯示/等將仍然可用在其他領域。我想我可以將特定的字段存儲在我們想要突出顯示並完全禁用源代碼的地方,但是我還不清楚它的整體效果。 – Masonoise 2012-08-09 16:50:28