suggester_completion_fuzzy.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // FuzzyCompletionSuggester is a CompletionSuggester that allows fuzzy
  6. // completion.
  7. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-suggesters-completion.html
  8. // for details, and
  9. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/search-suggesters-completion.html#fuzzy
  10. // for details about the fuzzy completion suggester.
  11. //
  12. // @Deprecated Use CompletionSuggester with FuzzyOptions instead.
  13. type FuzzyCompletionSuggester struct {
  14. Suggester
  15. name string
  16. text string
  17. field string
  18. analyzer string
  19. size *int
  20. shardSize *int
  21. contextQueries []SuggesterContextQuery
  22. fuzziness interface{}
  23. fuzzyTranspositions *bool
  24. fuzzyMinLength *int
  25. fuzzyPrefixLength *int
  26. unicodeAware *bool
  27. }
  28. // Fuzziness defines the fuzziness which is used in FuzzyCompletionSuggester.
  29. type Fuzziness struct {
  30. }
  31. // Creates a new completion suggester.
  32. func NewFuzzyCompletionSuggester(name string) *FuzzyCompletionSuggester {
  33. return &FuzzyCompletionSuggester{
  34. name: name,
  35. contextQueries: make([]SuggesterContextQuery, 0),
  36. }
  37. }
  38. func (q *FuzzyCompletionSuggester) Name() string {
  39. return q.name
  40. }
  41. func (q *FuzzyCompletionSuggester) Text(text string) *FuzzyCompletionSuggester {
  42. q.text = text
  43. return q
  44. }
  45. func (q *FuzzyCompletionSuggester) Field(field string) *FuzzyCompletionSuggester {
  46. q.field = field
  47. return q
  48. }
  49. func (q *FuzzyCompletionSuggester) Analyzer(analyzer string) *FuzzyCompletionSuggester {
  50. q.analyzer = analyzer
  51. return q
  52. }
  53. func (q *FuzzyCompletionSuggester) Size(size int) *FuzzyCompletionSuggester {
  54. q.size = &size
  55. return q
  56. }
  57. func (q *FuzzyCompletionSuggester) ShardSize(shardSize int) *FuzzyCompletionSuggester {
  58. q.shardSize = &shardSize
  59. return q
  60. }
  61. func (q *FuzzyCompletionSuggester) ContextQuery(query SuggesterContextQuery) *FuzzyCompletionSuggester {
  62. q.contextQueries = append(q.contextQueries, query)
  63. return q
  64. }
  65. func (q *FuzzyCompletionSuggester) ContextQueries(queries ...SuggesterContextQuery) *FuzzyCompletionSuggester {
  66. q.contextQueries = append(q.contextQueries, queries...)
  67. return q
  68. }
  69. // Fuzziness defines the strategy used to describe what "fuzzy" actually
  70. // means for the suggester, e.g. 1, 2, "0", "1..2", ">4", or "AUTO".
  71. // See https://www.elastic.co/guide/en/elasticsearch/reference/5.2/common-options.html#fuzziness
  72. // for a detailed description.
  73. func (q *FuzzyCompletionSuggester) Fuzziness(fuzziness interface{}) *FuzzyCompletionSuggester {
  74. q.fuzziness = fuzziness
  75. return q
  76. }
  77. func (q *FuzzyCompletionSuggester) FuzzyTranspositions(fuzzyTranspositions bool) *FuzzyCompletionSuggester {
  78. q.fuzzyTranspositions = &fuzzyTranspositions
  79. return q
  80. }
  81. func (q *FuzzyCompletionSuggester) FuzzyMinLength(minLength int) *FuzzyCompletionSuggester {
  82. q.fuzzyMinLength = &minLength
  83. return q
  84. }
  85. func (q *FuzzyCompletionSuggester) FuzzyPrefixLength(prefixLength int) *FuzzyCompletionSuggester {
  86. q.fuzzyPrefixLength = &prefixLength
  87. return q
  88. }
  89. func (q *FuzzyCompletionSuggester) UnicodeAware(unicodeAware bool) *FuzzyCompletionSuggester {
  90. q.unicodeAware = &unicodeAware
  91. return q
  92. }
  93. // Creates the source for the completion suggester.
  94. func (q *FuzzyCompletionSuggester) Source(includeName bool) (interface{}, error) {
  95. cs := &completionSuggesterRequest{}
  96. if q.text != "" {
  97. cs.Text = q.text
  98. }
  99. suggester := make(map[string]interface{})
  100. cs.Completion = suggester
  101. if q.analyzer != "" {
  102. suggester["analyzer"] = q.analyzer
  103. }
  104. if q.field != "" {
  105. suggester["field"] = q.field
  106. }
  107. if q.size != nil {
  108. suggester["size"] = *q.size
  109. }
  110. if q.shardSize != nil {
  111. suggester["shard_size"] = *q.shardSize
  112. }
  113. switch len(q.contextQueries) {
  114. case 0:
  115. case 1:
  116. src, err := q.contextQueries[0].Source()
  117. if err != nil {
  118. return nil, err
  119. }
  120. suggester["context"] = src
  121. default:
  122. var ctxq []interface{}
  123. for _, query := range q.contextQueries {
  124. src, err := query.Source()
  125. if err != nil {
  126. return nil, err
  127. }
  128. ctxq = append(ctxq, src)
  129. }
  130. suggester["context"] = ctxq
  131. }
  132. // Fuzzy Completion Suggester fields
  133. fuzzy := make(map[string]interface{})
  134. suggester["fuzzy"] = fuzzy
  135. if q.fuzziness != nil {
  136. fuzzy["fuzziness"] = q.fuzziness
  137. }
  138. if q.fuzzyTranspositions != nil {
  139. fuzzy["transpositions"] = *q.fuzzyTranspositions
  140. }
  141. if q.fuzzyMinLength != nil {
  142. fuzzy["min_length"] = *q.fuzzyMinLength
  143. }
  144. if q.fuzzyPrefixLength != nil {
  145. fuzzy["prefix_length"] = *q.fuzzyPrefixLength
  146. }
  147. if q.unicodeAware != nil {
  148. fuzzy["unicode_aware"] = *q.unicodeAware
  149. }
  150. if !includeName {
  151. return cs, nil
  152. }
  153. source := make(map[string]interface{})
  154. source[q.name] = cs
  155. return source, nil
  156. }