@toxplanet/pegasus-sdk 1.1.3 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/search.js +56 -12
  2. package/package.json +1 -1
package/lib/search.js CHANGED
@@ -310,22 +310,66 @@ class SearchService {
310
310
  elasticsearchService.registerIndexRoute(pattern, {
311
311
  search: async (params) => {
312
312
  const query = params.body?.query;
313
- const searchTerm = query?.match?.chemical_name ||
314
- query?.term?.chemical_name ||
315
- query?.query_string?.query ||
316
- query?.match_all ? '*' : '';
317
- const limit = params.body?.size || 10;
318
-
319
- return await this.searchChemicals(searchTerm, { limit });
313
+ const from = params.body?.from || 0;
314
+ const size = params.body?.size || 10;
315
+
316
+ // Determine search term and type from the ES-style query
317
+ let rawTerm = '';
318
+ let searchFn = (term, limit) => this.searchChemicals(term, { limit });
319
+
320
+ if (query?.query_string?.query) {
321
+ const qs = query.query_string.query;
322
+ if (qs.endsWith('*')) {
323
+ rawTerm = qs.slice(0, -1);
324
+ searchFn = (term, limit) => this.searchStartsWith(term, limit);
325
+ } else {
326
+ rawTerm = qs;
327
+ }
328
+ } else if (query?.match_phrase_prefix) {
329
+ const key = Object.keys(query.match_phrase_prefix)[0];
330
+ const val = query.match_phrase_prefix[key];
331
+ rawTerm = (typeof val === 'object' ? val.query : val) || '';
332
+ searchFn = (term, limit) => this.searchContains(term, limit);
333
+ } else if (query?.match?.chemical_name) {
334
+ rawTerm = query.match.chemical_name;
335
+ } else if (query?.term?.chemical_name) {
336
+ rawTerm = query.term.chemical_name;
337
+ } else if (query?.match_all !== undefined) {
338
+ rawTerm = '*';
339
+ }
340
+
341
+ const pegasusResults = await searchFn(rawTerm, from + size);
342
+ const hits = pegasusResults.results.slice(from);
343
+
344
+ return {
345
+ body: {
346
+ hits: {
347
+ hits: hits.map(r => ({
348
+ _id: r.id,
349
+ _source: {
350
+ chemical_name: r.name,
351
+ chemical_name_sensitive: r.name,
352
+ chemical_name_sort: (r.name || '').toLowerCase(),
353
+ chemical_identifier: [...(r.cas || []), ...(r.identifiers || [])],
354
+ chemical_set_identifier: (r.cas && r.cas[0]) || r.id || '',
355
+ }
356
+ })),
357
+ total: { value: pegasusResults.results.length, relation: 'eq' }
358
+ },
359
+ timed_out: false,
360
+ _shards: { total: 1, successful: 1, failed: 0 }
361
+ }
362
+ };
320
363
  },
321
364
 
322
365
  count: async (params) => {
323
366
  const query = params.body?.query;
324
- const searchTerm = query?.match?.chemical_name ||
325
- query?.term?.chemical_name ||
326
- query?.query_string?.query || '';
327
-
328
- const results = await this.searchChemicals(searchTerm, { limit: 10000 });
367
+ let rawTerm = query?.match?.chemical_name ||
368
+ query?.term?.chemical_name ||
369
+ query?.query_string?.query || '';
370
+ if (rawTerm.endsWith('*')) rawTerm = rawTerm.slice(0, -1);
371
+
372
+ const results = await this.searchChemicals(rawTerm, { limit: 10000 });
329
373
  return { count: results.results.length };
330
374
  }
331
375
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toxplanet/pegasus-sdk",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "SDK for migrating chemical data to Pegasus PostgreSQL + OpenSearch architecture with Elasticsearch client compatibility",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",