Categorygithub.com/Trendyol/es-query-builder
repository
0.3.7
Repository: https://github.com/trendyol/es-query-builder.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

es-query-builder GoDoc Release Build Status Go Report Card Coverage Status

A simple, user-friendly, and streamlined library for programmatically building Elasticsearch DSL queries in Go, designed for low overhead and minimal memory usage.

Install

With Go's module support, go [build|run|test] automatically fetches the necessary dependencies when you add the import in your code:

import "github.com/Trendyol/es-query-builder"

Alternatively, use go get:

go get -u github.com/Trendyol/es-query-builder

Example

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "author": "George Orwell"
          }
        }
      ],
      "must_not": [
        {
          "terms": {
            "genre": [
              "Fantasy",
              "Science Fiction"
            ]
          }
        },
        {
          "exists": {
            "field": "out_of_print"
          }
        }
      ],
      "should": [
        {
          "terms": {
            "title": [
              "1984",
              "Animal Farm"
            ]
          }
        }
      ]
    }
  },
  "aggs": {
    "genres_count": {
      "terms": {
        "field": "genre"
      }
    },
    "authors_and_genres": {
      "terms": {
        "field": "author"
      },
      "aggs": {
        "genres": {
          "terms": {
            "field": "genre"
          }
        }
      }
    }
  }
}

With es-query-builder

query := es.NewQuery(
    es.Bool().
        Must(
            es.Term("author", "George Orwell"),
        ).
        MustNot(
            es.Terms("genre", "Fantasy", "Science Fiction"),
            es.Exists("out_of_print"),
        ).
        Should(
            es.Terms("title", "1984", "Animal Farm"),
        ),
).Aggs("genres_count",
    es.AggTerms().
        Field("genre"),
).Aggs("authors_and_genres",
    es.AggTerms().
        Field("author").
        Aggs("genres",
            es.AggTerms().
                Field("genre"),
        ),
)

With vanilla Go

query := map[string]interface{}{
  "query": map[string]interface{}{
    "bool": map[string]interface{}{
      "must": []map[string]interface{}{
        {
          "term": map[string]interface{}{
            "author": "George Orwell",
          },
        },
      },
      "must_not": []map[string]interface{}{
        {
          "terms": map[string]interface{}{
            "genre": []string{
              "Fantasy",
              "Science Fiction",
            },
          },
        },
        {
          "exists": map[string]interface{}{
            "field": "out_of_print",
          },
        },
      },
      "should": []map[string]interface{}{
        {
          "terms": map[string]interface{}{
            "title": []string{
              "1984",
              "Animal Farm",
            },
          },
        },
      },
    },
  },
  "aggs": map[string]interface{}{
    "genres_count": map[string]interface{}{
      "terms": map[string]interface{}{
        "field": "genre",
      },
    },
    "authors_and_genres": map[string]interface{}{
      "terms": map[string]interface{}{
        "field": "author",
      },
      "aggs": map[string]interface{}{
        "genres": map[string]interface{}{
          "terms": map[string]interface{}{
            "field": "genre",
          },
        },
      },
    },
  },
}

Benchmarks

You can check and run benchmarks on your machine.

ARM64

  • Device: MacBook Pro 16" 2021
  • CPU: Apple Silicon M1 Pro 10 Core
  • Arch: ARM64
  • Memory: 32GB LPDDR5
  • Go Version: go1.22.1
  • es-query-builder Version: v0.2.1

arm64 combined

ARM64 Detailed Benchmark Results

arm64 simple

  • es-query-builder is 23% less efficient than vanilla Go.
  • aquasecurity/esquery is 84% less efficient than es-query-builder.

Benchmark test file at simple query benchmark


arm64 intermediate

  • es-query-builder is 24% less efficient than vanilla Go.
  • aquasecurity/esquery is 75% less efficient than es-query-builder.

Benchmark test file at intermediate query benchmark


arm64 complex

  • es-query-builder is 29% less efficient than vanilla Go.
  • aquasecurity/esquery is 71% less efficient than es-query-builder.

Benchmark test file at complex query benchmark


arm64 mixed

  • es-query-builder is 19% less efficient than vanilla Go.
  • aquasecurity/esquery is 64% less efficient than es-query-builder.

Benchmark test file at mixed query benchmark


arm64 conditional

  • es-query-builder is 32% less efficient than vanilla Go.
  • aquasecurity/esquery is 69% less efficient than es-query-builder.

Benchmark test file at conditional query benchmark


