Skip to content

Instantly share code, notes, and snippets.

@olivere
Last active September 17, 2018 15:16
Show Gist options
  • Save olivere/2e07531dac54c8ec4c4ea230a7b5eb91 to your computer and use it in GitHub Desktop.
Save olivere/2e07531dac54c8ec4c4ea230a7b5eb91 to your computer and use it in GitHub Desktop.
Issue 907 (2)
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
// Illustrates sorting aggs and hits.
//
// Example
//
// go run main.go -url=http://127.0.0.1:9200 -index=issue-907 -sniff=false -d
//
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"github.com/olivere/elastic"
)
const (
mapping = `
{
"settings":{
"number_of_shards":1,
"number_of_replicas":0
},
"mappings":{
"_doc":{
"properties":{
"country": {
"type":"keyword"
},
"value": {
"type":"integer"
},
"collected": {
"type":"date"
}
}
}
}
}`
)
type Doc struct {
Country string `json:"country"`
Value int `json:"value"`
Collected string `json:"collected"` // time.Time
}
func main() {
var (
url = flag.String("url", "http://localhost:9200", "Elasticsearch URL")
sniff = flag.Bool("sniff", true, "Enable or disable sniffing")
index = flag.String("index", "", "Index name")
debug = flag.Bool("d", false, "Enable debug output")
)
flag.Parse()
log.SetFlags(0)
if *url == "" {
*url = "http://127.0.0.1:9200"
}
if *index == "" {
log.Fatal("please specify an index name -index")
}
// Create an Elasticsearch client
options := []elastic.ClientOptionFunc{
elastic.SetURL(*url),
elastic.SetSniff(*sniff),
}
if *debug {
logger := log.New(os.Stdout, "", 0)
options = append(options, elastic.SetTraceLog(logger))
}
client, err := elastic.NewClient(options...)
if err != nil {
log.Fatal(err)
}
// Just a status message
fmt.Println("Connected")
// Check if index already exists. We'll drop it then.
// Next, we create a fresh index/mapping.
exists, err := client.IndexExists(*index).Do(context.Background())
if err != nil {
log.Fatal(err)
}
if exists {
_, err := client.DeleteIndex(*index).Do(context.Background())
if err != nil {
log.Fatal(err)
}
}
_, err = client.CreateIndex(*index).Body(mapping).Do(context.Background())
if err != nil {
log.Fatal(err)
}
// Add some docs via Bulk API
{
docs := []Doc{
{
Country: "France",
Value: 20,
Collected: "2015-03-12",
},
{
Country: "Canada",
Value: 21,
Collected: "2015-03-12",
},
{
Country: "Brazil",
Value: 33,
Collected: "2015-03-12",
},
{
Country: "France",
Value: 10,
Collected: "2015-02-01",
},
{
Country: "Canada",
Value: 11,
Collected: "2015-02-01",
},
{
Country: "Mexico",
Value: 9,
Collected: "2015-02-01",
},
}
var reqs []elastic.BulkableRequest
for i, doc := range docs {
req := elastic.NewBulkIndexRequest().
Id(fmt.Sprint(i + 1)).
Doc(doc)
reqs = append(reqs, req)
}
// Write the bulk requests and make sure everything's written
_, err := client.Bulk().
Index(*index).
Type("_doc").
Add(reqs...).
Refresh("wait_for").
Do(context.Background())
if err != nil {
log.Fatal(err)
}
}
// Now run a query
query := elastic.NewMatchAllQuery()
lastCollectedAgg := elastic.NewMaxAggregation().Field("collected")
countriesAgg := elastic.NewTermsAggregation().
Field("country").
Order("last_collected", false).
SubAggregation("last_collected", lastCollectedAgg)
res, err := client.Search(*index).
Type("_doc").
Query(query).
Aggregation("countries", countriesAgg).
Sort("country", false).
Pretty(true).
Do(context.Background())
if err != nil {
log.Fatal(err)
}
if n := res.TotalHits(); n > 0 {
log.Printf("Found a total of %d records\n", n)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment