suggester_context_geo.go 3.3 KB

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