diff options
| author | bobzel <zzzman@gmail.com> | 2024-05-19 01:01:02 -0400 |
|---|---|---|
| committer | bobzel <zzzman@gmail.com> | 2024-05-19 01:01:02 -0400 |
| commit | 6e72f969029c22fe797397a6437836a0482260b6 (patch) | |
| tree | e8ccde75702e557b2226c9069263e1bc3bd21a4b /src/server/ApiManagers/SearchManager.ts | |
| parent | 5ff0bef5d3c4825aa7210a26c98aae3b24f4a835 (diff) | |
| parent | 13dc6de0e0099f699ad0d2bb54401e6a0aa25018 (diff) | |
Merge branch 'restoringEslint' into alyssa-starter
Diffstat (limited to 'src/server/ApiManagers/SearchManager.ts')
| -rw-r--r-- | src/server/ApiManagers/SearchManager.ts | 78 |
1 files changed, 27 insertions, 51 deletions
diff --git a/src/server/ApiManagers/SearchManager.ts b/src/server/ApiManagers/SearchManager.ts index 72c01def7..684b49eaf 100644 --- a/src/server/ApiManagers/SearchManager.ts +++ b/src/server/ApiManagers/SearchManager.ts @@ -1,14 +1,12 @@ +/* eslint-disable no-use-before-define */ import { exec } from 'child_process'; import { cyan, green, red, yellow } from 'colors'; -import * as path from 'path'; -import { log_execution } from '../ActionUtilities'; -import { Database } from '../database'; +import { logExecution } from '../ActionUtilities'; import { Method } from '../RouteManager'; import RouteSubscriber from '../RouteSubscriber'; import { Search } from '../Search'; +import { Database } from '../database'; import ApiManager, { Registration } from './ApiManager'; -import { Directory, pathToDirectory } from './UploadManager'; -import { find } from 'find-in-files'; export class SearchManager extends ApiManager { protected initialize(register: Registration): void { @@ -20,8 +18,10 @@ export class SearchManager extends ApiManager { switch (action) { case 'start': case 'stop': - const status = req.params.action === 'start'; - SolrManager.SetRunning(status); + { + const status = req.params.action === 'start'; + SolrManager.SetRunning(status); + } break; case 'update': await SolrManager.update(); @@ -35,39 +35,12 @@ export class SearchManager extends ApiManager { register({ method: Method.GET, - subscription: '/textsearch', - secureHandler: async ({ req, res }) => { - const q = req.query.q; - if (q === undefined) { - res.send([]); - return; - } - const resObj: { ids: string[]; numFound: number; lines: string[] } = { ids: [], numFound: 0, lines: [] }; - let results: any; - const dir = pathToDirectory(Directory.text); - try { - const regex = new RegExp(q.toString()); - results = await find({ term: q, flags: 'ig' }, dir, '.txt$'); - 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); - } catch (e) { - console.log(red('textsearch:bad RegExp' + q.toString())); - res.send([]); - return; - } - }, - }); - - register({ - method: Method.GET, subscription: '/dashsearch', secureHandler: async ({ req, res }) => { const solrQuery: any = {}; - ['q', 'fq', 'start', 'rows', 'sort', 'hl.maxAnalyzedChars', 'hl', 'hl.fl'].forEach(key => (solrQuery[key] = req.query[key])); + ['q', 'fq', 'start', 'rows', 'sort', 'hl.maxAnalyzedChars', 'hl', 'hl.fl'].forEach(key => { + solrQuery[key] = req.query[key]; + }); if (solrQuery.q === undefined) { res.send([]); return; @@ -98,13 +71,13 @@ export namespace SolrManager { export async function update() { console.log(green('Beginning update...')); - await log_execution<void>({ + await logExecution<void>({ startMessage: 'Clearing existing Solr information...', endMessage: 'Solr information successfully cleared', action: Search.clear, color: cyan, }); - const cursor = await log_execution({ + const cursor = await logExecution({ startMessage: 'Connecting to and querying for all documents from database...', endMessage: ({ result, error }) => { const success = error === null && result !== undefined; @@ -127,30 +100,30 @@ export namespace SolrManager { if (doc.__type !== 'Doc') { return; } - const fields = doc.fields; + const { fields } = doc; if (!fields) { return; } - const update: any = { id: doc._id }; + const update2: any = { id: doc._id }; let dynfield = false; - for (const key in fields) { + fields.forEach((key: any) => { const value = fields[key]; const term = ToSearchTerm(value); if (term !== undefined) { - const { suffix, value } = term; + const { suffix, value: tvalue } = term; if (key.endsWith('modificationDate')) { - update['modificationDate' + suffix] = value; + update2['modificationDate' + suffix] = tvalue; } - update[key + suffix] = value; + update2[key + suffix] = value; dynfield = true; } - } + }); if (dynfield) { - updates.push(update); + updates.push(update2); } } await cursor?.forEach(updateDoc); - const result = await log_execution({ + const result = await logExecution({ startMessage: `Dispatching updates for ${updates.length} documents`, endMessage: 'Dispatched updates complete', action: () => Search.updateDocuments(updates), @@ -188,6 +161,7 @@ export namespace SolrManager { '_l', list => { const results = []; + // eslint-disable-next-line no-restricted-syntax for (const value of list.fields) { const term = ToSearchTerm(value); if (term) { @@ -199,14 +173,15 @@ export namespace SolrManager { ], }; - function ToSearchTerm(val: any): { suffix: string; value: any } | undefined { + function ToSearchTerm(valIn: any): { suffix: string; value: any } | undefined { + let val = valIn; if (val === null || val === undefined) { - return; + return undefined; } const type = val.__type || typeof val; let suffix = suffixMap[type]; if (!suffix) { - return; + return undefined; } if (Array.isArray(suffix)) { @@ -216,6 +191,7 @@ export namespace SolrManager { } else { val = val[accessor]; } + // eslint-disable-next-line prefer-destructuring suffix = suffix[0]; } |
