Skip to content

Commit c03bd8c

Browse files
committed
Add Escape option to QueryStringQuery
The QueryStringQuery has an Escape option in 2.x (missing in 1.x). It is not documented, but available in the Java source. Also added a test for the TimeZone option (see #247).
1 parent 110d596 commit c03bd8c

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Ryan Schmukler [@rschmukler](https://github.com/rschmukler)
3535
Sacheendra talluri [@sacheendra](https://github.com/sacheendra)
3636
Sean DuBois [@Sean-Der](https://github.com/Sean-Der)
3737
Shalin LK [@shalinlk](https://github.com/shalinlk)
38+
Stephen Kubovic [@stephenkubovic](https://github.com/stephenkubovic)
3839
Stuart Warren [@Woz](https://github.com/stuart-warren)
3940
Sundar [@sundarv85](https://github.com/sundarv85)
4041
Tetsuya Morimoto [@t2y](https://github.com/t2y)

search_queries_query_string.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type QueryStringQuery struct {
4141
queryName string
4242
timeZone string
4343
maxDeterminizedStates *int
44+
escape *bool
4445
}
4546

4647
// NewQueryStringQuery creates and initializes a new QueryStringQuery.
@@ -244,6 +245,12 @@ func (q *QueryStringQuery) TimeZone(timeZone string) *QueryStringQuery {
244245
return q
245246
}
246247

248+
// Escape performs escaping of the query string.
249+
func (q *QueryStringQuery) Escape(escape bool) *QueryStringQuery {
250+
q.escape = &escape
251+
return q
252+
}
253+
247254
// Source returns JSON for the query.
248255
func (q *QueryStringQuery) Source() (interface{}, error) {
249256
source := make(map[string]interface{})
@@ -344,6 +351,9 @@ func (q *QueryStringQuery) Source() (interface{}, error) {
344351
if q.timeZone != "" {
345352
query["time_zone"] = q.timeZone
346353
}
354+
if q.escape != nil {
355+
query["escape"] = *q.escape
356+
}
347357

348358
return source, nil
349359
}

search_queries_query_string_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,21 @@ func TestQueryStringQuery(t *testing.T) {
2626
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
2727
}
2828
}
29+
30+
func TestQueryStringQueryTimeZone(t *testing.T) {
31+
q := NewQueryStringQuery(`tweet_date:[2015-01-01 TO 2017-12-31]`)
32+
q = q.TimeZone("Europe/Berlin")
33+
src, err := q.Source()
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
data, err := json.Marshal(src)
38+
if err != nil {
39+
t.Fatalf("marshaling to JSON failed: %v", err)
40+
}
41+
got := string(data)
42+
expected := `{"query_string":{"query":"tweet_date:[2015-01-01 TO 2017-12-31]","time_zone":"Europe/Berlin"}}`
43+
if got != expected {
44+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
45+
}
46+
}

0 commit comments

Comments
 (0)