@openstax/ts-utils 1.50.0 → 1.50.2

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.
@@ -8,6 +8,8 @@ const aws_1 = require("@opensearch-project/opensearch/aws");
8
8
  const index_js_1 = require("../../config/index.js");
9
9
  const index_js_2 = require("../../guards/index.js");
10
10
  const helpers_js_1 = require("../../misc/helpers.js");
11
+ // our Filter DSL is a loose superset of the OpenSearch query DSL, so we cast
12
+ // at this boundary rather than threading exact QueryContainer types through it
11
13
  const mapFilter = (filter) => {
12
14
  if ('key' in filter && 'value' in filter) {
13
15
  const { key } = filter;
@@ -92,35 +94,35 @@ const openSearchService = (initializer = {}) => (configProvider) => {
92
94
  });
93
95
  };
94
96
  const search = async (options) => {
95
- var _a;
96
- const body = {
97
- query: { bool: {} },
98
- track_total_hits: true,
99
- size: pageSize
100
- };
97
+ const bool = {};
98
+ const { must_not, should, must, filter } = options;
101
99
  if (options.query) {
102
- body.query.bool.must = [{
100
+ bool.must = [{
103
101
  multi_match: {
104
102
  fields: options.fields.map((field) => 'weight' in field ? `${field.key}^${field.weight}` : field.key),
105
103
  query: options.query
106
104
  }
107
105
  }];
108
106
  }
109
- const { must_not, should, must, filter } = options;
110
107
  if (filter && filter.length > 0) {
111
- body.query.bool.filter = filter.map(mapFilter);
108
+ bool.filter = filter.map(mapFilter);
112
109
  }
113
110
  if (must && must.length > 0) {
114
- (_a = body.query.bool).must || (_a.must = []);
115
- body.query.bool.must = body.query.bool.must.concat(must.map(mapFilter));
111
+ const existing = bool.must instanceof Array ? bool.must : [];
112
+ bool.must = existing.concat(must.map(mapFilter));
116
113
  }
117
114
  if (must_not && must_not.length > 0) {
118
- body.query.bool.must_not = must_not.map(mapFilter);
115
+ bool.must_not = must_not.map(mapFilter);
119
116
  }
120
117
  if (should && should.length > 0) {
121
- body.query.bool.should = should.map(mapFilter);
122
- body.query.bool.minimum_should_match = 1;
118
+ bool.should = should.map(mapFilter);
119
+ bool.minimum_should_match = 1;
123
120
  }
121
+ const body = {
122
+ query: { bool },
123
+ track_total_hits: true,
124
+ size: pageSize
125
+ };
124
126
  if (options.sort && options.sort.length > 0) {
125
127
  body.sort = options.sort.map(sort => ({
126
128
  [sort.key]: { order: sort.order }
@@ -140,7 +142,7 @@ const openSearchService = (initializer = {}) => (configProvider) => {
140
142
  const items = hits.hits.map((hit) => hit._source).filter(index_js_2.isDefined);
141
143
  const currentPage = options.page || 1;
142
144
  const { total } = hits;
143
- const totalItems = typeof total === 'number' ? total : total.value;
145
+ const totalItems = total === undefined ? 0 : typeof total === 'number' ? total : total.value;
144
146
  const totalPages = Math.ceil(totalItems / pageSize) || 1;
145
147
  return { items, pageSize, currentPage, totalItems, totalPages };
146
148
  };