/
main.go
51 lines (41 loc) · 1.56 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"net/http"
"strings"
"github.com/grokify/mogo/fmt/fmtutil"
"github.com/grokify/mogo/log/logutil"
"github.com/grokify/mogo/net/http/httpsimple"
elastirad "github.com/grokify/elastirad-go"
"github.com/grokify/elastirad-go/docs/reference"
"github.com/grokify/elastirad-go/models/es5"
)
// Example from:
// https://www.elastic.co/guide/en/elasticsearch/reference/5.5/_executing_aggregations.html
// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation
// https://www.elastic.co/guide/en/elasticsearch/guide/current/aggregations-and-analysis.html#aggregations-and-analysis
// NESTED AGGREGATION
// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html
// main is a simple request that shows the ES documented
// request
// main shows nested aggregation in action
func main() {
esClient, err := elastirad.NewSimpleClient("", "", "", true)
logutil.FatalErr(err)
body := es5.AggsBody{
Size: 10000,
Aggs: map[string]es5.Agg{
"user": {
Nested: &es5.Nested{Path: "user"},
Aggs: map[string]es5.Agg{
"TweetCountByUsername": {
Terms: &es5.Term{Field: "user.username"}}}}}}
fmtutil.MustPrintJSON(body)
resp, err := esClient.Do(httpsimple.Request{
Method: http.MethodPost,
URL: strings.Join([]string{"twitter/tweet", elastirad.SearchSlug}, "/"),
Body: body,
BodyType: httpsimple.BodyTypeJSON})
reference.ProcResponse(resp, err)
fmt.Println("DONE")
}