arm64 aggs

  • es-query-builder is 23% less efficient than vanilla Go.
  • aquasecurity/esquery is 69% less efficient than es-query-builder.

Benchmark test file at aggs query benchmark


MacBook M1 Pro 10 Core Benchmark Result Table

Benchmark Namevanilla go scorevanilla go ns/opaquasecurity/esquery scoreaquasecurity/esquery ns/opes-query-builder scorees-query-builder ns/op
simple16002007376,61935308309912279682486,8
simple15991658376,71935002310012303226486,5
simple16034337373,51935006308612072054485
simple15873903374,81942242309112350944484,7
simple15957768374,61941484309112305442484,4
simple15948505375,21939986309212294543486,5
simple16036609374,11941672309412399751483,3
simple15903747374,51944676309412381858484,5
simple15937150376,61942200308312333574484,4
simple158876623761941118308912324646486,2
simple avg15957334,60375,261939869,403091,9012304572,00485,23
simple median15953136,50375,001941301,003091,5012315044,00484,85
simple stddev55280,941,093312,764,9985565,121,12
complex229571526124689931279116279983697
complex229355026144683371275816232533699
complex230862925994688531269316277893687
complex230348426044717341270116387583672
complex230146626334477081295716286773706
complex231436826064662091274916383723674
complex230902826044718971273216300873692
complex230406926114681981272416262143686
complex230961326014716861273316306823681
complex229703226234689301273116326463676
complex avg2303695,402610,70467254,5012756,901630447,603687,00
complex median2303776,502608,50468891,5012732,501629382,003686,50
complex stddev6464,2910,026744,3571,714719,8110,87
conditional40138141506833030715626963042223
conditional39606371512833611717027055232226
conditional39377591516832034716626979062220
conditional39775651511838292714527075632220
conditional39869961504824229714027134012219
conditional39615731503828835713227008662216
conditional40018751499823173713527106872218
conditional39746841514829016713927057212221
conditional39956921503828115713527161762217
conditional39963821505828490717427130702220
conditional avg3980697,701507,30829882,507149,202706721,702220,00
conditional median3982280,501505,50828925,507142,502706642,002220,00
conditional stddev21710,735,294278,4115,106431,962,76
intermediate44113441354829754698433439681780
intermediate44060731357856807698334203161770
intermediate44807721345850144695934017301761
intermediate44471611347850741694934172131762
intermediate44645651344847190693934087841765
intermediate45151951329848419696734098921763
intermediate45248441331855118695334184831758
intermediate44549051350842991696833980351765
intermediate44471191347847276695134180301765
intermediate45298501328843906694834208281760
intermediate avg4468182,801343,20847234,606960,103405727,901764,90
intermediate median4459735,001346,00847847,506956,003413552,501764,00
intermediate stddev41887,039,847145,6614,3321915,515,94
mixed339719517621000000520827625122201
mixed339892017541000000521627555692201
mixed343774317521000000521827479652177
mixed343548617521000000521227626942172
mixed342338617451000000518927670532166
mixed341561217511000000520727521922179
mixed341309217571000000521227589052185
mixed344156617641000000520827726342172
mixed341283917541000000520527578062176
mixed341850317531000000520227688882167
mixed avg3419434,201754,401000000,005207,702760621,802179,60
mixed median3417057,501753,501000000,005208,002760708,502176,50
mixed stddev14535,885,200,007,767276,7711,93
aggs24696402338606574984518646243215
aggs25867962322606202983919784293038
aggs25873702325606057982219849663037
aggs25932202321608720978519815573015
aggs26008062304611236976219913463003
aggs25945932311606961977719932763027
aggs25874192318596414980119743603019
aggs25907852319609549978419882693021
aggs25965022317603372977419832803024
aggs25797622323603915979020124872996
aggs avg2578689,302319,80605900,009797,901975259,403039,50
aggs median2589102,002320,00606388,009787,501984123,003022,50
aggs stddev36779,078,453895,5326,8638193,9559,83

Want to Contribute?

Join Us join us

Contribute to Our Project

Want to help out? Awesome! Here’s how you can contribute:

  1. Report Issues: Got a suggestion, recommendation, or found a bug? Head over to the Issues section and let us know.

  2. Make Changes: Want to improve the code?

    • Fork the repo
    • Create a new branch
    • Make your changes
    • Open a Pull Request (PR)

We’re excited to see your contributions. Thanks for helping make this project better!

License

MIT - Please check the LICENSE file for full text.