aboutsummaryrefslogtreecommitdiff
path: root/src/server/Search.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/Search.ts')
-rw-r--r--src/server/Search.ts48
1 files changed, 42 insertions, 6 deletions
diff --git a/src/server/Search.ts b/src/server/Search.ts
index d776480c6..69e327d2d 100644
--- a/src/server/Search.ts
+++ b/src/server/Search.ts
@@ -18,19 +18,34 @@ export class Search {
}
}
- public async search(query: string) {
+ public async updateDocuments(documents: any[]) {
+ try {
+ const res = await rp.post(this.url + "dash/update", {
+ headers: { 'content-type': 'application/json' },
+ body: JSON.stringify(documents)
+ });
+ return res;
+ } catch (e) {
+ // console.warn("Search error: ", e, documents);
+ }
+ }
+
+ public async search(query: string, filterQuery: string = "", start: number = 0, rows: number = 10) {
try {
const searchResults = JSON.parse(await rp.get(this.url + "dash/select", {
qs: {
q: query,
- fl: "id"
+ fq: filterQuery,
+ fl: "id",
+ start,
+ rows,
}
}));
- const fields = searchResults.response.docs;
- const ids = fields.map((field: any) => field.id);
- return ids;
+ const { docs, numFound } = searchResults.response;
+ const ids = docs.map((field: any) => field.id);
+ return { ids, numFound };
} catch {
- return [];
+ return { ids: [], numFound: -1 };
}
}
@@ -46,4 +61,25 @@ export class Search {
});
} catch { }
}
+
+ public 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(this.url + "dash/update", {
+ body: {
+ delete: {
+ query: deleteIds.map(id => `id:"${id}"`).join(" ")
+ }
+ },
+ json: true
+ }));
+ }
+
+ return Promise.all(promises);
+ }
} \ No newline at end of file