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

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 @@ -1616,4 +1616,47 @@ describe('SearchSource', () => {
);
});
});

describe('parseActiveIndexPatternFromQueryString', () => {
it.each([
{
indexPattern: '_index: logs-2024-06-27',
expectedResult: ['logs-2024-06-27'],
description: 'a single index name without wildcards',
},
{
indexPattern: '_index: logs-*',
expectedResult: ['logs-*'],
description: 'a single index name with wildcards',
},
{
indexPattern: '_index: logs-2024-06-27 or _index: foo-2024-06-27',
expectedResult: ['logs-2024-06-27', 'foo-2024-06-27'],
description: 'multiple index names',
},
{
indexPattern: "_index: 'logs-2024-06-27'",
expectedResult: ['logs-2024-06-27'],
description: 'index names with single quotes',
},
{
indexPattern: '_index: "logs-2024-06-27"',
expectedResult: ['logs-2024-06-27'],
description: 'index names with double quotes',
},
{
indexPattern: '_index: "logs-2024-06-27\'',
expectedResult: [],
description: 'index pattern with mixed quotes',
},
{
indexPattern: 'foo: bar',
expectedResult: [],
description: 'no index pattern when _index is not present',
},
])('should extract $description', ({ indexPattern: actualIndexPattern, expectedResult }) => {
const result = searchSource.parseActiveIndexPatternFromQueryString(actualIndexPattern);
expect(result).toEqual(expectedResult);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -1223,23 +1223,16 @@ export class SearchSource {
}

parseActiveIndexPatternFromQueryString(queryString: string): string[] {
let m;
const indexPatternSet: Set<string> = new Set();
const regex = /\s?(_index)\s?:\s?[\'\"]?(\w+\-?\*?)[\'\"]?\s?(\w+)?/g;
// Regex to capture full index names including dashes, numbers, and periods
const indexNameRegExp = /\s?(_index)\s*:\s*(['"]?)([^\s'"]+)\2/g;

while ((m = regex.exec(queryString)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
for (const match of queryString.matchAll(indexNameRegExp)) {
const indexName = match[3];
if (indexName) {
indexPatternSet.add(indexName);
}

m.forEach((match, groupIndex) => {
if (groupIndex === 2) {
indexPatternSet.add(match);
}
});
}

return [...indexPatternSet];
}
}