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