suggester_context_geo.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // -- SuggesterGeoMapping --
  6. // SuggesterGeoMapping provides a mapping for a geolocation context in a suggester.
  7. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/suggester-context.html#_geo_location_mapping.
  8. type SuggesterGeoMapping struct {
  9. name string
  10. defaultLocations []*GeoPoint
  11. precision []string
  12. neighbors *bool
  13. fieldName string
  14. }
  15. // NewSuggesterGeoMapping creates a new SuggesterGeoMapping.
  16. func NewSuggesterGeoMapping(name string) *SuggesterGeoMapping {
  17. return &SuggesterGeoMapping{
  18. name: name,
  19. }
  20. }
  21. func (q *SuggesterGeoMapping) DefaultLocations(locations ...*GeoPoint) *SuggesterGeoMapping {
  22. q.defaultLocations = append(q.defaultLocations, locations...)
  23. return q
  24. }
  25. func (q *SuggesterGeoMapping) Precision(precision ...string) *SuggesterGeoMapping {
  26. q.precision = append(q.precision, precision...)
  27. return q
  28. }
  29. func (q *SuggesterGeoMapping) Neighbors(neighbors bool) *SuggesterGeoMapping {
  30. q.neighbors = &neighbors
  31. return q
  32. }
  33. func (q *SuggesterGeoMapping) FieldName(fieldName string) *SuggesterGeoMapping {
  34. q.fieldName = fieldName
  35. return q
  36. }
  37. // Source returns a map that will be used to serialize the context query as JSON.
  38. func (q *SuggesterGeoMapping) Source() (interface{}, error) {
  39. source := make(map[string]interface{})
  40. x := make(map[string]interface{})
  41. source[q.name] = x
  42. x["type"] = "geo"
  43. if len(q.precision) > 0 {
  44. x["precision"] = q.precision
  45. }
  46. if q.neighbors != nil {
  47. x["neighbors"] = *q.neighbors
  48. }
  49. switch len(q.defaultLocations) {
  50. case 0:
  51. case 1:
  52. x["default"] = q.defaultLocations[0].Source()
  53. default:
  54. var arr []interface{}
  55. for _, p := range q.defaultLocations {
  56. arr = append(arr, p.Source())
  57. }
  58. x["default"] = arr
  59. }
  60. if q.fieldName != "" {
  61. x["path"] = q.fieldName
  62. }
  63. return source, nil
  64. }
  65. // -- SuggesterGeoQuery --
  66. // SuggesterGeoQuery provides querying a geolocation context in a suggester.
  67. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/suggester-context.html#_geo_location_query
  68. type SuggesterGeoQuery struct {
  69. name string
  70. location *GeoPoint
  71. precision []string
  72. }
  73. // NewSuggesterGeoQuery creates a new SuggesterGeoQuery.
  74. func NewSuggesterGeoQuery(name string, location *GeoPoint) *SuggesterGeoQuery {
  75. return &SuggesterGeoQuery{
  76. name: name,
  77. location: location,
  78. precision: make([]string, 0),
  79. }
  80. }
  81. func (q *SuggesterGeoQuery) Precision(precision ...string) *SuggesterGeoQuery {
  82. q.precision = append(q.precision, precision...)
  83. return q
  84. }
  85. // Source returns a map that will be used to serialize the context query as JSON.
  86. func (q *SuggesterGeoQuery) Source() (interface{}, error) {
  87. source := make(map[string]interface{})
  88. if len(q.precision) == 0 {
  89. if q.location != nil {
  90. source[q.name] = q.location.Source()
  91. }
  92. } else {
  93. x := make(map[string]interface{})
  94. source[q.name] = x
  95. if q.location != nil {
  96. x["value"] = q.location.Source()
  97. }
  98. switch len(q.precision) {
  99. case 0:
  100. case 1:
  101. x["precision"] = q.precision[0]
  102. default:
  103. x["precision"] = q.precision
  104. }
  105. }
  106. return source, nil
  107. }