fix a terms bug and add test

This commit is contained in:
wph95 2018-03-27 02:12:43 +08:00
parent a21afbe9a6
commit 4042e4b225
No known key found for this signature in database
GPG Key ID: C8B3F2A207A6AFDC
3 changed files with 102 additions and 3 deletions

View File

@ -41,7 +41,7 @@ type DateHistogramAgg struct {
}
type FiltersAgg struct {
Filter map[string]interface{} `json:"filter"`
Filters map[string]interface{} `json:"filters"`
}
type TermsAggSetting struct {

View File

@ -193,15 +193,17 @@ func (q *Query) getHistogramAgg(model *simplejson.Json) *HistogramAgg {
func (q *Query) getFilters(model *simplejson.Json) *FiltersAgg {
agg := &FiltersAgg{}
agg.Filters = map[string]interface{}{}
settings := simplejson.NewFromAny(model.Get("settings").Interface())
for filter := range settings.Get("filters").MustArray() {
for _, filter := range settings.Get("filters").MustArray() {
filterJson := simplejson.NewFromAny(filter)
query := filterJson.Get("query").MustString("")
label := filterJson.Get("label").MustString("")
if label == "" {
label = query
}
agg.Filter[label] = newQueryStringFilter(true, query)
agg.Filters[label] = newQueryStringFilter(true, query)
}
return agg
}

View File

@ -325,6 +325,103 @@ func TestElasticSearchQueryBuilder(t *testing.T) {
"aggs": {"4":{"aggs":{"2":{"aggs":{"1":{"sum":{"field":"value"}}},"date_histogram":{"extended_bounds":{"max":"<TO_TIMESTAMP>","min":"<FROM_TIMESTAMP>"},"field":"timestamp","format":"epoch_millis","interval":"1m","min_doc_count":0}}},"terms":{"field":"name_raw","order":{"_term":"desc"},"size":10}}}
}`
testElasticSearchResponse(testElasticsearchModelRequestJSON, expectedElasticsearchQueryJSON)
})
Convey("Test Filters Aggregates", func() {
testElasticsearchModelRequestJSON := `
{
"bucketAggs": [
{
"id": "3",
"settings": {
"filters": [{
"label": "hello",
"query": "host:\"67.65.185.232\""
}]
},
"type": "filters"
},
{
"field": "time",
"id": "2",
"settings": {
"interval": "auto",
"min_doc_count": 0,
"trimEdges": 0
},
"type": "date_histogram"
}
],
"metrics": [
{
"pipelineAgg": "select metric",
"field": "bytesSent",
"id": "1",
"meta": {},
"settings": {},
"type": "count"
}
],
"query": "*",
"refId": "A",
"timeField": "time"
}`
expectedElasticsearchQueryJSON := `{
"size": 0,
"query": {
"bool": {
"filter": [
{
"range": {
"time": {
"gte": "<FROM_TIMESTAMP>",
"lte": "<TO_TIMESTAMP>",
"format": "epoch_millis"
}
}
},
{
"query_string": {
"analyze_wildcard": true,
"query": "*"
}
}
]
}
},
"aggs": {
"3": {
"filters": {
"filters": {
"hello": {
"query_string": {
"query": "host:\"67.65.185.232\"",
"analyze_wildcard": true
}
}
}
},
"aggs": {
"2": {
"date_histogram": {
"interval": "200ms",
"field": "time",
"min_doc_count": 0,
"extended_bounds": {
"min": "<FROM_TIMESTAMP>",
"max": "<TO_TIMESTAMP>"
},
"format": "epoch_millis"
},
"aggs": {}
}
}
}
}
}
`
testElasticSearchResponse(testElasticsearchModelRequestJSON, expectedElasticsearchQueryJSON)
})
})