mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Elasticsearch: Added modifyQuery
method to add filters in Explore (#52313)
This commit is contained in:
parent
a2512dd1c7
commit
0531e4efc0
@ -1021,6 +1021,54 @@ describe('enhanceDataFrame', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('modifyQuery', () => {
|
||||
let ds: ElasticDatasource;
|
||||
beforeEach(() => {
|
||||
ds = getTestContext().ds;
|
||||
});
|
||||
describe('with empty query', () => {
|
||||
let query: ElasticsearchQuery;
|
||||
beforeEach(() => {
|
||||
query = { query: '', refId: 'A' };
|
||||
});
|
||||
|
||||
it('should add the filter', () => {
|
||||
expect(ds.modifyQuery(query, { type: 'ADD_FILTER', key: 'foo', value: 'bar' }).query).toBe('foo:"bar"');
|
||||
});
|
||||
|
||||
it('should add the negative filter', () => {
|
||||
expect(ds.modifyQuery(query, { type: 'ADD_FILTER_OUT', key: 'foo', value: 'bar' }).query).toBe('-foo:"bar"');
|
||||
});
|
||||
|
||||
it('should do nothing on unknown type', () => {
|
||||
expect(ds.modifyQuery(query, { type: 'unknown', key: 'foo', value: 'bar' }).query).toBe(query.query);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with non-empty query', () => {
|
||||
let query: ElasticsearchQuery;
|
||||
beforeEach(() => {
|
||||
query = { query: 'test:"value"', refId: 'A' };
|
||||
});
|
||||
|
||||
it('should add the filter', () => {
|
||||
expect(ds.modifyQuery(query, { type: 'ADD_FILTER', key: 'foo', value: 'bar' }).query).toBe(
|
||||
'test:"value" AND foo:"bar"'
|
||||
);
|
||||
});
|
||||
|
||||
it('should add the negative filter', () => {
|
||||
expect(ds.modifyQuery(query, { type: 'ADD_FILTER_OUT', key: 'foo', value: 'bar' }).query).toBe(
|
||||
'test:"value" AND -foo:"bar"'
|
||||
);
|
||||
});
|
||||
|
||||
it('should do nothing on unknown type', () => {
|
||||
expect(ds.modifyQuery(query, { type: 'unknown', key: 'foo', value: 'bar' }).query).toBe(query.query);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const createElasticQuery = (): DataQueryRequest<ElasticsearchQuery> => {
|
||||
return {
|
||||
requestId: '',
|
||||
|
@ -952,6 +952,27 @@ export class ElasticDatasource
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
modifyQuery(query: ElasticsearchQuery, action: { type: string; key: string; value: string }): ElasticsearchQuery {
|
||||
let expression = query.query ?? '';
|
||||
switch (action.type) {
|
||||
case 'ADD_FILTER': {
|
||||
if (expression.length > 0) {
|
||||
expression += ' AND ';
|
||||
}
|
||||
expression += `${action.key}:"${action.value}"`;
|
||||
break;
|
||||
}
|
||||
case 'ADD_FILTER_OUT': {
|
||||
if (expression.length > 0) {
|
||||
expression += ' AND ';
|
||||
}
|
||||
expression += `-${action.key}:"${action.value}"`;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return { ...query, query: expression };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user