suggester_context_category.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // -- SuggesterCategoryMapping --
  6. // SuggesterCategoryMapping provides a mapping for a category context in a suggester.
  7. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/suggester-context.html#_category_mapping.
  8. type SuggesterCategoryMapping struct {
  9. name string
  10. fieldName string
  11. defaultValues []string
  12. }
  13. // NewSuggesterCategoryMapping creates a new SuggesterCategoryMapping.
  14. func NewSuggesterCategoryMapping(name string) *SuggesterCategoryMapping {
  15. return &SuggesterCategoryMapping{
  16. name: name,
  17. defaultValues: make([]string, 0),
  18. }
  19. }
  20. func (q *SuggesterCategoryMapping) DefaultValues(values ...string) *SuggesterCategoryMapping {
  21. q.defaultValues = append(q.defaultValues, values...)
  22. return q
  23. }
  24. func (q *SuggesterCategoryMapping) FieldName(fieldName string) *SuggesterCategoryMapping {
  25. q.fieldName = fieldName
  26. return q
  27. }
  28. // Source returns a map that will be used to serialize the context query as JSON.
  29. func (q *SuggesterCategoryMapping) Source() (interface{}, error) {
  30. source := make(map[string]interface{})
  31. x := make(map[string]interface{})
  32. source[q.name] = x
  33. x["type"] = "category"
  34. switch len(q.defaultValues) {
  35. case 0:
  36. x["default"] = q.defaultValues
  37. case 1:
  38. x["default"] = q.defaultValues[0]
  39. default:
  40. x["default"] = q.defaultValues
  41. }
  42. if q.fieldName != "" {
  43. x["path"] = q.fieldName
  44. }
  45. return source, nil
  46. }
  47. // -- SuggesterCategoryQuery --
  48. // SuggesterCategoryQuery provides querying a category context in a suggester.
  49. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/suggester-context.html#_category_query.
  50. type SuggesterCategoryQuery struct {
  51. name string
  52. values map[string]*int
  53. }
  54. // NewSuggesterCategoryQuery creates a new SuggesterCategoryQuery.
  55. func NewSuggesterCategoryQuery(name string, values ...string) *SuggesterCategoryQuery {
  56. q := &SuggesterCategoryQuery{
  57. name: name,
  58. values: make(map[string]*int),
  59. }
  60. if len(values) > 0 {
  61. q.Values(values...)
  62. }
  63. return q
  64. }
  65. func (q *SuggesterCategoryQuery) Value(val string) *SuggesterCategoryQuery {
  66. q.values[val] = nil
  67. return q
  68. }
  69. func (q *SuggesterCategoryQuery) ValueWithBoost(val string, boost int) *SuggesterCategoryQuery {
  70. q.values[val] = &boost
  71. return q
  72. }
  73. func (q *SuggesterCategoryQuery) Values(values ...string) *SuggesterCategoryQuery {
  74. for _, val := range values {
  75. q.values[val] = nil
  76. }
  77. return q
  78. }
  79. // Source returns a map that will be used to serialize the context query as JSON.
  80. func (q *SuggesterCategoryQuery) Source() (interface{}, error) {
  81. source := make(map[string]interface{})
  82. switch len(q.values) {
  83. case 0:
  84. source[q.name] = make([]string, 0)
  85. default:
  86. contexts := make([]interface{}, 0)
  87. for val, boost := range q.values {
  88. context := make(map[string]interface{})
  89. context["context"] = val
  90. if boost != nil {
  91. context["boost"] = *boost
  92. }
  93. contexts = append(contexts, context)
  94. }
  95. source[q.name] = contexts
  96. }
  97. return source, nil
  98. }