Skip to content

Commit

Permalink
feature: add seperate inner_hits with better type safe building exper…
Browse files Browse the repository at this point in the history
…ience
  • Loading branch information
GokselKUCUKSAHIN committed Jan 3, 2025
1 parent 81dceab commit 721c4fc
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 4 deletions.
107 changes: 107 additions & 0 deletions es/inner_hits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package es

type innerHitsType Object

func InnerHits() innerHitsType {
return innerHitsType{}
}

func (ih innerHitsType) Explain(explain bool) innerHitsType {
ih["explain"] = explain
return ih
}

func (ih innerHitsType) From(from int) innerHitsType {
ih["from"] = from
return ih
}

func (ih innerHitsType) IgnoreUnmapped(ignoreUnmapped bool) innerHitsType {
ih["ignore_unmapped"] = ignoreUnmapped
return ih
}

func (ih innerHitsType) Size(size int) innerHitsType {
ih["size"] = size
return ih
}

func (ih innerHitsType) Query(queryClause any) innerHitsType {
ih["query"] = queryClause
return ih
}

func (ih innerHitsType) Name(name string) innerHitsType {
ih["name"] = name
return ih
}

func (ih innerHitsType) SeqNoPrimaryTerm(seqNoPrimaryTerm bool) innerHitsType {
ih["seq_no_primary_term"] = seqNoPrimaryTerm
return ih
}

func (ih innerHitsType) Sort(sorts ...sortType) innerHitsType {
ih["sort"] = sorts
return ih
}

func (ih innerHitsType) SourceFalse() innerHitsType {
ih["_source"] = false
return ih
}

func (ih innerHitsType) SourceIncludes(fields ...string) innerHitsType {
if len(fields) == 0 {
return ih
}
source, ok := ih["_source"].(Object)
if !ok {
source = Object{}
}
includes, ok := source["includes"].(Array)
if !ok {
includes = Array{}
}
for i := 0; i < len(fields); i++ {
includes = append(includes, fields[i])
}
source["includes"] = includes
ih["_source"] = source
return ih
}

func (ih innerHitsType) SourceExcludes(fields ...string) innerHitsType {
if len(fields) == 0 {
return ih
}
source, ok := ih["_source"].(Object)
if !ok {
source = Object{}
}
excludes, exists := source["excludes"].(Array)
if !exists {
excludes = Array{}
}
for i := 0; i < len(fields); i++ {
excludes = append(excludes, fields[i])
}
source["excludes"] = excludes
ih["_source"] = source
return ih
}

func (ih innerHitsType) StoredFields(fields ...string) innerHitsType {
ih["stored_fields"] = fields
return ih
}

func (ih innerHitsType) TrackScores(trackScores bool) innerHitsType {
ih["track_scores"] = trackScores
return ih
}

func (ih innerHitsType) Version(version bool) innerHitsType {
ih["version"] = version
return ih
}
3 changes: 3 additions & 0 deletions es/inner_hits_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package es_test

// TODO: add detailed units test
4 changes: 2 additions & 2 deletions es/nested_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ func Nested[T any](path string, nestedQuery T) nestedType {
// // nested now has an "inner_hits" field with the specified Object in the nested query.
//
// Parameters:
// - innerHits: An es.Object representing the inner hits configuration for the nested query.
// - innerHits: An innerHitsType representing the inner hits configuration for the nested query.
//
// Returns:
//
// The updated nestedType object with the "inner_hits" field set to the specified value.
func (n nestedType) InnerHits(innerHits Object) nestedType {
func (n nestedType) InnerHits(innerHits innerHitsType) nestedType {
return n.putInNested("inner_hits", innerHits)
}

Expand Down
9 changes: 7 additions & 2 deletions es/nested_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@ func Test_Nested_should_have_InnerHits_method(t *testing.T) {
func Test_InnerHits_should_add_inner_hits_field_into_Nested(t *testing.T) {
// Given
query := es.NewQuery(
es.Nested("nested.path", es.Object{}).InnerHits(es.Object{"inner": "hits"}),
es.Nested("nested.path", es.Object{}).
InnerHits(
es.InnerHits().
Size(1_000).
From(5_000),
),
)

// When Then
assert.NotNil(t, query)
bodyJSON := assert.MarshalWithoutError(t, query)
assert.Equal(t, "{\"query\":{\"nested\":{\"inner_hits\":{\"inner\":\"hits\"},\"path\":\"nested.path\",\"query\":{}}}}", bodyJSON)
assert.Equal(t, "{\"query\":{\"nested\":{\"inner_hits\":{\"from\":5000,\"size\":1000},\"path\":\"nested.path\",\"query\":{}}}}", bodyJSON)
}

func Test_Nested_should_have_ScoreMode_method(t *testing.T) {
Expand Down

0 comments on commit 721c4fc

Please sign in to comment.