Skip to content

Commit 663ba99

Browse files
Implicitly update input maps defined on non-expanded queries (common cases) (#632)
Signed-off-by: Tyler Ohlsen <ohltyler@amazon.com> (cherry picked from commit 8c0df10) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 860a23d commit 663ba99

File tree

3 files changed

+317
-18
lines changed

3 files changed

+317
-18
lines changed

common/constants.ts

+125-8
Original file line numberDiff line numberDiff line change
@@ -301,27 +301,108 @@ export const VECTOR_TEMPLATE_PLACEHOLDER = `\$\{${VECTOR}\}`;
301301
export const DEFAULT_K = 10;
302302
export const DEFAULT_FETCH_SIZE = 10;
303303

304-
export const FETCH_ALL_QUERY = {
304+
// term-level queries
305+
export const TERM_QUERY_TEXT = {
305306
query: {
306-
match_all: {},
307+
term: {
308+
[TEXT_FIELD_PATTERN]: {
309+
value: QUERY_TEXT_PATTERN,
310+
},
311+
},
307312
},
308-
size: DEFAULT_FETCH_SIZE,
309313
};
310-
export const FETCH_ALL_QUERY_LARGE = {
314+
export const EXISTS_QUERY_TEXT = {
311315
query: {
312-
match_all: {},
316+
exists: {
317+
field: TEXT_FIELD_PATTERN,
318+
},
313319
},
314-
size: 1000,
315320
};
316-
export const TERM_QUERY_TEXT = {
321+
export const FUZZY_QUERY_TEXT = {
317322
query: {
318-
term: {
323+
fuzzy: {
319324
[TEXT_FIELD_PATTERN]: {
320325
value: QUERY_TEXT_PATTERN,
321326
},
322327
},
323328
},
324329
};
330+
export const WILDCARD_QUERY_TEXT = {
331+
query: {
332+
wildcard: {
333+
[TEXT_FIELD_PATTERN]: {
334+
wildcard: QUERY_TEXT_PATTERN,
335+
case_insensitive: false,
336+
},
337+
},
338+
},
339+
};
340+
export const PREFIX_QUERY_TEXT = {
341+
query: {
342+
prefix: {
343+
[TEXT_FIELD_PATTERN]: {
344+
value: QUERY_TEXT_PATTERN,
345+
},
346+
},
347+
},
348+
};
349+
// full-text queries
350+
export const MATCH_QUERY_TEXT = {
351+
query: {
352+
match: {
353+
[TEXT_FIELD_PATTERN]: {
354+
query: QUERY_TEXT_PATTERN,
355+
},
356+
},
357+
},
358+
};
359+
export const MATCH_BOOLEAN_QUERY_TEXT = {
360+
query: {
361+
match_bool_prefix: {
362+
[TEXT_FIELD_PATTERN]: {
363+
query: QUERY_TEXT_PATTERN,
364+
},
365+
},
366+
},
367+
};
368+
export const MATCH_PHRASE_QUERY_TEXT = {
369+
query: {
370+
match_phrase: {
371+
[TEXT_FIELD_PATTERN]: {
372+
query: QUERY_TEXT_PATTERN,
373+
},
374+
},
375+
},
376+
};
377+
export const MATCH_PHRASE_PREFIX_QUERY_TEXT = {
378+
query: {
379+
match_phrase_prefix: {
380+
[TEXT_FIELD_PATTERN]: {
381+
query: QUERY_TEXT_PATTERN,
382+
},
383+
},
384+
},
385+
};
386+
export const QUERY_STRING_QUERY_TEXT = {
387+
query: {
388+
query_string: {
389+
query: QUERY_TEXT_PATTERN,
390+
},
391+
},
392+
};
393+
// misc / other queries
394+
export const FETCH_ALL_QUERY = {
395+
query: {
396+
match_all: {},
397+
},
398+
size: DEFAULT_FETCH_SIZE,
399+
};
400+
export const FETCH_ALL_QUERY_LARGE = {
401+
query: {
402+
match_all: {},
403+
},
404+
size: 1000,
405+
};
325406
export const KNN_QUERY = {
326407
_source: {
327408
excludes: [VECTOR_FIELD_PATTERN],
@@ -471,6 +552,42 @@ export const QUERY_PRESETS = [
471552
name: 'Term',
472553
query: customStringify(TERM_QUERY_TEXT),
473554
},
555+
{
556+
name: 'Match',
557+
query: customStringify(MATCH_QUERY_TEXT),
558+
},
559+
{
560+
name: 'Exists',
561+
query: customStringify(EXISTS_QUERY_TEXT),
562+
},
563+
{
564+
name: 'Fuzzy',
565+
query: customStringify(FUZZY_QUERY_TEXT),
566+
},
567+
{
568+
name: 'Wildcard',
569+
query: customStringify(WILDCARD_QUERY_TEXT),
570+
},
571+
{
572+
name: 'Prefix',
573+
query: customStringify(PREFIX_QUERY_TEXT),
574+
},
575+
{
576+
name: 'Match boolean',
577+
query: customStringify(MATCH_BOOLEAN_QUERY_TEXT),
578+
},
579+
{
580+
name: 'Match phrase',
581+
query: customStringify(MATCH_PHRASE_QUERY_TEXT),
582+
},
583+
{
584+
name: 'Match phrase prefix',
585+
query: customStringify(MATCH_PHRASE_PREFIX_QUERY_TEXT),
586+
},
587+
{
588+
name: 'Query string',
589+
query: customStringify(QUERY_STRING_QUERY_TEXT),
590+
},
474591
{
475592
name: 'Basic k-NN',
476593
query: customStringify(KNN_QUERY),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import '@testing-library/jest-dom';
7+
import { updatePathForExpandedQuery } from './config_to_template_utils';
8+
9+
describe('config_to_template_utils', () => {
10+
beforeEach(() => {});
11+
describe('updatePathForExpandedQuery', () => {
12+
test('term query', () => {
13+
expect(
14+
updatePathForExpandedQuery('query.term.a') === 'query.term.a.value'
15+
);
16+
expect(
17+
updatePathForExpandedQuery('query.term.a.value') ===
18+
'query.term.a.value'
19+
);
20+
expect(
21+
updatePathForExpandedQuery('$.query.term.a') === '$.query.term.a.value'
22+
);
23+
expect(
24+
updatePathForExpandedQuery('a.b.c.d.query.term.a') ===
25+
'a.b.c.d.query.term.a.value'
26+
);
27+
expect(
28+
updatePathForExpandedQuery('query.bool.must[0].term.a') ===
29+
'query.bool.must[0].term.a.value'
30+
);
31+
});
32+
test('prefix query', () => {
33+
expect(
34+
updatePathForExpandedQuery('query.prefix.a') === 'query.prefix.a.value'
35+
);
36+
expect(
37+
updatePathForExpandedQuery('query.prefix.a.value') ===
38+
'query.prefix.a.value'
39+
);
40+
});
41+
test('fuzzy query', () => {
42+
expect(
43+
updatePathForExpandedQuery('query.fuzzy.a') === 'query.fuzzy.a.value'
44+
);
45+
expect(
46+
updatePathForExpandedQuery('query.fuzzy.a.value') ===
47+
'query.fuzzy.a.value'
48+
);
49+
});
50+
test('wildcard query', () => {
51+
expect(
52+
updatePathForExpandedQuery('query.wildcard.a') ===
53+
'query.wildcard.a.wildcard'
54+
);
55+
expect(
56+
updatePathForExpandedQuery('query.wildcard.a.wildcard') ===
57+
'query.wildcard.a.wildcard'
58+
);
59+
});
60+
test('regexp query', () => {
61+
expect(
62+
updatePathForExpandedQuery('query.regexp.a') === 'query.regexp.a.value'
63+
);
64+
expect(
65+
updatePathForExpandedQuery('query.regexp.a.value') ===
66+
'query.regexp.a.value'
67+
);
68+
});
69+
test('match query', () => {
70+
expect(
71+
updatePathForExpandedQuery('query.match.a') === 'query.match.a.query'
72+
);
73+
expect(
74+
updatePathForExpandedQuery('query.match.a.query') ===
75+
'query.match.a.query'
76+
);
77+
});
78+
test('match bool prefix query', () => {
79+
expect(
80+
updatePathForExpandedQuery('query.match_bool_prefix.a') ===
81+
'query.match_bool_prefix.a.query'
82+
);
83+
expect(
84+
updatePathForExpandedQuery('query.match_bool_prefix.a.query') ===
85+
'query.match_bool_prefix.a.query'
86+
);
87+
});
88+
test('match phrase query', () => {
89+
expect(
90+
updatePathForExpandedQuery('query.match_phrase.a') ===
91+
'query.match_phrase.a.query'
92+
);
93+
expect(
94+
updatePathForExpandedQuery('query.match_phrase.a.query') ===
95+
'query.match_phrase.a.query'
96+
);
97+
});
98+
test('match phrase prefix query', () => {
99+
expect(
100+
updatePathForExpandedQuery('query.match_phrase_prefix.a') ===
101+
'query.match_phrase_prefix.a.query'
102+
);
103+
expect(
104+
updatePathForExpandedQuery('query.match_phrase_prefix.a.query') ===
105+
'query.match_phrase_prefix.a.query'
106+
);
107+
});
108+
test('aggs query', () => {
109+
expect(
110+
updatePathForExpandedQuery('aggs.avg_a.avg.field') ===
111+
'aggregations.avg_a.avg.field'
112+
);
113+
expect(
114+
updatePathForExpandedQuery('aggs.b.c.d.aggs.avg_a.avg.field') ===
115+
'aggregations.b.c.d.aggregations.avg_a.avg.field'
116+
);
117+
});
118+
});
119+
});

0 commit comments

Comments
 (0)