Skip to content

Commit 0746557

Browse files
committed
Adapt scoring in HasChildQuery/HasParentQuery
ES 5.0 changed score features in HasChildQuery/HasParentQuery. Close #399
1 parent 7838d16 commit 0746557

5 files changed

+20
-18
lines changed

client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
const (
2626
// Version is the current version of Elastic.
27-
Version = "5.0.10"
27+
Version = "5.0.11"
2828

2929
// DefaultUrl is the default endpoint of Elasticsearch on the local machine.
3030
// It is used e.g. when initializing a new Client without a specific URL.

search_queries_has_child.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type HasChildQuery struct {
1313
query Query
1414
childType string
1515
boost *float64
16-
scoreType string
16+
scoreMode string
1717
minChildren *int
1818
maxChildren *int
1919
shortCircuitCutoff *int
@@ -35,10 +35,11 @@ func (q *HasChildQuery) Boost(boost float64) *HasChildQuery {
3535
return q
3636
}
3737

38-
// ScoreType defines how the scores from the matching child documents
39-
// are mapped into the parent document.
40-
func (q *HasChildQuery) ScoreType(scoreType string) *HasChildQuery {
41-
q.scoreType = scoreType
38+
// ScoreMode defines how the scores from the matching child documents
39+
// are mapped into the parent document. Allowed values are: min, max,
40+
// avg, or none.
41+
func (q *HasChildQuery) ScoreMode(scoreMode string) *HasChildQuery {
42+
q.scoreMode = scoreMode
4243
return q
4344
}
4445

@@ -83,6 +84,7 @@ func (q *HasChildQuery) Source() (interface{}, error) {
8384
// {
8485
// "has_child" : {
8586
// "type" : "blog_tag",
87+
// "score_mode" : "min",
8688
// "query" : {
8789
// "term" : {
8890
// "tag" : "something"
@@ -103,8 +105,8 @@ func (q *HasChildQuery) Source() (interface{}, error) {
103105
if q.boost != nil {
104106
query["boost"] = *q.boost
105107
}
106-
if q.scoreType != "" {
107-
query["score_type"] = q.scoreType
108+
if q.scoreMode != "" {
109+
query["score_mode"] = q.scoreMode
108110
}
109111
if q.minChildren != nil {
110112
query["min_children"] = *q.minChildren

search_queries_has_child_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestHasChildQuery(t *testing.T) {
13-
q := NewHasChildQuery("blog_tag", NewTermQuery("tag", "something"))
13+
q := NewHasChildQuery("blog_tag", NewTermQuery("tag", "something")).ScoreMode("min")
1414
src, err := q.Source()
1515
if err != nil {
1616
t.Fatal(err)
@@ -20,7 +20,7 @@ func TestHasChildQuery(t *testing.T) {
2020
t.Fatalf("marshaling to JSON failed: %v", err)
2121
}
2222
got := string(data)
23-
expected := `{"has_child":{"query":{"term":{"tag":"something"}},"type":"blog_tag"}}`
23+
expected := `{"has_child":{"query":{"term":{"tag":"something"}},"score_mode":"min","type":"blog_tag"}}`
2424
if got != expected {
2525
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
2626
}

search_queries_has_parent.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type HasParentQuery struct {
1616
query Query
1717
parentType string
1818
boost *float64
19-
scoreType string
19+
score *bool
2020
queryName string
2121
innerHit *InnerHit
2222
}
@@ -35,9 +35,9 @@ func (q *HasParentQuery) Boost(boost float64) *HasParentQuery {
3535
return q
3636
}
3737

38-
// ScoreType defines how the parent score is mapped into the child documents.
39-
func (q *HasParentQuery) ScoreType(scoreType string) *HasParentQuery {
40-
q.scoreType = scoreType
38+
// Score defines if the parent score is mapped into the child documents.
39+
func (q *HasParentQuery) Score(score bool) *HasParentQuery {
40+
q.score = &score
4141
return q
4242
}
4343

@@ -80,8 +80,8 @@ func (q *HasParentQuery) Source() (interface{}, error) {
8080
if q.boost != nil {
8181
query["boost"] = *q.boost
8282
}
83-
if q.scoreType != "" {
84-
query["score_type"] = q.scoreType
83+
if q.score != nil {
84+
query["score"] = *q.score
8585
}
8686
if q.queryName != "" {
8787
query["_name"] = q.queryName

search_queries_has_parent_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestHasParentQueryTest(t *testing.T) {
13-
q := NewHasParentQuery("blog", NewTermQuery("tag", "something"))
13+
q := NewHasParentQuery("blog", NewTermQuery("tag", "something")).Score(true)
1414
src, err := q.Source()
1515
if err != nil {
1616
t.Fatal(err)
@@ -20,7 +20,7 @@ func TestHasParentQueryTest(t *testing.T) {
2020
t.Fatalf("marshaling to JSON failed: %v", err)
2121
}
2222
got := string(data)
23-
expected := `{"has_parent":{"parent_type":"blog","query":{"term":{"tag":"something"}}}}`
23+
expected := `{"has_parent":{"parent_type":"blog","query":{"term":{"tag":"something"}},"score":true}}`
2424
if got != expected {
2525
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
2626
}

0 commit comments

Comments
 (0)