@openstax/ts-utils 1.24.2-pre3 → 1.24.3

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.
@@ -12,10 +12,10 @@ export declare type IndexConfig = {
12
12
  mappings: Record<string, any>;
13
13
  pageSize?: number;
14
14
  };
15
- export declare const openSearchService: <C extends string = "deployed">(initializer?: Initializer<C>) => <T>(indexConfig: IndexConfig, configProvider: { [key in C]: {
15
+ export declare const openSearchService: <C extends string = "deployed">(initializer?: Initializer<C>) => <T>(configProvider: { [key in C]: {
16
16
  node: import("../../config").ConfigValueProvider<string>;
17
17
  region: import("../../config").ConfigValueProvider<string>;
18
- }; }) => {
18
+ }; }) => (indexConfig: IndexConfig) => {
19
19
  ensureIndexCreated: () => Promise<void>;
20
20
  index: (params: IndexOptions<T>) => Promise<void>;
21
21
  search: (options: SearchOptions) => Promise<{
@@ -5,9 +5,8 @@ import { AwsSigv4Signer } from '@opensearch-project/opensearch/aws';
5
5
  import { resolveConfigValue } from '../../config';
6
6
  import { ifDefined, isDefined } from '../../guards';
7
7
  import { once } from '../../misc/helpers';
8
- export const openSearchService = (initializer = {}) => (indexConfig, configProvider) => {
8
+ export const openSearchService = (initializer = {}) => (configProvider) => {
9
9
  const config = configProvider[ifDefined(initializer.configSpace, 'deployed')];
10
- const pageSize = indexConfig.pageSize || 10;
11
10
  const client = once(async () => new Client({
12
11
  ...AwsSigv4Signer({
13
12
  getCredentials: () => defaultProvider()(),
@@ -16,88 +15,91 @@ export const openSearchService = (initializer = {}) => (indexConfig, configProvi
16
15
  }),
17
16
  node: await resolveConfigValue(config.node),
18
17
  }));
19
- const createIndexIfNotExists = async (indices, params) => {
20
- const { index } = params;
21
- const { body } = await indices.exists({ index });
22
- if (!body) {
23
- await indices.create(params);
24
- }
25
- };
26
- const ensureIndexCreated = async () => {
27
- const { indices } = await client();
28
- await createIndexIfNotExists(indices, {
29
- index: indexConfig.name,
30
- body: {
31
- mappings: {
32
- dynamic: false,
33
- properties: indexConfig.mappings
34
- }
18
+ return (indexConfig) => {
19
+ const pageSize = indexConfig.pageSize || 10;
20
+ const createIndexIfNotExists = async (indices, params) => {
21
+ const { index } = params;
22
+ const { body } = await indices.exists({ index });
23
+ if (!body) {
24
+ await indices.create(params);
35
25
  }
36
- });
37
- };
38
- const index = async (params) => {
39
- const openSearchClient = await client();
40
- await openSearchClient.index({
41
- index: indexConfig.name,
42
- body: params.body,
43
- id: params.id,
44
- refresh: true
45
- });
46
- };
47
- const search = async (options) => {
48
- const body = { query: { bool: {} } };
49
- if (options.query) {
50
- body.query.bool.must = {
51
- multi_match: {
52
- fields: options.fields.map((field) => 'weight' in field ? `${field.key}^${field.weight}` : field.key),
53
- query: options.query
26
+ };
27
+ const ensureIndexCreated = async () => {
28
+ const { indices } = await client();
29
+ await createIndexIfNotExists(indices, {
30
+ index: indexConfig.name,
31
+ body: {
32
+ mappings: {
33
+ dynamic: false,
34
+ properties: indexConfig.mappings
35
+ }
54
36
  }
55
- };
56
- }
57
- const { must_not } = options;
58
- const must = 'filter' in options ? options.filter : options.must;
59
- if (must && must.length > 0) {
60
- body.query.bool.filter = [];
61
- must.forEach((filter) => {
62
- const { key } = filter;
63
- const values = filter.value instanceof Array ? filter.value : [filter.value];
64
- body.query.bool.filter.push({ terms: { [key]: values } });
65
37
  });
66
- }
67
- if (must_not && must_not.length > 0) {
68
- body.query.bool.must_not = [];
69
- must_not.forEach((filter) => {
70
- const { key } = filter;
71
- const values = filter.value instanceof Array ? filter.value : [filter.value];
72
- values.forEach((value) => body.query.bool.must_not.push({ term: { [key]: value } }));
38
+ };
39
+ const index = async (params) => {
40
+ const openSearchClient = await client();
41
+ await openSearchClient.index({
42
+ index: indexConfig.name,
43
+ body: params.body,
44
+ id: params.id,
45
+ refresh: true
73
46
  });
74
- }
75
- if (options.should && options.should.length > 0) {
76
- body.query.bool.should = options.should.map(term => {
77
- const { key } = term;
78
- const values = term.value instanceof Array ? term.value : [term.value];
79
- return { terms: { [key]: values } };
47
+ };
48
+ const search = async (options) => {
49
+ const body = { query: { bool: {} } };
50
+ if (options.query) {
51
+ body.query.bool.must = {
52
+ multi_match: {
53
+ fields: options.fields.map((field) => 'weight' in field ? `${field.key}^${field.weight}` : field.key),
54
+ query: options.query
55
+ }
56
+ };
57
+ }
58
+ const { must_not } = options;
59
+ const must = 'filter' in options ? options.filter : options.must;
60
+ if (must && must.length > 0) {
61
+ body.query.bool.filter = [];
62
+ must.forEach((filter) => {
63
+ const { key } = filter;
64
+ const values = filter.value instanceof Array ? filter.value : [filter.value];
65
+ body.query.bool.filter.push({ terms: { [key]: values } });
66
+ });
67
+ }
68
+ if (must_not && must_not.length > 0) {
69
+ body.query.bool.must_not = [];
70
+ must_not.forEach((filter) => {
71
+ const { key } = filter;
72
+ const values = filter.value instanceof Array ? filter.value : [filter.value];
73
+ values.forEach((value) => body.query.bool.must_not.push({ term: { [key]: value } }));
74
+ });
75
+ }
76
+ if (options.should && options.should.length > 0) {
77
+ body.query.bool.should = options.should.map(term => {
78
+ const { key } = term;
79
+ const values = term.value instanceof Array ? term.value : [term.value];
80
+ return { terms: { [key]: values } };
81
+ });
82
+ body.query.bool.minimum_should_match = 1;
83
+ }
84
+ if (options.page) {
85
+ body.size = pageSize;
86
+ body.from = (options.page - 1) * pageSize;
87
+ }
88
+ const response = await (await client()).search({
89
+ body,
90
+ index: indexConfig.name
80
91
  });
81
- body.query.bool.minimum_should_match = 1;
82
- }
83
- if (options.page) {
84
- body.size = pageSize;
85
- body.from = (options.page - 1) * pageSize;
86
- }
87
- const response = await (await client()).search({
88
- body,
89
- index: indexConfig.name
90
- });
91
- if (response.statusCode !== 200) {
92
- throw new Error(`Unexpected status code: ${response.statusCode} from OpenSearch`);
93
- }
94
- const hits = response.body.hits;
95
- const items = hits.hits.map((hit) => hit._source).filter(isDefined);
96
- const currentPage = options.page || 1;
97
- const { total } = hits;
98
- const totalItems = typeof total === 'number' ? total : total.value;
99
- const totalPages = Math.ceil(totalItems / pageSize) || 1;
100
- return { items, pageSize, currentPage, totalItems, totalPages };
92
+ if (response.statusCode !== 200) {
93
+ throw new Error(`Unexpected status code: ${response.statusCode} from OpenSearch`);
94
+ }
95
+ const hits = response.body.hits;
96
+ const items = hits.hits.map((hit) => hit._source).filter(isDefined);
97
+ const currentPage = options.page || 1;
98
+ const { total } = hits;
99
+ const totalItems = typeof total === 'number' ? total : total.value;
100
+ const totalPages = Math.ceil(totalItems / pageSize) || 1;
101
+ return { items, pageSize, currentPage, totalItems, totalPages };
102
+ };
103
+ return { ensureIndexCreated, index, search };
101
104
  };
102
- return { ensureIndexCreated, index, search };
103
105
  };