fetch_source_context.go 2.6 KB

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