aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/SearchManager.ts
blob: d3f8995b0dd477c64ac4115c79927f786eb191a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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);
            }
        });

    }

}