🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

github.com/Trendyol/es-query-builder

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/Trendyol/es-query-builder

v0.6.2
Source
Go
Version published
Created
Source

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

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(
            es.Agg("genres_count", es.TermsAgg("genre")), 
            es.Agg("authors_and_genres", es.TermsAgg("author").
                Aggs(es.Agg("genres", es.TermsAgg("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
  • OS: macOS Sonoma 14.7.3
  • CPU: Apple Silicon M1 Pro 10 Core
  • Arch: ARM64
  • Memory: 32GB LPDDR5
  • Go Version: go1.23.5
  • es-query-builder Version: v0.4.2
  • Benchmark Date: 02/01/2025

arm64 combined

ARM64 Detailed Benchmark Results

arm64 simple

Benchmark test file at simple query benchmark

arm64 intermediate

Benchmark test file at intermediate query benchmark

arm64 complex

Benchmark test file at complex query benchmark

arm64 mixed

Benchmark test file at mixed query benchmark

arm64 conditional

Benchmark test file at conditional query benchmark

arm64 multi filter

Benchmark test file at multi filter query benchmark

arm64 aggs

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/opdefensestation/osquery scoredefensestation/osquery ns/opes-query-builder scorees-query-builder ns/op
simple16289468368194933530791747318342511026227533
simple16274949368194828630771748682343111158036536
simple16157809370194409730821747022344610935156536
simple16272403370194513630821742745343911048505538
simple16122024370194861730751746759343311024264533
simple avg16223330,60369,201947094,203079,001746505,203434,8011038437,60535,20
simple median16272403,00370,001948286,003079,001747022,003433,0011026227,00536,00
simple stddev69286,110,982077,412,761993,477,1771305,271,94
complex23282432575471954127254269001416216095073705
complex23361792565470151126564281081410316277073705
complex23434122573477055126304268491403716221743698
complex23400902563474279126274278571403816193743695
complex23338272571474705126454255061406216253643702
complex avg2336350,202569,40473628,8012656,60427044,0014080,401620825,203701,00
complex median2336179,002571,00474279,0012645,00426900,0014062,001622174,003702,00
complex stddev5214,934,632375,7035,77918,4047,316324,353,95
conditional398353214918362047168793478752228947222087
conditional391819515348454277106805912748628849242121
conditional404411114878405297157794827754528980682081
conditional404871014888466107125804211746828812802082
conditional403915514848402567105802660747528918262072
conditional avg4006740,601496,80841805,207132,20800217,607499,202890164,002088,60
conditional median4039155,001488,00840529,007125,00802660,007486,002891826,002082,00
conditional stddev50174,9618,733784,3325,985075,8229,506203,1316,91
intermediate457331013128675626954779059767433955921774
intermediate456790413168610706973771276771434139001769
intermediate456367813198625936924782931765134082191777
intermediate455686313178671056904783907764733988141770
intermediate455942613208638776945780218777334163601768
intermediate avg4564236,201316,80864441,406940,00779478,207691,803406577,001771,60
intermediate median4563678,001317,00863877,006945,00780218,007674,003408219,001770,00
intermediate stddev5892,372,792527,2323,924461,7347,068160,443,38
mixed34523481739100000055761000000551929268902057
mixed34676901734100000051431000000552529300382052
mixed34895821726100000052241000000552029207242053
mixed34658781728100000051541000000554129355462053
mixed34746741734100000051411000000551329321012056
mixed avg3470034,401732,201000000,005247,601000000,005523,602929059,802054,20
mixed median3467690,001734,001000000,005154,001000000,005520,002930038,002053,00
mixed stddev12159,074,660,00167,010,009,505029,321,94
multi filter403553114818414717113797647748230642441964
multi filter408989214748365657091800364749230774221961
multi filter406220414778402907105801840749330641781957
multi filter403477014818380807136795669749130778751967
multi filter402619614748482997074806515748330783581962
multi filter avg4049718,601477,40840941,007103,80800407,007488,203072415,401962,20
multi filter median4035531,001477,00840290,007105,00800364,007491,003077422,001962,00
multi filter stdev23442,903,144054,1220,863726,374,716705,443,31
aggs2591437231461911797376041241014018594463232
aggs2625234229161130597115867381007418723663220
aggs2622649229261398197075960951068418713603216
aggs2626522228460507096925981781006618570143209
aggs2623935229561362296875939321006318588183224
aggs avg2617955,402295,20612619,009706,80595813,4010205,401863800,803220,20
aggs median2623935,002292,00613622,009707,00596095,0010074,001859446,003220,00
aggs stddev13321,9810,074556,2917,555668,14240,966638,647,70

Want to Contribute?

Join Us join us

Contribute to Our Project

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

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

  • 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.

FAQs

Package last updated on 05 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts