@openstax/ts-utils 1.25.3 → 1.25.5

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.
@@ -1,6 +1,12 @@
1
1
  export declare type Filter = {
2
2
  key: string;
3
3
  value: string | string[] | boolean;
4
+ } | {
5
+ terms: Record<string, string | string[] | number | boolean>;
6
+ } | {
7
+ exists: {
8
+ field: string;
9
+ };
4
10
  };
5
11
  declare type Field = {
6
12
  key: string;
@@ -23,6 +23,10 @@ export const memorySearchTheBadWay = () => ({ store }) => {
23
23
  let weight = 0;
24
24
  const matchFilters = (filters, matchType) => {
25
25
  for (const field of filters) {
26
+ if (!('key' in field && 'value' in field)) {
27
+ console.warn('local search only supports key/value filters');
28
+ continue;
29
+ }
26
30
  const docValues = coerceArray(resolveField(document, field));
27
31
  const coerceValue = getFieldType(field) === 'boolean'
28
32
  ? (input) => {
@@ -54,6 +54,7 @@ export const openSearchService = (initializer = {}) => (configProvider) => {
54
54
  const body = {
55
55
  query: { bool: {} },
56
56
  track_total_hits: true,
57
+ size: pageSize
57
58
  };
58
59
  if (options.query) {
59
60
  body.query.bool.must = {
@@ -68,24 +69,39 @@ export const openSearchService = (initializer = {}) => (configProvider) => {
68
69
  if (must && must.length > 0) {
69
70
  body.query.bool.filter = [];
70
71
  must.forEach((filter) => {
71
- const { key } = filter;
72
- const values = filter.value instanceof Array ? filter.value : [filter.value];
73
- body.query.bool.filter.push({ terms: { [key]: values } });
72
+ if ('key' in filter && 'value' in filter) {
73
+ const { key } = filter;
74
+ const values = filter.value instanceof Array ? filter.value : [filter.value];
75
+ body.query.bool.filter.push({ terms: { [key]: values } });
76
+ }
77
+ else {
78
+ body.query.bool.filter.push(filter);
79
+ }
74
80
  });
75
81
  }
76
82
  if (must_not && must_not.length > 0) {
77
83
  body.query.bool.must_not = [];
78
84
  must_not.forEach((filter) => {
79
- const { key } = filter;
80
- const values = filter.value instanceof Array ? filter.value : [filter.value];
81
- values.forEach((value) => body.query.bool.must_not.push({ term: { [key]: value } }));
85
+ if ('key' in filter && 'value' in filter) {
86
+ const { key } = filter;
87
+ const values = filter.value instanceof Array ? filter.value : [filter.value];
88
+ values.forEach((value) => body.query.bool.must_not.push({ term: { [key]: value } }));
89
+ }
90
+ else {
91
+ body.query.bool.must_not.push(filter);
92
+ }
82
93
  });
83
94
  }
84
95
  if (options.should && options.should.length > 0) {
85
- body.query.bool.should = options.should.map(term => {
86
- const { key } = term;
87
- const values = term.value instanceof Array ? term.value : [term.value];
88
- return { terms: { [key]: values } };
96
+ body.query.bool.should = options.should.map(filter => {
97
+ if ('key' in filter && 'value' in filter) {
98
+ const { key } = filter;
99
+ const values = filter.value instanceof Array ? filter.value : [filter.value];
100
+ return { terms: { [key]: values } };
101
+ }
102
+ else {
103
+ return filter;
104
+ }
89
105
  });
90
106
  body.query.bool.minimum_should_match = 1;
91
107
  }
@@ -95,7 +111,6 @@ export const openSearchService = (initializer = {}) => (configProvider) => {
95
111
  }));
96
112
  }
97
113
  if (options.page) {
98
- body.size = pageSize;
99
114
  body.from = (options.page - 1) * pageSize;
100
115
  }
101
116
  const response = await (await client()).search({