aboutsummaryrefslogtreecommitdiff
path: root/src/server/ApiManagers/SearchManager.ts
blob: 37d66666bc00e1c5400174ad8798ea884fff86fa (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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";
import { command_line } from "../ActionUtilities";
import request = require('request-promise');
import { red } from "colors";
import RouteSubscriber from "../RouteSubscriber";

export class SearchManager extends ApiManager {

    protected initialize(register: Registration): void {

        register({
            method: Method.GET,
            subscription: new RouteSubscriber("solr").add("action"),
            onValidation: async ({ req, res }) => {
                const { action } = req.params;
                if (["start", "stop"].includes(action)) {
                    const status = req.params.action === "start";
                    const success = await SolrManager.SetRunning(status);
                    console.log(success ? `Successfully ${status ? "started" : "stopped"} Solr!` : `Uh oh! Check the console for the error that occurred while ${status ? "starting" : "stopping"} Solr`);
                }
                res.redirect("/home");
            }
        });

        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.search(solrQuery);
                res.send(results);
            }
        });

    }

}

export namespace SolrManager {

    export async function SetRunning(status: boolean): Promise<boolean> {
        const args = status ? "start" : "stop -p 8983";
        try {
            console.log(`Solr management: trying to ${args}`);
            console.log(await command_line(`solr.cmd ${args}`, "./solr-8.3.1/bin"));
            return true;
        } catch (e) {
            console.log(red(`Solr management error: unable to ${args}`));
            return false;
        }
    }

}