An idiomatic Go query builder for ElasticSearch https://github.com/aquasecurity/esquery.git
Go to file
Maxim Yanchuk 04bc4c24dc highlight fix 2023-11-02 16:12:39 +03:00
.gitignore Features: SearchAfter、Term Aggregation Order、Aggregation Include Filter Values (#14) 2021-03-15 09:43:59 +02:00
.golangci.yml Add documentation, fix small bugs, improve CustomQuery (#6) 2020-02-27 16:19:07 +02:00
.travis.yml Rename travis.yml to .travis.yml 2020-02-23 13:01:54 +02:00
LICENSE Create LICENSE 2020-02-20 11:46:34 +02:00
README.md Merge remote-tracking branch 'origin/master' into es8 2023-11-02 00:14:33 +03:00
aggregations_test.go feat: add date_histogram agg. 2022-01-21 14:09:06 +08:00
aggs_bucket.go feat: add date_histogram agg. 2022-01-21 14:09:06 +08:00
aggs_filter.go FIX import & go.sum & simple issues 2023-11-02 00:30:33 +03:00
aggs_filter_test.go Features: Filter Aggregations, Nested Aggregations, Multi Match Queries, Highlights (#12) 2021-03-14 09:50:22 +02:00
aggs_metric.go FIX import & go.sum & simple issues 2023-11-02 00:30:33 +03:00
aggs_metric_test.go Refactor API, add aggregations and custom queries 2020-02-20 11:50:11 +02:00
aggs_nested.go FIX import & go.sum & simple issues 2023-11-02 00:30:33 +03:00
aggs_nested_test.go Features: Filter Aggregations, Nested Aggregations, Multi Match Queries, Highlights (#12) 2021-03-14 09:50:22 +02:00
common.go Support _source, sort and post_filter in search requests (#10) 2020-05-21 15:44:53 +03:00
count.go Support Elasticsearch v8 2022-09-26 16:38:45 +03:00
count_test.go Add support for Count requests (#8) 2020-04-06 12:24:44 +03:00
custom.go FIX import & go.sum & simple issues 2023-11-02 00:30:33 +03:00
custom_test.go Add documentation, fix small bugs, improve CustomQuery (#6) 2020-02-27 16:19:07 +02:00
delete.go Support Elasticsearch v8 2022-09-26 16:38:45 +03:00
es.go Support Elasticsearch v8 2022-09-26 16:38:45 +03:00
es_test.go Bugfix: Run() fails for queries, add MarshalJSON() 2020-02-20 16:46:27 +02:00
go.mod module change 2023-11-02 11:32:36 +03:00
go.sum FIX import & go.sum & simple issues 2023-11-02 00:30:33 +03:00
highlight.go highlight fix 2023-11-02 16:12:39 +03:00
highlight_test.go Features: Filter Aggregations, Nested Aggregations, Multi Match Queries, Highlights (#12) 2021-03-14 09:50:22 +02:00
queries_test.go Add documentation, fix small bugs, improve CustomQuery (#6) 2020-02-27 16:19:07 +02:00
query_boolean.go FIX import & go.sum & simple issues 2023-11-02 00:30:33 +03:00
query_boolean_test.go Refactor API, add aggregations and custom queries 2020-02-20 11:50:11 +02:00
query_boosting.go Add documentation, fix small bugs, improve CustomQuery (#6) 2020-02-27 16:19:07 +02:00
query_boosting_test.go Refactor API, add aggregations and custom queries 2020-02-20 11:50:11 +02:00
query_combined_fields.go FIX import & go.sum & simple issues 2023-11-02 00:30:33 +03:00
query_combined_fields_test.go Add support for combined_fields query 2021-12-21 22:00:39 +09:00
query_constant_score.go Add documentation, fix small bugs, improve CustomQuery (#6) 2020-02-27 16:19:07 +02:00
query_constant_score_test.go Refactor API, add aggregations and custom queries 2020-02-20 11:50:11 +02:00
query_dis_max.go Add documentation, fix small bugs, improve CustomQuery (#6) 2020-02-27 16:19:07 +02:00
query_dis_max_test.go Refactor API, add aggregations and custom queries 2020-02-20 11:50:11 +02:00
query_joining.go feat: Support Nested for joining queries. 2021-05-27 22:06:02 +08:00
query_joining_test.go feat: Support Nested for joining queries. 2021-05-27 22:06:02 +08:00
query_match.go FIX import & go.sum & simple issues 2023-11-02 00:30:33 +03:00
query_match_all.go Add documentation, fix small bugs, improve CustomQuery (#6) 2020-02-27 16:19:07 +02:00
query_match_all_test.go Refactor API, add aggregations and custom queries 2020-02-20 11:50:11 +02:00
query_match_test.go boost support added into match statement 2022-01-06 22:54:24 +02:00
query_multi_match.go FIX import & go.sum & simple issues 2023-11-02 00:30:33 +03:00
query_multi_match_test.go Features: Filter Aggregations, Nested Aggregations, Multi Match Queries, Highlights (#12) 2021-03-14 09:50:22 +02:00
query_term_level.go FIX import & go.sum & simple issues 2023-11-02 00:30:33 +03:00
query_term_level_test.go Add documentation, fix small bugs, improve CustomQuery (#6) 2020-02-27 16:19:07 +02:00
search.go FIX import & go.sum & simple issues 2023-11-02 00:30:33 +03:00
search_test.go Features: SearchAfter、Term Aggregation Order、Aggregation Include Filter Values (#14) 2021-03-15 09:43:59 +02:00

README.md

esquery

Build Status

A non-obtrusive, idiomatic and easy-to-use query and aggregation builder for the official Go client for ElasticSearch.

Table of Contents

Description

esquery alleviates the need to use extremely nested maps (map[string]interface{}) and serializing queries to JSON manually. It also helps eliminating common mistakes such as misspelling query types, as everything is statically typed.

Using esquery can make your code much easier to write, read and maintain, and significantly reduce the amount of code you write. Wanna know how much code you'll save? just check this project's tests.

Status

This is an early release, API may still change.

Installation

esquery is a Go module. To install, simply run this in your project's root directory:

go get github.com/aquasecurity/esquery/v8

Usage

esquery provides a method chaining-style API for building and executing queries and aggregations. It does not wrap the official Go client nor does it require you to change your existing code in order to integrate the library. Queries can be directly built with esquery, and executed by passing an *elasticsearch.Client instance (with optional search parameters). Results are returned as-is from the official client (e.g. *esapi.Response objects).

Getting started is extremely simple:

package main

import (
	"context"
	"log"

	"github.com/aquasecurity/esquery/v8"
	"github.com/elastic/go-elasticsearch/v8"
)

func main() {
    // connect to an ElasticSearch instance
    es, err := elasticsearch.NewDefaultClient()
    if err != nil {
        log.Fatalf("Failed creating client: %s", err)
    }

    // run a boolean search query
    res, err := esquery.Search().
        Query(
            esquery.
                Bool().
                Must(esquery.Term("title", "Go and Stuff")).
                Filter(esquery.Term("tag", "tech")),
        ).
        Aggs(
            esquery.Avg("average_score", "score"),
            esquery.Max("max_score", "score"),
        ).
        Size(20).
        Run(
            es,
            es.Search.WithContext(context.TODO()),
            es.Search.WithIndex("test"),
        )
        if err != nil {
            log.Fatalf("Failed searching for stuff: %s", err)
        }

    defer res.Body.Close()

    // ...
}

Notes

  • esquery currently supports version 7 of the ElasticSearch Go client.
  • The library cannot currently generate "short queries". For example, whereas ElasticSearch can accept this:
{ "query": { "term": { "user": "Kimchy" } } }

The library will always generate this:

{ "query": { "term": { "user": { "value": "Kimchy" } } } }

This is also true for queries such as "bool", where fields like "must" can either receive one query object, or an array of query objects. esquery will generate an array even if there's only one query object.

Features

Supported Queries

The following queries are currently supported:

ElasticSearch DSL esquery Function
"match" Match()
"match_bool_prefix" MatchBoolPrefix()
"match_phrase" MatchPhrase()
"match_phrase_prefix" MatchPhrasePrefix()
"match_all" MatchAll()
"match_none" MatchNone()
"multi_match" MultiMatch()
"exists" Exists()
"fuzzy" Fuzzy()
"ids" IDs()
"prefix" Prefix()
"range" Range()
"regexp" Regexp()
"term" Term()
"terms" Terms()
"terms_set" TermsSet()
"wildcard" Wildcard()
"bool" Bool()
"boosting" Boosting()
"constant_score" ConstantScore()
"dis_max" DisMax()

Supported Aggregations

The following aggregations are currently supported:

ElasticSearch DSL esquery Function
"avg" Avg()
"weighted_avg" WeightedAvg()
"cardinality" Cardinality()
"max" Max()
"min" Min()
"sum" Sum()
"value_count" ValueCount()
"percentiles" Percentiles()
"stats" Stats()
"string_stats" StringStats()
"top_hits" TopHits()
"terms" TermsAgg()
"date_histogram" DateHistogramAgg()

Supported Top Level Options

The following top level options are currently supported:

ElasticSearch DSL esquery.Search Function
"highlight" Highlight()
"explain" Explain()
"from" From()
"postFilter" PostFilter()
"query" Query()
"aggs" Aggs()
"size" Size()
"sort" Sort()
"source" SourceIncludes(), SourceExcludes()
"timeout" Timeout()

Custom Queries and Aggregations

To execute an arbitrary query or aggregation (including those not yet supported by the library), use the CustomQuery() or CustomAgg() functions, respectively. Both accept any map[string]interface{} value.

License

This library is distributed under the terms of the Apache License 2.0.