@outliant/sunrise-utils 1.3.5 → 1.3.6

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.
package/CHANGELOG.md CHANGED
@@ -4,9 +4,16 @@ All notable changes to this project will be documented in this file. Dates are d
4
4
 
5
5
  Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
6
6
 
7
+ #### [1.3.6](https://github.com/outliant/sunrise-utils/compare/1.3.5...1.3.6)
8
+
9
+ - chore: update project global search #86b428rnf [`#45`](https://github.com/outliant/sunrise-utils/pull/45)
10
+
7
11
  #### [1.3.5](https://github.com/outliant/sunrise-utils/compare/1.3.4...1.3.5)
8
12
 
13
+ > 14 March 2025
14
+
9
15
  - chore: global search updates #86b428rnf [`#44`](https://github.com/outliant/sunrise-utils/pull/44)
16
+ - chore(release): 1.3.5 [`18aa190`](https://github.com/outliant/sunrise-utils/commit/18aa190de0982257a215fa3060f1158b47269fed)
10
17
 
11
18
  #### [1.3.4](https://github.com/outliant/sunrise-utils/compare/1.3.3...1.3.4)
12
19
 
@@ -2,7 +2,7 @@ const { validate: isUuid } = require('uuid');
2
2
  const { escapeElasticQuery } = require('./es');
3
3
  const _ = require('lodash');
4
4
 
5
- module.exports = (query = {}, searchFields = []) => {
5
+ module.exports = (query = {}, searchFields = [], isGlobalSearch = false) => {
6
6
  const search = query.search;
7
7
  let searchString = search.trim();
8
8
 
@@ -19,27 +19,37 @@ module.exports = (query = {}, searchFields = []) => {
19
19
  const localFields = searchFields.filter((x) => x !== '_id' && !isUuid(x));
20
20
 
21
21
  if (searchFields.includes('_id')) {
22
- should.push({
23
- term: {
24
- _id: {
25
- value: searchString
26
- }
27
- }
28
- });
22
+ should.push(
23
+ isGlobalSearch
24
+ ? {
25
+ terms: {
26
+ _id: _.split(searchString, ' ') || []
27
+ }
28
+ }
29
+ : {
30
+ term: {
31
+ _id: {
32
+ value: searchString
33
+ }
34
+ }
35
+ }
36
+ );
29
37
  }
30
38
 
31
- /**
32
- * For case insensitive id search with hyphen
33
- */
34
- _.split(search, '-').forEach(keyword => {
35
- should.push({
36
- wildcard: {
37
- id: {
38
- value: `*${keyword}*`
39
+ if (!isGlobalSearch) {
40
+ /**
41
+ * For case insensitive id search with hyphen
42
+ */
43
+ _.split(search, '-').forEach(keyword => {
44
+ should.push({
45
+ wildcard: {
46
+ id: {
47
+ value: `*${keyword}*`
48
+ }
39
49
  }
40
- }
50
+ });
41
51
  });
42
- });
52
+ }
43
53
 
44
54
  if (localFields.length > 0) {
45
55
  if (isQueryQuoteWrapped) {
@@ -17,7 +17,8 @@ class ProjectPipeline {
17
17
  departmentId,
18
18
  filters = [],
19
19
  query = {},
20
- searchFields = []
20
+ searchFields = [],
21
+ isGlobalSearch = false
21
22
  ) {
22
23
  const mustQuery = [
23
24
  {
@@ -48,7 +49,7 @@ class ProjectPipeline {
48
49
  }
49
50
 
50
51
  if (query.search && query.search.length > 0) {
51
- const should = searchFilter(query, searchFields);
52
+ const should = searchFilter(query, searchFields, isGlobalSearch);
52
53
 
53
54
  if (should.length > 0) {
54
55
  mustQuery.push({
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@outliant/sunrise-utils",
3
3
  "description": "Helper functions for project Sunrise",
4
- "version": "1.3.5",
4
+ "version": "1.3.6",
5
5
  "license": "ISC",
6
6
  "author": "Outliant",
7
7
  "main": "index.js",
@@ -6,9 +6,10 @@ const { expect } = require('chai');
6
6
  const projectPipeline = require('../../lib/projectPipeline');
7
7
 
8
8
  describe('projectPipeline', function () {
9
+ const ORG_ID = 'orgId';
10
+ const DEPARTMENT_ID = 'department_id';
11
+
9
12
  describe('buildFiltersQuery', () => {
10
- const ORG_ID = 'orgId';
11
- const DEPARTMENT_ID = 'department_id';
12
13
  const entries = [
13
14
  {
14
15
  search: 'COM-41853',
@@ -164,75 +165,24 @@ describe('projectPipeline', function () {
164
165
  ]
165
166
  }
166
167
  }
167
- },
168
- {
169
- search: '41853',
170
- departmentId: [DEPARTMENT_ID, 'test-departmentId-2'],
171
- expected: {
172
- bool: {
173
- must: [
174
- {
175
- term: {
176
- organization_id: {
177
- value: ORG_ID
178
- }
179
- }
180
- },
181
- {
182
- terms: {
183
- department_id: [
184
- DEPARTMENT_ID,
185
- 'test-departmentId-2'
186
- ]
187
- }
188
- },
189
- {
190
- bool: {
191
- should: [
192
- {
193
- wildcard: {
194
- id: {
195
- value: '*41853*'
196
- }
197
- }
198
- },
199
- {
200
- multi_match: {
201
- query: '41853',
202
- fields: ['project_id^5']
203
- }
204
- },
205
- {
206
- match_phrase_prefix: {
207
- name: '41853'
208
- }
209
- }
210
- ]
211
- }
212
- }
213
- ]
214
- }
215
- }
216
168
  }
217
169
  ];
218
170
 
219
171
  entries.forEach((entry) => {
220
- it(`it should search Project ID correctly for ${entry.search}`, (done) => {
172
+ it(`it should search Project ID correctly for ${entry.search}`, () => {
221
173
  const query = {
222
174
  search: entry.search
223
175
  };
224
176
 
225
177
  const result = projectPipeline.buildFiltersQuery(
226
178
  ORG_ID,
227
- entry.departmentId || DEPARTMENT_ID,
179
+ DEPARTMENT_ID,
228
180
  [],
229
181
  query,
230
- ['project_id']
182
+ ['project_id'],
231
183
  );
232
184
 
233
185
  expect(result).to.deep.equal(entry.expected);
234
-
235
- done();
236
186
  });
237
187
  });
238
188
  });
@@ -321,5 +271,91 @@ describe('projectPipeline', function () {
321
271
  }
322
272
  });
323
273
  });
274
+
275
+ it('should generate es query source params for global search', () => {
276
+ const params = {
277
+ search: '41853 SER-69 COM-69',
278
+ projectSearchFields: [
279
+ 'name',
280
+ 'id',
281
+ '_id',
282
+ '4ef2760a-083a-466b-b761-667856aeb5ee'
283
+ ]
284
+ };
285
+ const query = {
286
+ search: params.search
287
+ };
288
+
289
+ const result = projectPipeline.buildFiltersQuery(
290
+ ORG_ID,
291
+ DEPARTMENT_ID,
292
+ [],
293
+ query,
294
+ params.projectSearchFields,
295
+ true
296
+ );
297
+
298
+ expect(result).to.be.deep.equal({
299
+ bool: {
300
+ must: [
301
+ { term: { organization_id: { value: ORG_ID } } },
302
+ { term: { department_id: { value: DEPARTMENT_ID } } },
303
+ {
304
+ bool: {
305
+ should: [
306
+ { terms: { _id: params.search.split(' ') } },
307
+ { multi_match: { query: params.search, fields: ['name^5', 'id^5'] } },
308
+ { match_phrase_prefix: { name: params.search } },
309
+ {
310
+ function_score: {
311
+ query: {
312
+ nested: {
313
+ path: 'fields',
314
+ query: {
315
+ bool: {
316
+ must: [
317
+ { term: { 'fields.id': { value: params.projectSearchFields[3] } } },
318
+ { match: { 'fields.text.analyzed': { query: params.search, operator: 'or' } } }
319
+ ]
320
+ }
321
+ }
322
+ }
323
+ },
324
+ min_score: 15
325
+ }
326
+ },
327
+ {
328
+ bool: {
329
+ must: [
330
+ {
331
+ nested: {
332
+ path: 'fields',
333
+ query: {
334
+ bool: {
335
+ must: [
336
+ { term: { 'fields.id': { value: params.projectSearchFields[3] } } },
337
+ {
338
+ bool: {
339
+ should: [
340
+ { match: { 'fields.text.analyzed': { query: params.search, operator: 'and' } } },
341
+ { match_phrase_prefix: { 'fields.text.analyzed': params.search } }
342
+ ]
343
+ }
344
+ }
345
+ ]
346
+ }
347
+ }
348
+ }
349
+ }
350
+ ]
351
+ }
352
+ }
353
+ ]
354
+ }
355
+ }
356
+ ]
357
+ }
358
+ });
359
+ });
324
360
  });
325
361
  });