search_facets_statistical_script.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2012-2015 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. // Statistical facet allows to compute statistical data on a numeric fields.
  6. // The statistical data include count, total, sum of squares, mean (average),
  7. // minimum, maximum, variance, and standard deviation.
  8. // See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-facets-statistical-facet.html
  9. type StatisticalScriptFacet struct {
  10. facetFilter Filter
  11. global *bool
  12. nested string
  13. mode string
  14. lang string
  15. script string
  16. params map[string]interface{}
  17. }
  18. func NewStatisticalScriptFacet() StatisticalScriptFacet {
  19. return StatisticalScriptFacet{
  20. params: make(map[string]interface{}),
  21. }
  22. }
  23. func (f StatisticalScriptFacet) FacetFilter(filter Facet) StatisticalScriptFacet {
  24. f.facetFilter = filter
  25. return f
  26. }
  27. func (f StatisticalScriptFacet) Global(global bool) StatisticalScriptFacet {
  28. f.global = &global
  29. return f
  30. }
  31. func (f StatisticalScriptFacet) Nested(nested string) StatisticalScriptFacet {
  32. f.nested = nested
  33. return f
  34. }
  35. func (f StatisticalScriptFacet) Mode(mode string) StatisticalScriptFacet {
  36. f.mode = mode
  37. return f
  38. }
  39. func (f StatisticalScriptFacet) Lang(lang string) StatisticalScriptFacet {
  40. f.lang = lang
  41. return f
  42. }
  43. func (f StatisticalScriptFacet) Script(script string) StatisticalScriptFacet {
  44. f.script = script
  45. return f
  46. }
  47. func (f StatisticalScriptFacet) Param(name string, value interface{}) StatisticalScriptFacet {
  48. f.params[name] = value
  49. return f
  50. }
  51. func (f StatisticalScriptFacet) addFilterFacetAndGlobal(source map[string]interface{}) {
  52. if f.facetFilter != nil {
  53. source["facet_filter"] = f.facetFilter.Source()
  54. }
  55. if f.nested != "" {
  56. source["nested"] = f.nested
  57. }
  58. if f.global != nil {
  59. source["global"] = *f.global
  60. }
  61. if f.mode != "" {
  62. source["mode"] = f.mode
  63. }
  64. }
  65. func (f StatisticalScriptFacet) Source() interface{} {
  66. source := make(map[string]interface{})
  67. f.addFilterFacetAndGlobal(source)
  68. opts := make(map[string]interface{})
  69. source["statistical"] = opts
  70. opts["script"] = f.script
  71. if f.lang != "" {
  72. opts["lang"] = f.lang
  73. }
  74. if len(f.params) > 0 {
  75. opts["params"] = f.params
  76. }
  77. return source
  78. }