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
|
import { red } from 'colors';
import * as rp from 'request-promise';
const pathTo = (relative: string) => `http://localhost:8983/solr/dash/${relative}`;
export namespace Search {
export async function updateDocument(document: any) {
try {
return await rp.post(pathTo('update'), {
headers: { 'content-type': 'application/json' },
body: JSON.stringify([document]),
});
} catch (e) {
// console.warn("Search error: " + e + document);
}
return undefined;
}
export async function updateDocuments(documents: any[]) {
try {
return await rp.post(pathTo('update'), {
headers: { 'content-type': 'application/json' },
body: JSON.stringify(documents),
});
} catch (e) {
// console.warn("Search error: ", e, documents);
}
return undefined;
}
export async function search(query: any) {
try {
const output = await rp.get(pathTo('select'), { qs: query });
const searchResults = JSON.parse(output);
const { docs, numFound } = searchResults.response;
const ids = docs.map((field: any) => field.id);
return { ids, numFound, highlighting: searchResults.highlighting };
} catch {
return { ids: [], numFound: -1 };
}
}
export async function clear() {
try {
await rp.post(pathTo('update'), {
body: {
delete: {
query: '*:*',
},
},
json: true,
});
} catch (e: any) {
console.log(red('Unable to clear search...'));
console.log(red(e.message));
}
}
export async function deleteDocuments(docs: string[]) {
const promises: rp.RequestPromise[] = [];
const nToDelete = 1000;
let index = 0;
while (index < docs.length) {
const count = Math.min(docs.length - index, nToDelete);
const deleteIds = docs.slice(index, index + count);
index += count;
promises.push(
rp.post(pathTo('update'), {
body: {
delete: {
query: deleteIds.map(id => `id:"${id}"`).join(' '),
},
},
json: true,
})
);
}
return Promise.all(promises);
}
}
|