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