Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
25e2f1c2
Commit
25e2f1c2
authored
Jan 21, 2020
by
Emil Hessman
Committed by
kay delaney
Jan 21, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Plugins: Apply adhoc filter in Elasticsearch logs query (#21346)
Fixes #21086
parent
07d96fe4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
8 deletions
+66
-8
public/app/plugins/datasource/elasticsearch/datasource.ts
+1
-1
public/app/plugins/datasource/elasticsearch/query_builder.ts
+3
-1
public/app/plugins/datasource/elasticsearch/specs/query_builder.test.ts
+62
-6
No files found.
public/app/plugins/datasource/elasticsearch/datasource.ts
View file @
25e2f1c2
...
@@ -343,7 +343,7 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
...
@@ -343,7 +343,7 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
target
.
metrics
=
[
queryDef
.
defaultMetricAgg
()];
target
.
metrics
=
[
queryDef
.
defaultMetricAgg
()];
// Setting this for metrics queries that are typed as logs
// Setting this for metrics queries that are typed as logs
target
.
isLogsQuery
=
true
;
target
.
isLogsQuery
=
true
;
queryObj
=
this
.
queryBuilder
.
getLogsQuery
(
target
,
queryString
);
queryObj
=
this
.
queryBuilder
.
getLogsQuery
(
target
,
adhocFilters
,
queryString
);
}
else
{
}
else
{
if
(
target
.
alias
)
{
if
(
target
.
alias
)
{
target
.
alias
=
this
.
templateSrv
.
replace
(
target
.
alias
,
options
.
scopedVars
,
'lucene'
);
target
.
alias
=
this
.
templateSrv
.
replace
(
target
.
alias
,
options
.
scopedVars
,
'lucene'
);
...
...
public/app/plugins/datasource/elasticsearch/query_builder.ts
View file @
25e2f1c2
...
@@ -379,7 +379,7 @@ export class ElasticQueryBuilder {
...
@@ -379,7 +379,7 @@ export class ElasticQueryBuilder {
return
query
;
return
query
;
}
}
getLogsQuery
(
target
:
any
,
querystring
:
string
)
{
getLogsQuery
(
target
:
any
,
adhocFilters
?:
any
,
querystring
?
:
string
)
{
let
query
:
any
=
{
let
query
:
any
=
{
size
:
0
,
size
:
0
,
query
:
{
query
:
{
...
@@ -389,6 +389,8 @@ export class ElasticQueryBuilder {
...
@@ -389,6 +389,8 @@ export class ElasticQueryBuilder {
},
},
};
};
this
.
addAdhocFilters
(
query
,
adhocFilters
);
if
(
target
.
query
)
{
if
(
target
.
query
)
{
query
.
query
.
bool
.
filter
.
push
({
query
.
query
.
bool
.
filter
.
push
({
query_string
:
{
query_string
:
{
...
...
public/app/plugins/datasource/elasticsearch/specs/query_builder.test.ts
View file @
25e2f1c2
...
@@ -476,7 +476,6 @@ describe('ElasticQueryBuilder', () => {
...
@@ -476,7 +476,6 @@ describe('ElasticQueryBuilder', () => {
it
(
'should set correct explicit sorting'
,
()
=>
{
it
(
'should set correct explicit sorting'
,
()
=>
{
const
order
=
testGetTermsQuery
({
order
:
'desc'
});
const
order
=
testGetTermsQuery
({
order
:
'desc'
});
console
.
log
({
order
});
checkSort
(
order
,
'desc'
);
checkSort
(
order
,
'desc'
);
expect
(
order
.
_count
).
toBeUndefined
();
expect
(
order
.
_count
).
toBeUndefined
();
});
});
...
@@ -496,11 +495,68 @@ describe('ElasticQueryBuilder', () => {
...
@@ -496,11 +495,68 @@ describe('ElasticQueryBuilder', () => {
});
});
});
});
it
(
'getTermsQuery should request documents and date histogram'
,
()
=>
{
describe
(
'getLogsQuery'
,
()
=>
{
const
query
=
builder
.
getLogsQuery
({},
''
);
it
(
'should return query with defaults'
,
()
=>
{
console
.
log
({
query
});
const
query
=
builder
.
getLogsQuery
({},
null
,
'*'
);
expect
(
query
).
toHaveProperty
(
'query.bool.filter'
);
expect
(
query
.
aggs
[
'2'
]).
toHaveProperty
(
'date_histogram'
);
expect
(
query
.
size
).
toEqual
(
500
);
const
expectedQuery
=
{
bool
:
{
filter
:
[{
range
:
{
'@timestamp'
:
{
gte
:
'$timeFrom'
,
lte
:
'$timeTo'
,
format
:
'epoch_millis'
}
}
}],
},
};
expect
(
query
.
query
).
toEqual
(
expectedQuery
);
expect
(
query
.
sort
).
toEqual
({
'@timestamp'
:
{
order
:
'desc'
,
unmapped_type
:
'boolean'
}
});
const
expectedAggs
=
{
2
:
{
aggs
:
{},
date_histogram
:
{
extended_bounds
:
{
max
:
'$timeTo'
,
min
:
'$timeFrom'
},
field
:
'@timestamp'
,
format
:
'epoch_millis'
,
interval
:
'$__interval'
,
min_doc_count
:
0
,
},
},
};
expect
(
query
.
aggs
).
toMatchObject
(
expectedAggs
);
});
it
(
'with querystring'
,
()
=>
{
const
query
=
builder
.
getLogsQuery
({
query
:
'foo'
},
null
,
'foo'
);
const
expectedQuery
=
{
bool
:
{
filter
:
[
{
range
:
{
'@timestamp'
:
{
gte
:
'$timeFrom'
,
lte
:
'$timeTo'
,
format
:
'epoch_millis'
}
}
},
{
query_string
:
{
analyze_wildcard
:
true
,
query
:
'foo'
}
},
],
},
};
expect
(
query
.
query
).
toEqual
(
expectedQuery
);
});
it
(
'with adhoc filters'
,
()
=>
{
const
adhocFilters
=
[
{
key
:
'key1'
,
operator
:
'='
,
value
:
'value1'
},
{
key
:
'key2'
,
operator
:
'!='
,
value
:
'value2'
},
{
key
:
'key3'
,
operator
:
'<'
,
value
:
'value3'
},
{
key
:
'key4'
,
operator
:
'>'
,
value
:
'value4'
},
{
key
:
'key5'
,
operator
:
'=~'
,
value
:
'value5'
},
{
key
:
'key6'
,
operator
:
'!~'
,
value
:
'value6'
},
];
const
query
=
builder
.
getLogsQuery
({},
adhocFilters
,
'*'
);
expect
(
query
.
query
.
bool
.
must
[
0
].
match_phrase
[
'key1'
].
query
).
toBe
(
'value1'
);
expect
(
query
.
query
.
bool
.
must_not
[
0
].
match_phrase
[
'key2'
].
query
).
toBe
(
'value2'
);
expect
(
query
.
query
.
bool
.
filter
[
1
].
range
[
'key3'
].
lt
).
toBe
(
'value3'
);
expect
(
query
.
query
.
bool
.
filter
[
2
].
range
[
'key4'
].
gt
).
toBe
(
'value4'
);
expect
(
query
.
query
.
bool
.
filter
[
3
].
regexp
[
'key5'
]).
toBe
(
'value5'
);
expect
(
query
.
query
.
bool
.
filter
[
4
].
bool
.
must_not
.
regexp
[
'key6'
]).
toBe
(
'value6'
);
});
});
});
});
});
});
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment