suggester_term.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. // TermSuggester suggests terms based on edit distance.
  6. // For more details, see
  7. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-suggesters-term.html.
  8. type TermSuggester struct {
  9. Suggester
  10. name string
  11. text string
  12. field string
  13. analyzer string
  14. size *int
  15. shardSize *int
  16. contextQueries []SuggesterContextQuery
  17. // fields specific to term suggester
  18. suggestMode string
  19. accuracy *float64
  20. sort string
  21. stringDistance string
  22. maxEdits *int
  23. maxInspections *int
  24. maxTermFreq *float64
  25. prefixLength *int
  26. minWordLength *int
  27. minDocFreq *float64
  28. }
  29. // NewTermSuggester creates a new TermSuggester.
  30. func NewTermSuggester(name string) *TermSuggester {
  31. return &TermSuggester{
  32. name: name,
  33. }
  34. }
  35. func (q *TermSuggester) Name() string {
  36. return q.name
  37. }
  38. func (q *TermSuggester) Text(text string) *TermSuggester {
  39. q.text = text
  40. return q
  41. }
  42. func (q *TermSuggester) Field(field string) *TermSuggester {
  43. q.field = field
  44. return q
  45. }
  46. func (q *TermSuggester) Analyzer(analyzer string) *TermSuggester {
  47. q.analyzer = analyzer
  48. return q
  49. }
  50. func (q *TermSuggester) Size(size int) *TermSuggester {
  51. q.size = &size
  52. return q
  53. }
  54. func (q *TermSuggester) ShardSize(shardSize int) *TermSuggester {
  55. q.shardSize = &shardSize
  56. return q
  57. }
  58. func (q *TermSuggester) ContextQuery(query SuggesterContextQuery) *TermSuggester {
  59. q.contextQueries = append(q.contextQueries, query)
  60. return q
  61. }
  62. func (q *TermSuggester) ContextQueries(queries ...SuggesterContextQuery) *TermSuggester {
  63. q.contextQueries = append(q.contextQueries, queries...)
  64. return q
  65. }
  66. func (q *TermSuggester) SuggestMode(suggestMode string) *TermSuggester {
  67. q.suggestMode = suggestMode
  68. return q
  69. }
  70. func (q *TermSuggester) Accuracy(accuracy float64) *TermSuggester {
  71. q.accuracy = &accuracy
  72. return q
  73. }
  74. func (q *TermSuggester) Sort(sort string) *TermSuggester {
  75. q.sort = sort
  76. return q
  77. }
  78. func (q *TermSuggester) StringDistance(stringDistance string) *TermSuggester {
  79. q.stringDistance = stringDistance
  80. return q
  81. }
  82. func (q *TermSuggester) MaxEdits(maxEdits int) *TermSuggester {
  83. q.maxEdits = &maxEdits
  84. return q
  85. }
  86. func (q *TermSuggester) MaxInspections(maxInspections int) *TermSuggester {
  87. q.maxInspections = &maxInspections
  88. return q
  89. }
  90. func (q *TermSuggester) MaxTermFreq(maxTermFreq float64) *TermSuggester {
  91. q.maxTermFreq = &maxTermFreq
  92. return q
  93. }
  94. func (q *TermSuggester) PrefixLength(prefixLength int) *TermSuggester {
  95. q.prefixLength = &prefixLength
  96. return q
  97. }
  98. func (q *TermSuggester) MinWordLength(minWordLength int) *TermSuggester {
  99. q.minWordLength = &minWordLength
  100. return q
  101. }
  102. func (q *TermSuggester) MinDocFreq(minDocFreq float64) *TermSuggester {
  103. q.minDocFreq = &minDocFreq
  104. return q
  105. }
  106. // termSuggesterRequest is necessary because the order in which
  107. // the JSON elements are routed to Elasticsearch is relevant.
  108. // We got into trouble when using plain maps because the text element
  109. // needs to go before the term element.
  110. type termSuggesterRequest struct {
  111. Text string `json:"text"`
  112. Term interface{} `json:"term"`
  113. }
  114. // Source generates the source for the term suggester.
  115. func (q *TermSuggester) Source(includeName bool) (interface{}, error) {
  116. // "suggest" : {
  117. // "my-suggest-1" : {
  118. // "text" : "the amsterdma meetpu",
  119. // "term" : {
  120. // "field" : "body"
  121. // }
  122. // },
  123. // "my-suggest-2" : {
  124. // "text" : "the rottredam meetpu",
  125. // "term" : {
  126. // "field" : "title",
  127. // }
  128. // }
  129. // }
  130. ts := &termSuggesterRequest{}
  131. if q.text != "" {
  132. ts.Text = q.text
  133. }
  134. suggester := make(map[string]interface{})
  135. ts.Term = suggester
  136. if q.analyzer != "" {
  137. suggester["analyzer"] = q.analyzer
  138. }
  139. if q.field != "" {
  140. suggester["field"] = q.field
  141. }
  142. if q.size != nil {
  143. suggester["size"] = *q.size
  144. }
  145. if q.shardSize != nil {
  146. suggester["shard_size"] = *q.shardSize
  147. }
  148. switch len(q.contextQueries) {
  149. case 0:
  150. case 1:
  151. src, err := q.contextQueries[0].Source()
  152. if err != nil {
  153. return nil, err
  154. }
  155. suggester["context"] = src
  156. default:
  157. ctxq := make([]interface{}, len(q.contextQueries))
  158. for i, query := range q.contextQueries {
  159. src, err := query.Source()
  160. if err != nil {
  161. return nil, err
  162. }
  163. ctxq[i] = src
  164. }
  165. suggester["context"] = ctxq
  166. }
  167. // Specific to term suggester
  168. if q.suggestMode != "" {
  169. suggester["suggest_mode"] = q.suggestMode
  170. }
  171. if q.accuracy != nil {
  172. suggester["accuracy"] = *q.accuracy
  173. }
  174. if q.sort != "" {
  175. suggester["sort"] = q.sort
  176. }
  177. if q.stringDistance != "" {
  178. suggester["string_distance"] = q.stringDistance
  179. }
  180. if q.maxEdits != nil {
  181. suggester["max_edits"] = *q.maxEdits
  182. }
  183. if q.maxInspections != nil {
  184. suggester["max_inspections"] = *q.maxInspections
  185. }
  186. if q.maxTermFreq != nil {
  187. suggester["max_term_freq"] = *q.maxTermFreq
  188. }
  189. if q.prefixLength != nil {
  190. suggester["prefix_length"] = *q.prefixLength
  191. }
  192. if q.minWordLength != nil {
  193. suggester["min_word_len"] = *q.minWordLength
  194. }
  195. if q.minDocFreq != nil {
  196. suggester["min_doc_freq"] = *q.minDocFreq
  197. }
  198. if !includeName {
  199. return ts, nil
  200. }
  201. source := make(map[string]interface{})
  202. source[q.name] = ts
  203. return source, nil
  204. }