Skip to content

Commit d808d0d

Browse files
authored
Implemented getHighlightQuery (#13)
1 parent 8afc7eb commit d808d0d

File tree

6 files changed

+227
-6
lines changed

6 files changed

+227
-6
lines changed

__test__/query.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ describe(`generates search query correctly`, () => {
2626
];
2727
const query = ref.query(testQuery);
2828

29-
console.log(`text query: `, JSON.stringify(query));
3029
it(`should have correct mongo format for searchQuery`, () => {
3130
const expected = [
3231
{
@@ -87,7 +86,6 @@ describe(`generate geo query correctly`, () => {
8786
},
8887
];
8988
const query = ref.query(testQuery);
90-
console.log(`geo query: `, JSON.stringify(query));
9189
it(`should have correct mongo format for geo query`, () => {
9290
const expected = [
9391
{
@@ -193,8 +191,6 @@ describe(`generates range query correctly`, () => {
193191
];
194192
const query = ref.query(testQuery);
195193

196-
console.log(`range query: `, JSON.stringify(query));
197-
198194
it(`should have correct mongo format for range query`, () => {
199195
const expected = [
200196
{
@@ -304,7 +300,6 @@ describe(`generate term query correctly`, () => {
304300
];
305301

306302
const query = ref.query(testQuery);
307-
console.log(`term query: `, JSON.stringify(query));
308303
it(`should have correct mongo format for term query`, () => {
309304
const expected = [
310305
{

__test__/targets/common.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,21 @@ test('getFuzziness when fuzziness is 0', () => {
157157
const expected = {};
158158
expect(result).toStrictEqual(expected);
159159
});
160+
161+
test('getIncludeExcludeFields when includeFields, excludeFields contains some column and highlight is true', () => {
162+
const result = getIncludeExcludeFields({
163+
excludeFields: ['test'],
164+
includeFields: ['test1', 'test2'],
165+
highlight: true,
166+
});
167+
const expected = {
168+
$project: {
169+
test: 0,
170+
test1: 1,
171+
test2: 1,
172+
highlights: { $meta: 'searchHighlights' },
173+
},
174+
};
175+
// Snapshot demo
176+
expect(result).toStrictEqual(expected);
177+
});

__test__/targets/search.test.ts

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
getHighlightQuery,
23
getQueryStringQuery,
34
getSearchSortByQuery,
45
} from '../../src/targets/search';
@@ -181,3 +182,149 @@ test('getQueryStringQuery when datafield is an object array and queryString is t
181182
// Snapshot demo
182183
expect(result).toStrictEqual(expected);
183184
});
185+
186+
test('getHighlightQuery when highlight is missing', () => {
187+
const result = getHighlightQuery({
188+
dataField: [
189+
{ field: 'data1', weight: 1 },
190+
{ field: 'data2', weight: 1 },
191+
{ field: 'data3', weight: 1 },
192+
],
193+
sortBy: `desc`,
194+
});
195+
const expected = {};
196+
// Snapshot demo
197+
expect(result).toStrictEqual(expected);
198+
});
199+
200+
test('getHighlightQuery when highlight is false', () => {
201+
const result = getHighlightQuery({
202+
dataField: [
203+
{ field: 'data1', weight: 1 },
204+
{ field: 'data2', weight: 1 },
205+
{ field: 'data3', weight: 1 },
206+
],
207+
sortBy: `desc`,
208+
highlight: false,
209+
});
210+
const expected = {};
211+
// Snapshot demo
212+
expect(result).toStrictEqual(expected);
213+
});
214+
215+
test('getHighlightQuery when highlight is true and datafield is an array of DataField', () => {
216+
const result = getHighlightQuery({
217+
dataField: [
218+
{ field: 'data1', weight: 1 },
219+
{ field: 'data2', weight: 1 },
220+
{ field: 'data3', weight: 1 },
221+
],
222+
sortBy: `desc`,
223+
highlight: true,
224+
});
225+
const expected = {
226+
highlight: {
227+
path: ['data1', 'data2', 'data3'],
228+
maxCharsToExamine: 500000,
229+
maxNumPassages: 5,
230+
},
231+
};
232+
// Snapshot demo
233+
expect(result).toStrictEqual(expected);
234+
});
235+
236+
test('getHighlightQuery when highlight is true and datafield is an array of DataField', () => {
237+
const result = getHighlightQuery({
238+
dataField: [
239+
{ field: 'data1', weight: 1 },
240+
{ field: 'data2', weight: 1 },
241+
{ field: 'data3', weight: 1 },
242+
],
243+
sortBy: `desc`,
244+
highlight: true,
245+
});
246+
const expected = {
247+
highlight: {
248+
path: ['data1', 'data2', 'data3'],
249+
maxCharsToExamine: 500000,
250+
maxNumPassages: 5,
251+
},
252+
};
253+
// Snapshot demo
254+
expect(result).toStrictEqual(expected);
255+
});
256+
257+
test('getHighlightQuery when highlight is true and datafield is an array of string', () => {
258+
const result = getHighlightQuery({
259+
dataField: ['data1', 'data2', 'data3'],
260+
sortBy: `desc`,
261+
highlight: true,
262+
});
263+
const expected = {
264+
highlight: {
265+
path: ['data1', 'data2', 'data3'],
266+
maxCharsToExamine: 500000,
267+
maxNumPassages: 5,
268+
},
269+
};
270+
// Snapshot demo
271+
expect(result).toStrictEqual(expected);
272+
});
273+
274+
test('getHighlightQuery when highlight is true and highlightField is a string', () => {
275+
const result = getHighlightQuery({
276+
dataField: 'data',
277+
sortBy: `desc`,
278+
highlight: true,
279+
highlightField: 'field1',
280+
});
281+
const expected = {
282+
highlight: {
283+
path: ['field1'],
284+
maxCharsToExamine: 500000,
285+
maxNumPassages: 5,
286+
},
287+
};
288+
// Snapshot demo
289+
expect(result).toStrictEqual(expected);
290+
});
291+
292+
test('getHighlightQuery when highlight is true and highlightField is an array', () => {
293+
const result = getHighlightQuery({
294+
dataField: 'data',
295+
sortBy: `desc`,
296+
highlight: true,
297+
highlightField: ['field1', 'field2'],
298+
});
299+
const expected = {
300+
highlight: {
301+
path: ['field1', 'field2'],
302+
maxCharsToExamine: 500000,
303+
maxNumPassages: 5,
304+
},
305+
};
306+
// Snapshot demo
307+
expect(result).toStrictEqual(expected);
308+
});
309+
310+
test('getHighlightQuery when highlight is true and customHighlight is passed', () => {
311+
const result = getHighlightQuery({
312+
dataField: 'data',
313+
sortBy: `desc`,
314+
highlight: true,
315+
highlightField: ['field1', 'field2'],
316+
customHighlight: {
317+
maxCharsToExamine: 250000,
318+
maxNumPassages: 10,
319+
},
320+
});
321+
const expected = {
322+
highlight: {
323+
path: ['field1', 'field2'],
324+
maxCharsToExamine: 250000,
325+
maxNumPassages: 10,
326+
},
327+
};
328+
// Snapshot demo
329+
expect(result).toStrictEqual(expected);
330+
});

src/targets/common.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ export const getIncludeExcludeFields = (query: RSQuery<any>): any => {
5252
$project: { ...excludeAggregation, ...includeAggregation },
5353
};
5454

55+
if (query.highlight) {
56+
res.$project = {
57+
...res.$project,
58+
highlights: { $meta: 'searchHighlights' },
59+
};
60+
}
61+
5562
if (Object.keys(res).length) {
5663
return res;
5764
}

src/targets/search.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,57 @@ const _getFirstDataFieldValue = (
102102
}
103103
return field;
104104
};
105+
106+
export const getHighlightQuery = (query: RSQuery<string>): any => {
107+
const {
108+
highlight = false,
109+
highlightField,
110+
customHighlight,
111+
dataField,
112+
} = query;
113+
const { maxCharsToExamine = 500000, maxNumPassages = 5 } =
114+
customHighlight || {};
115+
116+
if (highlight) {
117+
let fields: string[] = [];
118+
if (highlightField) {
119+
if (typeof highlightField === 'string') {
120+
fields = [highlightField as string];
121+
} else {
122+
fields = highlightField as string[];
123+
}
124+
} else {
125+
if (dataField) {
126+
if (typeof dataField === 'string') {
127+
fields = [dataField];
128+
} else {
129+
// It's an array
130+
if (dataField.length > 0) {
131+
const queryField = dataField[0];
132+
if (
133+
typeof queryField === 'string' ||
134+
queryField instanceof String
135+
) {
136+
fields = dataField as string[];
137+
} else {
138+
fields = dataField.map((value: any) => value.field);
139+
}
140+
} else {
141+
return {};
142+
}
143+
}
144+
} else {
145+
return {};
146+
}
147+
}
148+
return {
149+
highlight: {
150+
path: fields,
151+
maxCharsToExamine,
152+
maxNumPassages,
153+
},
154+
};
155+
} else {
156+
return {};
157+
}
158+
};

src/types/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export type RSQuery<T> = {
149149

150150
highlightField?: string | Array<string>;
151151

152-
customHighlight?: Object;
152+
customHighlight?: { maxCharsToExamine: number; maxNumPassages: number };
153153

154154
interval?: number;
155155

0 commit comments

Comments
 (0)