Skip to content

Commit

Permalink
chore: update readme example. add example into
Browse files Browse the repository at this point in the history
  • Loading branch information
GokselKUCUKSAHIN committed Jun 9, 2024
1 parent 3eb019f commit 5b5808b
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 44 deletions.
145 changes: 101 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,56 @@ go get github.com/GokselKUCUKSAHIN/es-query-builder
"bool": {
"must": [
{
"bool": {
"should": [
{
"term": {
"doc.id": "293"
}
},
{
"term": {
"file.fileId": "293"
}
}
"term": {
"author.keyword": "George Orwell"
}
}
],
"must_not": [
{
"terms": {
"genre.keyword": [
"Fantasy",
"Science Fiction"
]
}
},
{
"exists": {
"field": "out_of_print"
}
}
],
"filter": [
"should": [
{
"terms": {
"type": [
"DOC",
"FILE"
"title.keyword": [
"1984",
"Animal Farm"
]
}
}
]
}
},
"aggs": {
"genres_count": {
"terms": {
"field": "genre.keyword"
}
},
"authors_and_genres": {
"terms": {
"field": "author.keyword"
},
"aggs": {
"genres": {
"terms": {
"field": "genre.keyword"
}
}
}
}
}
}
```
Expand All @@ -56,50 +79,84 @@ With vanilla Go
query := map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": []interface{}{
map[string]interface{}{
"bool": map[string]interface{}{
"should": []interface{}{
map[string]interface{}{
"term": map[string]interface{}{
"doc.id": id,
},
},
map[string]interface{}{
"term": map[string]interface{}{
"file.fileId": id,
},
},
"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",
},
},
},
"filter": []interface{}{
map[string]interface{}{
"should": []map[string]interface{}{
{
"terms": map[string]interface{}{
"type": []string{
"DOC", "FILE",
"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",
},
},
},
},
},
}
```

```go
query := es.NewQuery(
es.Bool().
Must(
es.Bool().
Should(
es.Term("doc.id", id),
es.Term("file.fileId", id),
),
).
Filter(
es.Terms("type", "DOC", "FILE"),
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"),
),
)
```
Expand All @@ -111,7 +168,7 @@ You can check and run [benchmarks](./benchmarks) on your machine.
### ARMv6l

- **Device**: Raspberry Pi Zero W
- **CPU**: Broadcom BCM2835 1GHz 1 Core
- **CPU**: Broadcom BCM2835 1GHz Single Core
- **Arch**: ARM v6 32 bit
- **Memory**: 512MB LPDDR2
- **Go Version**: go1.22.3
Expand Down
129 changes: 129 additions & 0 deletions benchmarks/readme_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package benchmarks

import (
"encoding/json"
"testing"

"github.com/GokselKUCUKSAHIN/es-query-builder/es"
"github.com/GokselKUCUKSAHIN/es-query-builder/test/assert"
)

//// Readme Example ////

func createReadmeQuery() string {
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"),
),
)
marshal, err := json.Marshal(query)
if err != nil {
return ""
}
return string(marshal)
}

func createReadmeQueryVanillaGo() string {
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",
},
},
},
},
},
}
marshal, err := json.Marshal(query)
if err != nil {
return ""
}
return string(marshal)
}

func Test_Readme_Queries_are_equal(t *testing.T) {
build := createReadmeQuery()
vanilla := createReadmeQueryVanillaGo()
assert.Equal(t, vanilla, build)
}

func Benchmark_Readme_Example_Builder(b *testing.B) {
createReadmeQuery()
b.ResetTimer()
for i := 0; i < b.N; i++ {
createReadmeQuery()
}
}

func Benchmark_Readme_Example_VanillaGo(b *testing.B) {
createReadmeQueryVanillaGo()
b.ResetTimer()
for i := 0; i < b.N; i++ {
createReadmeQueryVanillaGo()
}
}

0 comments on commit 5b5808b

Please sign in to comment.