diff options
| author | Sam Wilkins <35748010+samwilkins333@users.noreply.github.com> | 2019-12-02 12:21:09 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-02 12:21:09 -0500 |
| commit | 0595f93dde717b7b6990e9a81c5b43a73a3808d5 (patch) | |
| tree | af03ac257a5584f9913f120c44d4d39bb13916f2 /src/server/ApiManagers/SearchManager.ts | |
| parent | 68f49ef5daf3bf5c47d1d21c8f1cd2097947d071 (diff) | |
| parent | ae76fd39a6530ac055948bb7b98537d38b592ef6 (diff) | |
Merge pull request #316 from browngraphicslab/server_refactor
Server refactor
Diffstat (limited to 'src/server/ApiManagers/SearchManager.ts')
| -rw-r--r-- | src/server/ApiManagers/SearchManager.ts | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/server/ApiManagers/SearchManager.ts b/src/server/ApiManagers/SearchManager.ts new file mode 100644 index 000000000..d3f8995b0 --- /dev/null +++ b/src/server/ApiManagers/SearchManager.ts @@ -0,0 +1,49 @@ +import ApiManager, { Registration } from "./ApiManager"; +import { Method } from "../RouteManager"; +import { Search } from "../Search"; +var findInFiles = require('find-in-files'); +import * as path from 'path'; +import { pathToDirectory, Directory } from "./UploadManager"; + +export default class SearchManager extends ApiManager { + + protected initialize(register: Registration): void { + + register({ + method: Method.GET, + subscription: "/textsearch", + onValidation: async ({ req, res }) => { + let q = req.query.q; + if (q === undefined) { + res.send([]); + return; + } + let results = await findInFiles.find({ 'term': q, 'flags': 'ig' }, pathToDirectory(Directory.text), ".txt$"); + let resObj: { ids: string[], numFound: number, lines: string[] } = { ids: [], numFound: 0, lines: [] }; + for (var result in results) { + resObj.ids.push(path.basename(result, ".txt").replace(/upload_/, "")); + resObj.lines.push(results[result].line); + resObj.numFound++; + } + res.send(resObj); + } + }); + + register({ + method: Method.GET, + subscription: "/search", + onValidation: async ({ req, res }) => { + const solrQuery: any = {}; + ["q", "fq", "start", "rows", "hl", "hl.fl"].forEach(key => solrQuery[key] = req.query[key]); + if (solrQuery.q === undefined) { + res.send([]); + return; + } + let results = await Search.Instance.search(solrQuery); + res.send(results); + } + }); + + } + +}
\ No newline at end of file |
