aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/SearchManager.ts
diff options
context:
space:
mode:
authorkimdahey <claire_kim1@brown.edu>2019-12-03 16:32:13 -0500
committerkimdahey <claire_kim1@brown.edu>2019-12-03 16:32:13 -0500
commit70583fa47bd9920d1823d381708c81283534d6ce (patch)
tree5c81976813436852403ea352797efe1d518dbb1a /src/server/ApiManagers/SearchManager.ts
parent56b83d89f37a5523ab319977e3385f539ecaf996 (diff)
parent213962406327cc2f7267064f3016fabf0fd16872 (diff)
merged w master
Diffstat (limited to 'src/server/ApiManagers/SearchManager.ts')
-rw-r--r--src/server/ApiManagers/SearchManager.ts49
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..7afecbb18
--- /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";
+const 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 }) => {
+ const q = req.query.q;
+ if (q === undefined) {
+ res.send([]);
+ return;
+ }
+ const results = await findInFiles.find({ 'term': q, 'flags': 'ig' }, pathToDirectory(Directory.text), ".txt$");
+ const resObj: { ids: string[], numFound: number, lines: string[] } = { ids: [], numFound: 0, lines: [] };
+ for (const 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;
+ }
+ const results = await Search.Instance.search(solrQuery);
+ res.send(results);
+ }
+ });
+
+ }
+
+} \ No newline at end of file