Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -736,10 +736,15 @@ function buildEsRule(fieldName, value, operator, config, valueSrc) {
const isUnaryOperator = op === 'is_null' || op === 'is_not_null';
const hasValue = Array.isArray(value) && value.length > 0;
if (isNestedExtensionField && entityType && (hasValue || isUnaryOperator)) {
// Query Builder represents some operators (e.g. between) as nested arrays: [[from, to]].
// For custom properties we want to pass the operator value through unchanged so the
// underlying range builder can see both bounds.
const extensionValue = hasValue ? value[0] : null;

return buildExtensionQuery(
extensionPropertyName,
entityType,
hasValue ? value[0] : null,
extensionValue,
op,
not
);
Expand Down Expand Up @@ -882,7 +887,14 @@ export function elasticSearchFormat(tree, config, syntax = ES_6_SYNTAX) {
return;
}

if (value && Array.isArray(value[0])) {
// Query Builder uses nested arrays for a few cases:
// - multiselect values: [[v1, v2, ...]]
// - range operators (between): [[from, to]]
// Only expand the nested array into multiple rules for multiselect operators.
const isMultiSelectOperator =
operator?.startsWith('multiselect_') || operator?.startsWith('select_');

if (isMultiSelectOperator && value && Array.isArray(value[0])) {
// Check if this is a multiselect equals operator that should use AND logic
const useAndLogic =
operator === 'multiselect_equals' ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2026 Collate.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { QbUtils } from '@react-awesome-query-builder/antd';
import { NUMBER_FIELD_OPERATORS } from '../constants/AdvancedSearch.constants';
import { SearchOutputType } from '../components/Explore/AdvanceSearchProvider/AdvanceSearchProvider.interface';
import { SearchIndex } from '../enums/search.enum';
import { elasticSearchFormat } from './QueryBuilderElasticsearchFormatUtils';
import { getTreeConfig } from './AdvancedSearchUtils';

describe('QueryBuilderElasticsearchFormatUtils', () => {
it('builds a bounded range query for number custom properties with between operator', () => {
const config = getTreeConfig({
searchOutputType: SearchOutputType.ElasticSearch,
searchIndex: SearchIndex.TABLE,
isExplorePage: true,
});

// Make the test deterministic: Advanced Search injects custom property subfields at runtime.
// Provide a minimal extension field so QbUtils.checkTree keeps the rule.
config.fields.extension = {
...(config.fields.extension ?? {}),
subfields: {
...(config.fields.extension?.subfields ?? {}),
table: {
type: '!group',
subfields: {
myNumberProperty: {
type: 'number',
operators: NUMBER_FIELD_OPERATORS,
},
},
},
},
};

const jsonTree = {
id: 'root',
type: 'group',
properties: { conjunction: 'AND', not: false },
children1: {
rules: {
type: 'rule',
id: 'rules',
properties: {
field: 'extension.table.myNumberProperty',
operator: 'between',
value: [[1, 5]],
valueSrc: ['value'],
Comment on lines +23 to +59
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The base config returned by getTreeConfig() initializes fields.extension.subfields as {} (custom properties are injected later at runtime). With no extension.table.myNumberProperty field present, QbUtils.checkTree() is likely to drop/neutralize this rule, making the test unreliable. Consider injecting minimal config.fields.extension.subfields for table -> myNumberProperty (type number, operators NUMBER_FIELD_OPERATORS) before calling checkTree.

Copilot uses AI. Check for mistakes.
},
},
},
};

const tree = QbUtils.checkTree(QbUtils.loadTree(jsonTree), config);
const query = elasticSearchFormat(tree, config);

// Should apply both gte and lte bounds (and not degrade to "exists"/unbounded query)
expect(query).toEqual(
expect.objectContaining({
bool: expect.objectContaining({
must: expect.arrayContaining([
expect.objectContaining({
bool: expect.objectContaining({
should: expect.arrayContaining([
expect.objectContaining({
nested: expect.objectContaining({
path: 'customPropertiesTyped',
query: expect.objectContaining({
bool: expect.objectContaining({
must: expect.arrayContaining([
{
term: {
'customPropertiesTyped.name': 'myNumberProperty',
},
},
expect.objectContaining({
range: expect.objectContaining({
'customPropertiesTyped.longValue': {
gte: 1,
lte: 5,
},
}),
}),
]),
}),
}),
}),
}),
]),
}),
}),
{
term: {
entityType: 'table',
},
},
]),
}),
})
);
});
});

Loading