看板 Soft_Job 關於我們 聯絡資訊
借助軟體版的高人氣,請教一下前輩們,小弟有一支搜尋Google Drive的程式 因為使用者通常不知道folder id,所以預設的搜尋位置是從根目錄(root)開始搜尋檔案 我採用的是深度優先搜尋法(DFS),也就是搜尋到的檔案如果是資料夾 那麼接著就開始搜尋該資料夾下的檔案,以此類推 如果要搜尋的檔案在很前面 (不清楚一開始搜尋的資料夾是依據什麼), 那麼該檔案就很有可能被找到 反之,就有可能回傳 HTTP 500 Internal Server Error(應該是Time out) 程式碼如下,是使用遞迴搜尋: ... 省略 ... service = new Drive.Builder(httpTransport, JSON_FACTORY, credential) .setApplicationName(APPLICATION_NAME).build(); System.out.println("=== Start to search ==="); long startTime = System.currentTimeMillis(); File searchResult = recursiveSearch(folderID, searchFileName); if (searchResult != null) { result = searchResult.getName(); // 結束時間 long endTime = System.currentTimeMillis(); long totTime = (endTime - startTime) / 1000; // 印出花費時間 System.out.println("花費時間:" + totTime + "秒"); } public File recursiveSearch(String folderID, String searchFileName) throws IOException { File searchResult = null; FileList fileList = service.files().list() .setQ("'" + folderID + "' in parents and trashed = false") // .setSpaces("drive") .setCorpora("user") .setFields("nextPageToken, files(id, name, mimeType)").execute(); List<File> items = fileList.getFiles(); System.out.println("files size is " + items.size()); for (File file : items) { if (file.getName().equals(searchFileName)) { searchResult = file; System.out.println(file.getName() + " is found!"); return searchResult; } else if (file.getMimeType().equals("application/vnd.google-apps .folder")) { System.out.println("recursive search"); System.out.println("file.getId() is " + file.getId()); searchResult = recursiveSearch(file.getId(), searchFileName); } else { System.out.println("file name is " + file.getName()); } if (searchResult != null) { System.out.println("Finish"); break; } } return searchResult; } public static void main(String[] args) throws IOException { DriveSearch driveSearch = new DriveSearch(); String result = driveSearch.fetchData("hfjBV5Z3V2c", "test.txt"); System.out.println(result); } 在Google Drive上面的根目錄搜尋同樣檔案一下子就找到了, 所以是演算法的問題嗎? 程式該怎麼改寫才能增進搜尋效率? 謝謝!
StarRoad: 這裡是"軟體工作"版,不是軟體版 09/20 16:15
JameC: 你可以去Prob_Solve 問問看 09/20 16:22
JameC: 那裡雖然冷清,但通常都會有人回答你 09/20 16:23
sing10407: stackoverflow上應該比較容易有解答 09/20 16:34
drajan: 建index 每一小時更新一次那個index table就好 09/20 16:37
alog: 1. 需要建立跟維護檔案的索引表,搜尋時可以優先找索引裡的 09/20 16:51
alog: 資料 09/20 16:51
alog: 2. 索引表裡面可以根據一些特徵,幫他標上一些 Meta 例如: 09/20 16:54
alog: 屬於圖片類、文件類,再根據搜尋的關鍵字特徵優先尋找特定 09/20 16:54
alog: 類的資料 09/20 16:54
alog: 概念大概就是這樣 剩下的你得自己去想 畢竟是維護你自己app 09/20 16:55
※ 編輯: FacetheFaith (59.124.165.65), 09/20/2017 16:58:47
FacetheFaith: 用elasticsearch建立index效果好嗎 09/20 18:17
pttuser: 你可以重寫 09/20 18:29
PUTOUCHANG: 你會自刪嗎? 09/20 18:42
chuegou: 樓上好問題 09/20 18:53
ChungLi5566: 我猜25號前就刪了 09/20 19:03
blackcan: 有種手機拍電腦畫面的感覺…請用gist 09/20 19:20
zerof: https://goo.gl/2r97PM ? 09/20 19:33
alog: 1. 如果你的服務下需要用到這樣,用 elasticsearch 可啊, 09/20 19:33
alog: 不過要說,這東西是索引系統如果真的掛了你就當作不能恢復, 09/20 19:33
alog: 直接重新建立 2. 你可以先用 docker 把軟體拉下來運作進行 09/20 19:33
alog: 評估 3. 記得搜尋條件記得下好,不要讓其他 user 可以存取 09/20 19:33
alog: 到他人的索引 這台也只能掛在後端不能對外存取 09/20 19:33
pttworld: 我在解題版回你了 09/20 20:18
bakedgrass: 因為高人氣所以當作問題板不是好風氣 09/21 02:52
iamshiao: 貼 SO 問 09/21 10:29
THEWORLDS: GOOGLE的我寫過 他會動態改變每一次地址真的很難抓 09/22 15:11