suggester_context.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "errors"
  6. // SuggesterContextQuery is used to define context information within
  7. // a suggestion request.
  8. type SuggesterContextQuery interface {
  9. Source() (interface{}, error)
  10. }
  11. // ContextSuggester is a fast suggester for e.g. type-ahead completion that supports filtering and boosting based on contexts.
  12. // See https://www.elastic.co/guide/en/elasticsearch/reference/current/suggester-context.html
  13. // for more details.
  14. type ContextSuggester struct {
  15. Suggester
  16. name string
  17. prefix string
  18. field string
  19. size *int
  20. contextQueries []SuggesterContextQuery
  21. }
  22. // Creates a new context suggester.
  23. func NewContextSuggester(name string) *ContextSuggester {
  24. return &ContextSuggester{
  25. name: name,
  26. contextQueries: make([]SuggesterContextQuery, 0),
  27. }
  28. }
  29. func (q *ContextSuggester) Name() string {
  30. return q.name
  31. }
  32. func (q *ContextSuggester) Prefix(prefix string) *ContextSuggester {
  33. q.prefix = prefix
  34. return q
  35. }
  36. func (q *ContextSuggester) Field(field string) *ContextSuggester {
  37. q.field = field
  38. return q
  39. }
  40. func (q *ContextSuggester) Size(size int) *ContextSuggester {
  41. q.size = &size
  42. return q
  43. }
  44. func (q *ContextSuggester) ContextQuery(query SuggesterContextQuery) *ContextSuggester {
  45. q.contextQueries = append(q.contextQueries, query)
  46. return q
  47. }
  48. func (q *ContextSuggester) ContextQueries(queries ...SuggesterContextQuery) *ContextSuggester {
  49. q.contextQueries = append(q.contextQueries, queries...)
  50. return q
  51. }
  52. // contextSuggesterRequest is necessary because the order in which
  53. // the JSON elements are routed to Elasticsearch is relevant.
  54. // We got into trouble when using plain maps because the text element
  55. // needs to go before the completion element.
  56. type contextSuggesterRequest struct {
  57. Prefix string `json:"prefix"`
  58. Completion interface{} `json:"completion"`
  59. }
  60. // Creates the source for the context suggester.
  61. func (q *ContextSuggester) Source(includeName bool) (interface{}, error) {
  62. cs := &contextSuggesterRequest{}
  63. if q.prefix != "" {
  64. cs.Prefix = q.prefix
  65. }
  66. suggester := make(map[string]interface{})
  67. cs.Completion = suggester
  68. if q.field != "" {
  69. suggester["field"] = q.field
  70. }
  71. if q.size != nil {
  72. suggester["size"] = *q.size
  73. }
  74. switch len(q.contextQueries) {
  75. case 0:
  76. case 1:
  77. src, err := q.contextQueries[0].Source()
  78. if err != nil {
  79. return nil, err
  80. }
  81. suggester["context"] = src
  82. default:
  83. ctxq := make(map[string]interface{})
  84. for _, query := range q.contextQueries {
  85. src, err := query.Source()
  86. if err != nil {
  87. return nil, err
  88. }
  89. // Merge the dictionary into ctxq
  90. m, ok := src.(map[string]interface{})
  91. if !ok {
  92. return nil, errors.New("elastic: context query is not a map")
  93. }
  94. for k, v := range m {
  95. ctxq[k] = v
  96. }
  97. }
  98. suggester["contexts"] = ctxq
  99. }
  100. if !includeName {
  101. return cs, nil
  102. }
  103. source := make(map[string]interface{})
  104. source[q.name] = cs
  105. return source, nil
  106. }