2012-08-08 30 views
4

我有一些PDF附件在Elasticsearch中被索引,使用輪胎寶石。這一切都很好,但我將有很多GB的PDF,我們可能會將這些PDF存儲在S3中以供訪問。目前,base64編碼的PDF存儲在Elasticsearch _source中,這將使索引變得龐大。我想將附件編入索引,但沒有存儲,我還沒有找到正確的咒語來放置Tire的「映射」塊來阻止它。該塊是像現在這樣的權利:如何使用Elasticsearch和Tire防止附件存儲在_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 } 

當我運行的輪胎它看起來不錯:進口耙的任務,但它似乎沒有有所作爲。有沒有人知道A)如果這是可能的?和B)如何做到這一點?

在此先感謝。

+0

你想完全禁用源或僅排除這一特定領域? – imotov 2012-08-09 15:06:46

+0

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

回答

4

_source field settings包含應該從源中排除的字段列表。我猜測在輪胎的情況下,應該這樣做:

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

看起來像這樣做!非常感謝答案 - 希望這會被添加到Tire的文檔中,因爲這是一個很好的選擇。 – Masonoise 2012-08-10 18:37:08

0

@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 
+0

這聽起來很明顯,但我只是想仔細檢查一下:在添加「排除」之後,您是否刪除了索引並執行完整的重新索引?我問,因爲當我測試時,我忘了這麼做,並花了幾分鐘才發現它,所以它不會受傷檢查。你的代碼看起來是正確的,所以... – Masonoise 2012-08-17 06:02:05

+0

是的,我確實運行過:rake environment tyre:import CLASS ='Article'FORCE = true to reindex。我也從tire.search()中刪除了重點,但它沒有幫助。我仍然看到附件內容包含在_source :( – 2012-08-17 07:04:23

+0

hmm,我剛剛注意到在搜索結果中包括那些未映射的字段都包含在_source中,這不應該是正確的嗎?我認爲我會發布有關此問題的另一個問題。謝謝! – 2012-08-17 07:33:44

相關問題