An idiomatic Go query builder for ElasticSearch https://github.com/aquasecurity/esquery.git
Go to file
Ido Perlmuter 55000abc77 Add Search() function, README and fix some lint errors
This commit changes the internal `search()` function into an exposed
`Search()` function that can be used to execute queries against an
instance of an ElasticSearch client. The per-query-type methods of
`Run()` are removed for now to prevent having to create them for every
type. `Search()` is agnostic.

A README.md file is added with some information, and a few lingering
lint errors are fixed.
2020-02-20 11:50:11 +02:00
.golangci.yml Add Search() function, README and fix some lint errors 2020-02-20 11:50:11 +02:00
LICENSE Create LICENSE 2020-02-20 11:46:34 +02:00
README.md Add Search() function, README and fix some lint errors 2020-02-20 11:50:11 +02:00
boolean.go Add support for compound queries 2020-02-20 11:50:11 +02:00
boolean_test.go Add support for compound queries 2020-02-20 11:50:11 +02:00
boosting.go Add support for compound queries 2020-02-20 11:50:11 +02:00
boosting_test.go Add support for compound queries 2020-02-20 11:50:11 +02:00
constant_score.go Add support for compound queries 2020-02-20 11:50:11 +02:00
constant_score_test.go Add support for compound queries 2020-02-20 11:50:11 +02:00
dis_max.go Add support for compound queries 2020-02-20 11:50:11 +02:00
dis_max_test.go Add support for compound queries 2020-02-20 11:50:11 +02:00
es.go Add Search() function, README and fix some lint errors 2020-02-20 11:50:11 +02:00
es_test.go Initial commit 2020-02-20 11:50:11 +02:00
go.mod Add Search() function, README and fix some lint errors 2020-02-20 11:50:11 +02:00
go.sum Add Search() function, README and fix some lint errors 2020-02-20 11:50:11 +02:00
match.go Add Search() function, README and fix some lint errors 2020-02-20 11:50:11 +02:00
match_all.go Add Search() function, README and fix some lint errors 2020-02-20 11:50:11 +02:00
match_all_test.go Initial commit 2020-02-20 11:50:11 +02:00
match_test.go Initial commit 2020-02-20 11:50:11 +02:00
term_level.go Initial commit 2020-02-20 11:50:11 +02:00
term_level_test.go Initial commit 2020-02-20 11:50:11 +02:00

README.md

esquery

esquery is an idiomatic, easy-to-use query builder for the official Go client for ElasticSearch. It alleviates the need to use extremely nested maps of empty interfaces and serializing queries to JSON manually. It also helps eliminating common mistakes such as misspelling query types, as everything is statically typed.

Usage

esquery can be used directly to build queries, with no need for external dependencies. It can execute the queries against an existing instance of *esapi.API, but the queries can also be manually converted to JSON if necessary.

package main

import (
	"context"
	"log"

	"bitbucket.org/scalock/esquery"
	"github.com/elastic/go-elasticsearch/v7"
)

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

	res, err := esquery.Search(
		es,
		esquery.
			Bool().
			Must(esquery.Term("title", "Go and Stuff")).
			Filter(esquery.Term("tag", "tech")),
		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

  • Library currently supports v7 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.

Supported queries

The following queries are currently supported:

Query esquery Function
"match" Match()
"match_bool_prefix" MatchBoolPrefix()
"match_phrase" MatchPhrase()
"match_phrase_prefix" MatchPhrasePrefix()
"match_all" MatchAll()
"match_none" MatchNone()
"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()