Skip to content

Commit 44bae82

Browse files
committed
Fix ES issue 28002 by a workaround
You can now pass `Lang("nil")` or `Lang("null")` in `Script` to set the language to `null`. This is a workaround for [1] in ES <= 6.3. I will remove this when 7.x is out. [1] elastic/elasticsearch#28002 [2] elastic/elasticsearch#29039
1 parent dc11d9b commit 44bae82

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
const (
2828
// Version is the current version of Elastic.
29-
Version = "6.1.20"
29+
Version = "6.1.21"
3030

3131
// DefaultURL is the default endpoint of Elasticsearch on the local machine.
3232
// It is used e.g. when initializing a new Client without a specific URL.

script.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ func (s *Script) Source() (interface{}, error) {
8989
} else {
9090
source["id"] = s.script
9191
}
92-
if s.lang != "" {
92+
if s.lang == "null" || s.lang == "nil" {
93+
// HACK(oe) Hotfix for https://github.com/elastic/elasticsearch/issues/28002; remove when 6.3 and/or 7.x are out.
94+
source["lang"] = nil
95+
} else if s.lang != "" {
9396
source["lang"] = s.lang
9497
}
9598
if len(s.params) > 0 {

script_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,23 @@ func TestScriptingStored(t *testing.T) {
5959
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
6060
}
6161
}
62+
63+
// TestScriptingBug28002 is a workaround for
64+
// https://github.com/elastic/elasticsearch/issues/28002
65+
// in that it allows to use a Script with ID but a language of "null".
66+
func TestScriptingBug28002(t *testing.T) {
67+
builder := NewScriptStored("script-with-id").Lang("null").Param("factor", 2.0)
68+
src, err := builder.Source()
69+
if err != nil {
70+
t.Fatal(err)
71+
}
72+
data, err := json.Marshal(src)
73+
if err != nil {
74+
t.Fatalf("marshaling to JSON failed: %v", err)
75+
}
76+
got := string(data)
77+
expected := `{"id":"script-with-id","lang":null,"params":{"factor":2}}`
78+
if got != expected {
79+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
80+
}
81+
}

0 commit comments

Comments
 (0)