Skip to content
This repository was archived by the owner on May 12, 2026. It is now read-only.

Commit 6ecf039

Browse files
committed
Quote single-element array values to handle spaces and colons
Values like ["foo bar"] or ["foo:bar"] produced broken queries (e.g. field:foo bar) because the single-element path emitted bare terms. Now all array element values are quoted as phrase queries, consistent with the multi-element IN path.
1 parent a123c0c commit 6ecf039

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

src/modifyQuery.test.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { addAddHocFilter } from './modifyQuery';
22

33
describe('addAddHocFilter', () => {
44
describe('array values', () => {
5-
it('unwraps single-element array into a term query', () => {
5+
it('unwraps single-element array into a phrase query', () => {
66
const result = addAddHocFilter('', {
77
key: 'attributes.tags',
88
operator: '=',
99
value: '["paperclip"]',
1010
});
11-
expect(result).toBe('attributes.tags:paperclip');
11+
expect(result).toBe('attributes.tags:"paperclip"');
1212
});
1313

1414
it('unwraps multi-element array into IN set query', () => {
@@ -20,13 +20,13 @@ describe('addAddHocFilter', () => {
2020
expect(result).toBe('attributes.tags:IN ["paperclip" "stapler"]');
2121
});
2222

23-
it('negated single-element array produces negated term query', () => {
23+
it('negated single-element array produces negated phrase query', () => {
2424
const result = addAddHocFilter('', {
2525
key: 'attributes.tags',
2626
operator: '!=',
2727
value: '["paperclip"]',
2828
});
29-
expect(result).toBe('-attributes.tags:paperclip');
29+
expect(result).toBe('-attributes.tags:"paperclip"');
3030
});
3131

3232
it('negated multi-element array produces negated IN set query', () => {
@@ -44,7 +44,43 @@ describe('addAddHocFilter', () => {
4444
operator: '=',
4545
value: '["paperclip"]',
4646
});
47-
expect(result).toBe('status:200 AND attributes.tags:paperclip');
47+
expect(result).toBe('status:200 AND attributes.tags:"paperclip"');
48+
});
49+
50+
it('handles single-element array with spaces in value', () => {
51+
const result = addAddHocFilter('', {
52+
key: 'attributes.tags',
53+
operator: '=',
54+
value: '["foo bar"]',
55+
});
56+
expect(result).toBe('attributes.tags:"foo bar"');
57+
});
58+
59+
it('handles single-element array with colons in value', () => {
60+
const result = addAddHocFilter('', {
61+
key: 'attributes.tags',
62+
operator: '=',
63+
value: '["foo:bar"]',
64+
});
65+
expect(result).toBe('attributes.tags:"foo:bar"');
66+
});
67+
68+
it('handles multi-element array with spaces in values', () => {
69+
const result = addAddHocFilter('', {
70+
key: 'attributes.tags',
71+
operator: '=',
72+
value: '["foo bar","baz qux"]',
73+
});
74+
expect(result).toBe('attributes.tags:IN ["foo bar" "baz qux"]');
75+
});
76+
77+
it('handles array values containing double quotes', () => {
78+
const result = addAddHocFilter('', {
79+
key: 'attributes.tags',
80+
operator: '=',
81+
value: '["say \\"hello\\""]',
82+
});
83+
expect(result).toBe('attributes.tags:"say \\"hello\\""');
4884
});
4985

5086
it('passes through non-array bracket strings unchanged', () => {

src/modifyQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function addAddHocFilter(query: string, filter: AdHocVariableFilter): str
4646
const modifier = filter.operator === '=' ? '' : '-';
4747
const key = escapeFilter(filter.key);
4848
if (arrayElements.length === 1) {
49-
return concatenate(query, `${modifier}${key}:${escapeFilterValue(arrayElements[0])}`, 'AND');
49+
return concatenate(query, `${modifier}${key}:"${escapeFilterValue(arrayElements[0])}"`, 'AND');
5050
}
5151
const terms = arrayElements.map((el) => `"${escapeFilterValue(el)}"`).join(' ');
5252
return concatenate(query, `${modifier}${key}:IN [${terms}]`, 'AND');

0 commit comments

Comments
 (0)