es: github.com/tj/es Index | Examples | Files

package es

import "github.com/tj/es"

Package es provides an Elasticsearch query DSL.

Code:

query := Pretty(Query(
    Aggs(
        Agg("results",
            Filter(
                Term("user.login", "tj"),
                Range("now-7d", "now"),
            )(
                Aggs(
                    Agg("repos",
                        Terms("repository.name.keyword", 100),
                        Aggs(
                            Agg("labels",
                                Terms("issue.labels.keyword", 100),
                                Aggs(
                                    Agg("duration_sum", Sum("duration"))))))))))))

fmt.Println(query)

Output:

{
  "aggs": {
    "results": {
      "aggs": {
        "repos": {
          "aggs": {
            "labels": {
              "aggs": {
                "duration_sum": {
                  "sum": {
                    "field": "duration"
                  }
                }
              },
              "terms": {
                "field": "issue.labels.keyword",
                "size": 100
              }
            }
          },
          "terms": {
            "field": "repository.name.keyword",
            "size": 100
          }
        }
      },
      "filter": {
        "bool": {
          "filter": [
            {
              "term": {
                "user.login": "tj"
              }
            },
            {
              "range": {
                "timestamp": {
                  "gte": "now-7d",
                  "lte": "now"
                }
              }
            }
          ]
        }
      }
    }
  },
  "size": 0
}

Code:

labels := Aggs(
    Agg("labels",
        Terms("issue.labels.keyword", 100),
        Aggs(
            Agg("duration_sum",
                Sum("duration")))))

repos := Aggs(
    Agg("repos",
        Terms("repository.name.keyword", 100),
        labels))

filter := Filter(
    Term("user.login", "tj"),
    Range("now-7d", "now"))

results := Aggs(
    Agg("results",
        filter(repos)))

query := Pretty(Query(results))

fmt.Println(query)

Output:

{
  "aggs": {
    "results": {
      "aggs": {
        "repos": {
          "aggs": {
            "labels": {
              "aggs": {
                "duration_sum": {
                  "sum": {
                    "field": "duration"
                  }
                }
              },
              "terms": {
                "field": "issue.labels.keyword",
                "size": 100
              }
            }
          },
          "terms": {
            "field": "repository.name.keyword",
            "size": 100
          }
        }
      },
      "filter": {
        "bool": {
          "filter": [
            {
              "term": {
                "user.login": "tj"
              }
            },
            {
              "range": {
                "timestamp": {
                  "gte": "now-7d",
                  "lte": "now"
                }
              }
            }
          ]
        }
      }
    }
  },
  "size": 0
}

Index

Examples

Package Files

es.go

func Agg Uses

func Agg(name string, children ...string) string

Agg with the given name.

func Aggs Uses

func Aggs(children ...string) string

Aggs with one or more agg.

func Avg Uses

func Avg(field string) string

Avg agg of the given field.

func DateHistogram Uses

func DateHistogram(field string, options ...string) string

DateHistogram agg of the given field.

func ExtendedBounds Uses

func ExtendedBounds(min, max int) string

ExtendedBounds of `min` / `max`.

func Filter Uses

func Filter(filters ...string) func(children ...string) string

Filter applies the given filters.

func Histogram Uses

func Histogram(field string, options ...string) string

Histogram agg of the given field.

func Interval Uses

func Interval(v interface{}) string

Interval int or string.

func Max Uses

func Max(field string) string

Max agg of the given field.

func Min Uses

func Min(field string) string

Min agg of the given field.

func MinDocCount Uses

func MinDocCount(n int) string

MinDocCount of `n`.

func Missing Uses

func Missing(n int) string

Missing value of `n`.

func Order Uses

func Order(field string, dir Direction) string

Order `field` by `direction`.

func Percentiles Uses

func Percentiles(field string, percents ...float64) string

Percentiles agg of the given field, optionally specifying which `percents` to include.

func Pretty Uses

func Pretty(s string) string

Pretty JSON.

func Query Uses

func Query(children ...string) string

Query is the root of a query.

Code:

s := Pretty(Query(
    Aggs(
        Agg("foo", Sum("count")),
        Agg("bar", Sum("count")),
        Agg("baz", Sum("count")),
    ),
))

fmt.Println(s)

Output:

	{
  "aggs": {
    "bar": {
      "sum": {
        "field": "count"
      }
    },
    "baz": {
      "sum": {
        "field": "count"
      }
    },
    "foo": {
      "sum": {
        "field": "count"
      }
    }
  },
  "size": 0
}

func Range Uses

func Range(gte, lte string) string

Range for filtering.

func Stats Uses

func Stats(field string) string

Stats agg of the given field.

func Sum Uses

func Sum(field string) string

Sum agg of the given field.

func Term Uses

func Term(field, value string) string

Term returns a term reference for filtering.

func Terms Uses

func Terms(field string, size int) string

Terms agg of the given field.

func TimeZone Uses

func TimeZone(s ...string) string

TimeZone offset such as "-08:00" or America/Los_Angeles". If the location is invalid this function will panic.

func When Uses

func When(cond bool, children ...string) string

When returns `children` only when `cond` is met.

Code:

period := "month"

query := Pretty(Query(
    Aggs(
        Agg("results",
            Filter(
                Term("user.login", "tj"),
                When(period == "week", Range("now-7d", "now")),
                When(period == "month", Range("now-1M", "now")),
            )(
                Aggs(
                    Agg("repos",
                        Terms("repository.name.keyword", 100),
                        Aggs(
                            Agg("labels",
                                Terms("issue.labels.keyword", 100),
                                Aggs(
                                    Agg("duration_sum", Sum("duration"))))))))))))

fmt.Println(query)

Output:

	{
  "aggs": {
    "results": {
      "aggs": {
        "repos": {
          "aggs": {
            "labels": {
              "aggs": {
                "duration_sum": {
                  "sum": {
                    "field": "duration"
                  }
                }
              },
              "terms": {
                "field": "issue.labels.keyword",
                "size": 100
              }
            }
          },
          "terms": {
            "field": "repository.name.keyword",
            "size": 100
          }
        }
      },
      "filter": {
        "bool": {
          "filter": [
            {
              "term": {
                "user.login": "tj"
              }
            },
            {
              "range": {
                "timestamp": {
                  "gte": "now-1M",
                  "lte": "now"
                }
              }
            }
          ]
        }
      }
    }
  },
  "size": 0
}

type Direction Uses

type Direction string

Direction for sorting.

const (
    Ascending  Direction = "asc"
    Descending           = "desc"
)

Directions available.

Package es imports 4 packages (graph). Updated 2017-06-03. Refresh now. Tools for package owners. This is an inactive package (no imports and no commits in at least two years).