|
1 |
| -# esquery |
| 1 | +# opensearch query builder |
2 | 2 |
|
3 |
| -[](https://godoc.org/github.com/aquasecurity/esquery) [](LICENSE) [](https://travis-ci.org/aquasecurity/esquery) |
| 3 | +[](LICENSE) [](https://app.circleci.com/pipelines/github/deliveroo/os_query?branch=main) |
4 | 4 |
|
5 | 5 |
|
6 |
| -**A non-obtrusive, idiomatic and easy-to-use query and aggregation builder for the [official Go client](https://github.com/elastic/go-elasticsearch) for [ElasticSearch](https://www.elastic.co/products/elasticsearch).** |
| 6 | +**A non-obtrusive, idiomatic and easy-to-use query and aggregation builder for the [official Opensearch client](https://opensearch.org/docs/latest/clients/go/) for [Opensearch](https://opensearch.org/).** |
7 | 7 |
|
8 | 8 | ## Table of Contents
|
9 | 9 |
|
@@ -34,65 +34,59 @@ This is an early release, API may still change.
|
34 | 34 |
|
35 | 35 | `esquery` is a Go module. To install, simply run this in your project's root directory:
|
36 | 36 |
|
37 |
| -```bash |
38 |
| -go get github.com/aquasecurity/esquery |
39 |
| -``` |
40 |
| - |
41 | 37 | ## Usage
|
42 | 38 |
|
43 |
| -esquery provides a [method chaining](https://en.wikipedia.org/wiki/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). |
| 39 | +it provides a [method chaining](https://en.wikipedia.org/wiki/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, and executed by passing an `*opensearch.Client` instance (with optional search parameters). Results are returned as-is from the official client (e.g. `*opensearchapi.Response` objects). |
44 | 40 |
|
45 | 41 | Getting started is extremely simple:
|
46 | 42 |
|
47 | 43 | ```go
|
48 | 44 | package main
|
49 | 45 |
|
50 | 46 | import (
|
51 |
| - "context" |
52 | 47 | "log"
|
53 | 48 |
|
54 |
| - "github.com/aquasecurity/esquery" |
55 |
| - "github.com/elastic/go-elasticsearch/v7" |
| 49 | + "github.com/opensearch-project/opensearch-go/v2" |
| 50 | + |
| 51 | + oq "github.com/deliveroo/os_query" |
56 | 52 | )
|
57 | 53 |
|
58 | 54 | func main() {
|
59 |
| - // connect to an ElasticSearch instance |
60 |
| - es, err := elasticsearch.NewDefaultClient() |
61 |
| - if err != nil { |
62 |
| - log.Fatalf("Failed creating client: %s", err) |
63 |
| - } |
64 |
| - |
65 |
| - // run a boolean search query |
66 |
| - res, err := esquery.Search(). |
67 |
| - Query( |
68 |
| - esquery. |
69 |
| - Bool(). |
70 |
| - Must(esquery.Term("title", "Go and Stuff")). |
71 |
| - Filter(esquery.Term("tag", "tech")), |
72 |
| - ). |
73 |
| - Aggs( |
74 |
| - esquery.Avg("average_score", "score"), |
75 |
| - esquery.Max("max_score", "score"), |
76 |
| - ). |
77 |
| - Size(20). |
78 |
| - Run( |
79 |
| - es, |
80 |
| - es.Search.WithContext(context.TODO()), |
81 |
| - es.Search.WithIndex("test"), |
82 |
| - ) |
83 |
| - if err != nil { |
84 |
| - log.Fatalf("Failed searching for stuff: %s", err) |
85 |
| - } |
86 |
| - |
87 |
| - defer res.Body.Close() |
88 |
| - |
89 |
| - // ... |
| 55 | + // connect to an Opensearch instance |
| 56 | + client, err := opensearch.NewDefaultClient() |
| 57 | + if err != nil { |
| 58 | + log.Fatalf("Failed creating client: %s", err) |
| 59 | + } |
| 60 | + |
| 61 | + query := oq.Query( |
| 62 | + oq. |
| 63 | + Bool(). |
| 64 | + Must(oq.Term("title", "Go and Stuff")). |
| 65 | + Filter(oq.Term("tag", "tech")), |
| 66 | + ). |
| 67 | + Aggs( |
| 68 | + oq.Avg("average_score", "score"), |
| 69 | + oq.Max("max_score", "score"), |
| 70 | + ). |
| 71 | + Size(20) |
| 72 | + |
| 73 | + queryString, err := query.MarshalJSON() |
| 74 | + if err != nil { |
| 75 | + log.Fatal("Failed creating query") |
| 76 | + } |
| 77 | + // run a boolean search query |
| 78 | + search := client.Search |
| 79 | + res, err := search(search.WithQuery(string(queryString))) |
| 80 | + if err != nil { |
| 81 | + log.Fatal("Failed executing query") |
| 82 | + } |
| 83 | + |
| 84 | + defer res.Body.Close() |
90 | 85 | }
|
91 | 86 | ```
|
92 | 87 |
|
93 | 88 | ## Notes
|
94 | 89 |
|
95 |
| -* `esquery` currently supports version 7 of the ElasticSearch Go client. |
96 | 90 | * The library cannot currently generate "short queries". For example, whereas
|
97 | 91 | ElasticSearch can accept this:
|
98 | 92 |
|
|
0 commit comments