@openstax/ts-utils 1.25.4 → 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;
@@ -26,6 +26,10 @@ const memorySearchTheBadWay = () => ({ store }) => {
26
26
  let weight = 0;
27
27
  const matchFilters = (filters, matchType) => {
28
28
  for (const field of filters) {
29
+ if (!('key' in field && 'value' in field)) {
30
+ console.warn('local search only supports key/value filters');
31
+ continue;
32
+ }
29
33
  const docValues = (0, __1.coerceArray)(resolveField(document, field));
30
34
  const coerceValue = getFieldType(field) === 'boolean'
31
35
  ? (input) => {
@@ -57,6 +57,7 @@ const openSearchService = (initializer = {}) => (configProvider) => {
57
57
  const body = {
58
58
  query: { bool: {} },
59
59
  track_total_hits: true,
60
+ size: pageSize
60
61
  };
61
62
  if (options.query) {
62
63
  body.query.bool.must = {
@@ -71,24 +72,39 @@ const openSearchService = (initializer = {}) => (configProvider) => {
71
72
  if (must && must.length > 0) {
72
73
  body.query.bool.filter = [];
73
74
  must.forEach((filter) => {
74
- const { key } = filter;
75
- const values = filter.value instanceof Array ? filter.value : [filter.value];
76
- body.query.bool.filter.push({ terms: { [key]: values } });
75
+ if ('key' in filter && 'value' in filter) {
76
+ const { key } = filter;
77
+ const values = filter.value instanceof Array ? filter.value : [filter.value];
78
+ body.query.bool.filter.push({ terms: { [key]: values } });
79
+ }
80
+ else {
81
+ body.query.bool.filter.push(filter);
82
+ }
77
83
  });
78
84
  }
79
85
  if (must_not && must_not.length > 0) {
80
86
  body.query.bool.must_not = [];
81
87
  must_not.forEach((filter) => {
82
- const { key } = filter;
83
- const values = filter.value instanceof Array ? filter.value : [filter.value];
84
- values.forEach((value) => body.query.bool.must_not.push({ term: { [key]: value } }));
88
+ if ('key' in filter && 'value' in filter) {
89
+ const { key } = filter;
90
+ const values = filter.value instanceof Array ? filter.value : [filter.value];
91
+ values.forEach((value) => body.query.bool.must_not.push({ term: { [key]: value } }));
92
+ }
93
+ else {
94
+ body.query.bool.must_not.push(filter);
95
+ }
85
96
  });
86
97
  }
87
98
  if (options.should && options.should.length > 0) {
88
- body.query.bool.should = options.should.map(term => {
89
- const { key } = term;
90
- const values = term.value instanceof Array ? term.value : [term.value];
91
- return { terms: { [key]: values } };
99
+ body.query.bool.should = options.should.map(filter => {
100
+ if ('key' in filter && 'value' in filter) {
101
+ const { key } = filter;
102
+ const values = filter.value instanceof Array ? filter.value : [filter.value];
103
+ return { terms: { [key]: values } };
104
+ }
105
+ else {
106
+ return filter;
107
+ }
92
108
  });
93
109
  body.query.bool.minimum_should_match = 1;
94
110
  }
@@ -98,7 +114,6 @@ const openSearchService = (initializer = {}) => (configProvider) => {
98
114
  }));
99
115
  }
100
116
  if (options.page) {
101
- body.size = pageSize;
102
117
  body.from = (options.page - 1) * pageSize;
103
118
  }
104
119
  const response = await (await client()).search({