suggester_completion_fuzzy.go 4.4 KB

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