aboutsummaryrefslogtreecommitdiff
path: root/src/server/Search.ts
blob: 8ae996e9ecd0610ff340668181362fcf501e60d0 (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
import * as rp from 'request-promise';
import { Database } from './database';

export class Search {
    public static Instance = new Search();
    private url = 'http://localhost:8983/solr/';

    public updateDocument(document: any): rp.RequestPromise {
        return rp.post(this.url + "dash/update/json/docs", {
            headers: { 'content-type': 'application/json' },
            body: JSON.stringify(document)
        });
    }

    public async search(query: string) {
        console.log("____________________________");
        console.log(query);
        const searchResults = JSON.parse(await rp.get(this.url + "dash/select", {
            qs: {
                q: query
            }
        }));
        const fields = searchResults.response.docs;
        const ids = fields.map((field: any) => field.id);
        const docs = await Database.Instance.searchQuery(ids);
        const docIds = docs.map((doc: any) => doc._id);
        return docIds;
    }

    public async clear() {
        return rp.post(this.url + "dash/update", {
            body: {
                delete: {
                    query: "*:*"
                }
            },
            json: true
        });
    }
}