| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"net/url" |
| 9 |
"strings" |
| 10 |
) |
| 11 |
|
| 12 |
// FetchSourceContext enables source filtering, i.e. it allows control |
| 13 |
// over how the _source field is returned with every hit. It is used |
| 14 |
// with various endpoints, e.g. when searching for documents, retrieving |
| 15 |
// individual documents, or even updating documents. |
| 16 |
// |
| 17 |
// See https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-request-source-filtering.html |
| 18 |
// for details. |
| 19 |
type FetchSourceContext struct { |
| 20 |
fetchSource bool |
| 21 |
includes []string |
| 22 |
excludes []string |
| 23 |
} |
| 24 |
|
| 25 |
// NewFetchSourceContext returns a new FetchSourceContext. |
| 26 |
func NewFetchSourceContext(fetchSource bool) *FetchSourceContext { |
| 27 |
return &FetchSourceContext{ |
| 28 |
fetchSource: fetchSource, |
| 29 |
includes: make([]string, 0), |
| 30 |
excludes: make([]string, 0), |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
// FetchSource indicates whether to return the _source. |
| 35 |
func (fsc *FetchSourceContext) FetchSource() bool { |
| 36 |
return fsc.fetchSource |
| 37 |
} |
| 38 |
|
| 39 |
// SetFetchSource specifies whether to return the _source. |
| 40 |
func (fsc *FetchSourceContext) SetFetchSource(fetchSource bool) { |
| 41 |
fsc.fetchSource = fetchSource |
| 42 |
} |
| 43 |
|
| 44 |
// Include indicates to return specific parts of the _source. |
| 45 |
// Wildcards are allowed here. |
| 46 |
func (fsc *FetchSourceContext) Include(includes ...string) *FetchSourceContext { |
| 47 |
fsc.includes = append(fsc.includes, includes...) |
| 48 |
return fsc |
| 49 |
} |
| 50 |
|
| 51 |
// Exclude indicates to exclude specific parts of the _source. |
| 52 |
// Wildcards are allowed here. |
| 53 |
func (fsc *FetchSourceContext) Exclude(excludes ...string) *FetchSourceContext { |
| 54 |
fsc.excludes = append(fsc.excludes, excludes...) |
| 55 |
return fsc |
| 56 |
} |
| 57 |
|
| 58 |
// Source returns the JSON-serializable data to be used in a body. |
| 59 |
func (fsc *FetchSourceContext) Source() (interface{}, error) { |
| 60 |
if !fsc.fetchSource { |
| 61 |
return false, nil |
| 62 |
} |
| 63 |
if len(fsc.includes) == 0 && len(fsc.excludes) == 0 { |
| 64 |
return true, nil |
| 65 |
} |
| 66 |
src := make(map[string]interface{}) |
| 67 |
if len(fsc.includes) > 0 { |
| 68 |
src["includes"] = fsc.includes |
| 69 |
} |
| 70 |
if len(fsc.excludes) > 0 { |
| 71 |
src["excludes"] = fsc.excludes |
| 72 |
} |
| 73 |
return src, nil |
| 74 |
} |
| 75 |
|
| 76 |
// Query returns the parameters in a form suitable for a URL query string. |
| 77 |
func (fsc *FetchSourceContext) Query() url.Values { |
| 78 |
params := url.Values{} |
| 79 |
if fsc.fetchSource { |
| 80 |
if len(fsc.includes) > 0 { |
| 81 |
params.Add("_source_include", strings.Join(fsc.includes, ",")) |
| 82 |
} |
| 83 |
if len(fsc.excludes) > 0 { |
| 84 |
params.Add("_source_exclude", strings.Join(fsc.excludes, ",")) |
| 85 |
} |
| 86 |
} else { |
| 87 |
params.Add("_source", "false") |
| 88 |
} |
| 89 |
return params |
| 90 |
} |