作者FacetheFaith ( )
看板Soft_Job
标题Fw: [问题] 如何加快搜寻效率
时间Wed Sep 20 16:12:23 2017
借助软体版的高人气,请教一下前辈们,小弟有一支搜寻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上面的根目录搜寻同样档案一下子就找到了,
所以是演算法的问题吗?
程式该怎麽改写才能增进搜寻效率?
谢谢!
1F:→ StarRoad: 这里是"软体工作"版,不是软体版 09/20 16:15
2F:推 JameC: 你可以去Prob_Solve 问问看 09/20 16:22
3F:→ JameC: 那里虽然冷清,但通常都会有人回答你 09/20 16:23
4F:推 sing10407: stackoverflow上应该比较容易有解答 09/20 16:34
5F:推 drajan: 建index 每一小时更新一次那个index table就好 09/20 16:37
6F:→ alog: 1. 需要建立跟维护档案的索引表,搜寻时可以优先找索引里的 09/20 16:51
7F:→ alog: 资料 09/20 16:51
8F:→ alog: 2. 索引表里面可以根据一些特徵,帮他标上一些 Meta 例如: 09/20 16:54
9F:→ alog: 属於图片类、文件类,再根据搜寻的关键字特徵优先寻找特定 09/20 16:54
10F:→ alog: 类的资料 09/20 16:54
11F:→ alog: 概念大概就是这样 剩下的你得自己去想 毕竟是维护你自己app 09/20 16:55
※ 编辑: FacetheFaith (59.124.165.65), 09/20/2017 16:58:47
12F:→ FacetheFaith: 用elasticsearch建立index效果好吗 09/20 18:17
13F:嘘 pttuser: 你可以重写 09/20 18:29
14F:→ PUTOUCHANG: 你会自删吗? 09/20 18:42
15F:推 chuegou: 楼上好问题 09/20 18:53
16F:→ ChungLi5566: 我猜25号前就删了 09/20 19:03
17F:推 blackcan: 有种手机拍电脑画面的感觉…请用gist 09/20 19:20
19F:→ alog: 1. 如果你的服务下需要用到这样,用 elasticsearch 可啊, 09/20 19:33
20F:→ alog: 不过要说,这东西是索引系统如果真的挂了你就当作不能恢复, 09/20 19:33
21F:→ alog: 直接重新建立 2. 你可以先用 docker 把软体拉下来运作进行 09/20 19:33
22F:→ alog: 评估 3. 记得搜寻条件记得下好,不要让其他 user 可以存取 09/20 19:33
23F:→ alog: 到他人的索引 这台也只能挂在後端不能对外存取 09/20 19:33
24F:→ pttworld: 我在解题版回你了 09/20 20:18
25F:嘘 bakedgrass: 因为高人气所以当作问题板不是好风气 09/21 02:52
26F:→ iamshiao: 贴 SO 问 09/21 10:29
27F:→ THEWORLDS: GOOGLE的我写过 他会动态改变每一次地址真的很难抓 09/22 15:11