Skip to content

Commit

Permalink
Merge pull request #9 from GokselKUCUKSAHIN/feature/sort-v2
Browse files Browse the repository at this point in the history
feat: update sort with builder pattern
  • Loading branch information
GokselKUCUKSAHIN committed May 26, 2024
2 parents 3fb6d20 + 500e6fa commit 802b31e
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 68 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ A Simple query builder for Elasticsearch
go get github.com/GokselKUCUKSAHIN/es-query-builder
```

# TODOs

## Project
- [ ] improve README
- [x] add examples
Expand Down
88 changes: 46 additions & 42 deletions benchmarks/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func createSimpleQuery() string {
return string(marshal)
}

func createSimpleQueryPureGo() string {
func createSimpleQueryVanillaGo() string {
query := map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
Expand All @@ -51,7 +51,7 @@ func createSimpleQueryPureGo() string {

func Test_Simple_Queries_are_equal(t *testing.T) {
build := createSimpleQuery()
pure := createSimpleQueryPureGo()
pure := createSimpleQueryVanillaGo()
assert.Equal(t, pure, build)
}

Expand All @@ -63,11 +63,11 @@ func Benchmark_Simple_Builder(b *testing.B) {
}
}

func Benchmark_Simple_PureGo(b *testing.B) {
createSimpleQueryPureGo()
func Benchmark_Simple_VanillaGo(b *testing.B) {
createSimpleQueryVanillaGo()
b.ResetTimer()
for i := 0; i < b.N; i++ {
createSimpleQueryPureGo()
createSimpleQueryVanillaGo()
}
}

Expand All @@ -88,7 +88,9 @@ func createIntermediateQuery(id int) string {
),
)
query.Size(45)
query.Sort(es.Sort("name", order.Asc))
query.Sort(
es.Sort("name").Order(order.Asc),
)
query.Source().
Includes("id", "type", "indexedAt", "chapters")

Expand All @@ -99,7 +101,7 @@ func createIntermediateQuery(id int) string {
return string(marshal)
}

func createIntermediateQueryPureGo(id int) string {
func createIntermediateQueryVanillaGo(id int) string {
query := map[string]interface{}{
"_source": map[string]interface{}{
"includes": []interface{}{"id", "type", "indexedAt", "chapters"},
Expand Down Expand Up @@ -155,7 +157,7 @@ func createIntermediateQueryPureGo(id int) string {
func Test_Intermediate_Queries_are_equal(t *testing.T) {
id := 42
build := createIntermediateQuery(id)
pure := createIntermediateQueryPureGo(id)
pure := createIntermediateQueryVanillaGo(id)
assert.Equal(t, pure, build)
}

Expand All @@ -168,12 +170,12 @@ func Benchmark_Intermediate_Builder(b *testing.B) {
}
}

func Benchmark_Intermediate_PureGo(b *testing.B) {
func Benchmark_Intermediate_VanillaGo(b *testing.B) {
id := 42
createIntermediateQueryPureGo(id)
createIntermediateQueryVanillaGo(id)
b.ResetTimer()
for i := 0; i < b.N; i++ {
createIntermediateQueryPureGo(id)
createIntermediateQueryVanillaGo(id)
}
}

Expand All @@ -199,7 +201,9 @@ func createConditionalQuery(items []int) string {
),
)
query.Size(100)
query.Sort(es.Sort("modifiedDate", order.Desc))
query.Sort(
es.Sort("modifiedDate").Order(order.Desc),
)
query.Source().
Includes("id", "type", "indexedAt", "chapters").
Excludes("private.key")
Expand All @@ -215,7 +219,7 @@ func createConditionalQuery(items []int) string {
return string(marshal)
}

func createConditionalQueryPureGo(items []int) string {
func createConditionalQueryVanillaGo(items []int) string {
var flag bool
for _, item := range items {
if item == 21 {
Expand Down Expand Up @@ -289,7 +293,7 @@ func createConditionalQueryPureGo(items []int) string {
func Test_Conditional_Queries_are_equal(t *testing.T) {
items := []int{1, 1, 2, 3, 5, 8, 13, 21, 34, 55}
build := createConditionalQuery(items)
pure := createConditionalQueryPureGo(items)
pure := createConditionalQueryVanillaGo(items)
assert.Equal(t, pure, build)
}

Expand All @@ -302,12 +306,12 @@ func Benchmark_Conditional_Builder(b *testing.B) {
}
}

func Benchmark_Conditional_PureGo(b *testing.B) {
func Benchmark_Conditional_VanillaGo(b *testing.B) {
items := []int{1, 1, 2, 3, 5, 8, 13, 21, 34, 55}
createConditionalQueryPureGo(items)
createConditionalQueryVanillaGo(items)
b.ResetTimer()
for i := 0; i < b.N; i++ {
createConditionalQueryPureGo(items)
createConditionalQueryVanillaGo(items)
}
}

Expand Down Expand Up @@ -338,9 +342,9 @@ func createComplexQuery(id int) string {
query.Size(100)
query.From(5000)
query.Sort(
es.Sort("modifiedDate", order.Desc),
es.SortWithMode("name", order.Asc, mode.Median),
es.Sort("indexedAt", order.Asc),
es.Sort("modifiedDate").Order(order.Desc),
es.Sort("name").Order(order.Asc).Mode(mode.Median),
es.Sort("indexedAt").Order(order.Asc),
)
query.Source().
Includes("id", "type", "indexedAt", "chapters").
Expand All @@ -356,7 +360,7 @@ func createComplexQuery(id int) string {
return string(marshal)
}

func createComplexQueryPureGo(id int) string {
func createComplexQueryVanillaGo(id int) string {
query := map[string]interface{}{
"_source": map[string]interface{}{
"includes": []interface{}{"id", "type", "indexedAt", "chapters"},
Expand Down Expand Up @@ -451,7 +455,7 @@ func createComplexQueryPureGo(id int) string {
func Test_Complex_Queries_are_equal(t *testing.T) {
id := 76
build := createComplexQuery(id)
pure := createComplexQueryPureGo(id)
pure := createComplexQueryVanillaGo(id)
assert.Equal(t, pure, build)
}

Expand All @@ -464,12 +468,12 @@ func Benchmark_Complex_Builder(b *testing.B) {
}
}

func Benchmark_Complex_PureGo(b *testing.B) {
func Benchmark_Complex_VanillaGo(b *testing.B) {
id := 76
createComplexQueryPureGo(id)
createComplexQueryVanillaGo(id)
b.ResetTimer()
for i := 0; i < b.N; i++ {
createComplexQueryPureGo(id)
createComplexQueryVanillaGo(id)
}
}

Expand All @@ -494,7 +498,7 @@ func createTyExampleQuery(brandIds []int64, storefrontIds []string) string {
return string(marshal)
}

func createTyExampleQueryPureGo(brandIds []int64, storefrontIds []string) string {
func createTyExampleQueryVanillaGo(brandIds []int64, storefrontIds []string) string {
query := map[string]interface{}{
"size": 1,
"query": map[string]interface{}{
Expand Down Expand Up @@ -532,7 +536,7 @@ func Test_TyExample_Queries_are_equal(t *testing.T) {
brandIds := []int64{11, 22, 33, 44}
storefrontIds := []string{"35", "36", "43", "48", "49", "50"}
build := createTyExampleQuery(brandIds, storefrontIds)
pure := createTyExampleQueryPureGo(brandIds, storefrontIds)
pure := createTyExampleQueryVanillaGo(brandIds, storefrontIds)
assert.Equal(t, pure, build)
}

Expand All @@ -546,13 +550,13 @@ func Benchmark_Ty_Example_Builder(b *testing.B) {
}
}

func Benchmark_Ty_Example_PureGo(b *testing.B) {
func Benchmark_Ty_Example_VanillaGo(b *testing.B) {
brandIds := []int64{11, 22, 33, 44}
storefrontIds := []string{"35", "36", "43", "48", "49", "50"}
createTyExampleQueryPureGo(brandIds, storefrontIds)
createTyExampleQueryVanillaGo(brandIds, storefrontIds)
b.ResetTimer()
for i := 0; i < b.N; i++ {
createTyExampleQueryPureGo(brandIds, storefrontIds)
createTyExampleQueryVanillaGo(brandIds, storefrontIds)
}
}

Expand All @@ -578,7 +582,7 @@ func createNestedQuery() string {
return string(marshal)
}

func createNestedQueryPureGo() string {
func createNestedQueryVanillaGo() string {
query := map[string]interface{}{
"query": map[string]interface{}{
"nested": map[string]interface{}{
Expand Down Expand Up @@ -617,7 +621,7 @@ func createNestedQueryPureGo() string {

func Test_Nested_Queries_are_equal(t *testing.T) {
build := createNestedQuery()
pure := createNestedQueryPureGo()
pure := createNestedQueryVanillaGo()
assert.Equal(t, pure, build)
}

Expand All @@ -629,11 +633,11 @@ func Benchmark_Nested_Example_Builder(b *testing.B) {
}
}

func Benchmark_Nested_Example_PureGo(b *testing.B) {
createNestedQueryPureGo()
func Benchmark_Nested_Example_VanillaGo(b *testing.B) {
createNestedQueryVanillaGo()
b.ResetTimer()
for i := 0; i < b.N; i++ {
createNestedQueryPureGo()
createNestedQueryVanillaGo()
}
}

Expand All @@ -653,8 +657,8 @@ func createAggsQuery() string {
query.
Size(5_000).
Sort(
es.Sort("modifiedDate", order.Desc),
es.Sort("indexedAt", order.Desc),
es.Sort("modifiedDate").Order(order.Desc),
es.Sort("indexedAt").Order(order.Desc),
)

query.
Expand Down Expand Up @@ -683,7 +687,7 @@ func createAggsQuery() string {
return string(marshal)
}

func createAggsQueryPureGo() string {
func createAggsQueryVanillaGo() string {
query := map[string]interface{}{
"size": 5000,
"sort": []map[string]interface{}{
Expand Down Expand Up @@ -756,7 +760,7 @@ func createAggsQueryPureGo() string {

func Test_Aggs_Queries_are_equal(t *testing.T) {
build := createAggsQuery()
pure := createAggsQueryPureGo()
pure := createAggsQueryVanillaGo()
assert.Equal(t, pure, build)
}

Expand All @@ -768,10 +772,10 @@ func Benchmark_Aggs_Example_Builder(b *testing.B) {
}
}

func Benchmark_Aggs_Example_PureGo(b *testing.B) {
createAggsQueryPureGo()
func Benchmark_Aggs_Example_VanillaGo(b *testing.B) {
createAggsQueryVanillaGo()
b.ResetTimer()
for i := 0; i < b.N; i++ {
createAggsQueryPureGo()
createAggsQueryVanillaGo()
}
}
28 changes: 17 additions & 11 deletions es/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,27 @@ func (o Object) From(from int) Object {
return o
}

func SortWithMode(field string, order Order.Order, mode Mode.Mode) sortType {
o := Object{}
if order != Order.Default {
o["order"] = order
}
if mode != Mode.Default {
o["mode"] = mode
}
func Sort(field string) sortType {
return sortType{
field: o,
field: Object{},
}
}

func (s sortType) putInTheField(key string, value any) sortType {
for field := range s {
if fieldObject, ok := s[field].(Object); ok {
fieldObject[key] = value
}
}
return s
}

func (s sortType) Order(order Order.Order) sortType {
return s.putInTheField("order", order)
}

func Sort(field string, order Order.Order) sortType {
return SortWithMode(field, order, Mode.Default)
func (s sortType) Mode(mode Mode.Mode) sortType {
return s.putInTheField("mode", mode)
}

func (o Object) Sort(sorts ...sortType) Object {
Expand Down
27 changes: 18 additions & 9 deletions es/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ func Test_Sort_should_exist_on_es_package(t *testing.T) {
assert.NotNil(t, es.Sort)
}

func Test_Sort_should_return_sortType(t *testing.T) {
func Test_Sort_should_return_sortType_with_order(t *testing.T) {
// Given
sort := es.Sort("name", Order.Asc)
sort := es.Sort("name").Order(Order.Asc)

// When
bodyType := reflect.TypeOf(sort).String()
Expand All @@ -272,14 +272,23 @@ func Test_Sort_should_return_sortType(t *testing.T) {
assert.Equal(t, "{\"name\":{\"order\":\"asc\"}}", bodyJSON)
}

func Test_SortWithMode_should_exist_on_es_package(t *testing.T) {
// Given When Then
assert.NotNil(t, es.SortWithMode)
func Test_Sort_should_return_sortType_with_mode(t *testing.T) {
// Given
sort := es.Sort("age").Mode(Mode.Median)

// When
bodyType := reflect.TypeOf(sort).String()

// Then
assert.NotNil(t, sort)
assert.Equal(t, "es.sortType", bodyType)
bodyJSON := assert.MarshalWithoutError(t, sort)
assert.Equal(t, "{\"age\":{\"mode\":\"median\"}}", bodyJSON)
}

func Test_SortWithMode_should_return_sortType(t *testing.T) {
func Test_Sort_should_return_sortType_with_order_and_mode(t *testing.T) {
// Given
sort := es.SortWithMode("name", Order.Asc, Mode.Sum)
sort := es.Sort("salary").Order(Order.Desc).Mode(Mode.Sum)

// When
bodyType := reflect.TypeOf(sort).String()
Expand All @@ -288,7 +297,7 @@ func Test_SortWithMode_should_return_sortType(t *testing.T) {
assert.NotNil(t, sort)
assert.Equal(t, "es.sortType", bodyType)
bodyJSON := assert.MarshalWithoutError(t, sort)
assert.Equal(t, "{\"name\":{\"mode\":\"sum\",\"order\":\"asc\"}}", bodyJSON)
assert.Equal(t, "{\"salary\":{\"mode\":\"sum\",\"order\":\"desc\"}}", bodyJSON)
}

func Test_Sort_should_add_sort_field_into_Object(t *testing.T) {
Expand All @@ -297,7 +306,7 @@ func Test_Sort_should_add_sort_field_into_Object(t *testing.T) {

// When
_, beforeExists := query["sort"]
query.Sort(es.Sort("name", Order.Desc))
query.Sort(es.Sort("name").Order(Order.Desc))
sort, afterExists := query["sort"]

// Then
Expand Down
2 changes: 1 addition & 1 deletion example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func buildQuery(id int) es.Object {
),
)
query.Size(45)
query.Sort(es.Sort("name", order.Asc))
query.Sort(es.Sort("name").Order(order.Asc))
query.Source().
Includes("id", "type", "indexedAt", "chapters")
return query
Expand Down
Loading

0 comments on commit 802b31e

Please sign in to comment.