search_queries_fuzzy_like_this.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // FuzzyLikeThisQuery finds documents that are "like" provided text by
  6. // running it against one or more fields.
  7. // For more details, see
  8. // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-flt-query.html
  9. type FuzzyLikeThisQuery struct {
  10. Query
  11. fields []string
  12. boost *float32
  13. likeText *string
  14. fuzziness interface{}
  15. prefixLength *int
  16. maxQueryTerms *int
  17. ignoreTF *bool
  18. analyzer string
  19. failOnUnsupportedField *bool
  20. queryName string
  21. }
  22. // NewFuzzyLikeThisQuery creates a new fuzzy query.
  23. func NewFuzzyLikeThisQuery() FuzzyLikeThisQuery {
  24. q := FuzzyLikeThisQuery{
  25. fields: make([]string, 0),
  26. }
  27. return q
  28. }
  29. func (q FuzzyLikeThisQuery) Field(field string) FuzzyLikeThisQuery {
  30. q.fields = append(q.fields, field)
  31. return q
  32. }
  33. func (q FuzzyLikeThisQuery) Fields(fields ...string) FuzzyLikeThisQuery {
  34. q.fields = append(q.fields, fields...)
  35. return q
  36. }
  37. func (q FuzzyLikeThisQuery) LikeText(likeText string) FuzzyLikeThisQuery {
  38. q.likeText = &likeText
  39. return q
  40. }
  41. // Fuzziness can be an integer/long like 0, 1 or 2 as well as strings like "auto",
  42. // "0..1", "1..4" or "0.0..1.0".
  43. func (q FuzzyLikeThisQuery) Fuzziness(fuzziness interface{}) FuzzyLikeThisQuery {
  44. q.fuzziness = fuzziness
  45. return q
  46. }
  47. func (q FuzzyLikeThisQuery) PrefixLength(prefixLength int) FuzzyLikeThisQuery {
  48. q.prefixLength = &prefixLength
  49. return q
  50. }
  51. func (q FuzzyLikeThisQuery) MaxQueryTerms(maxQueryTerms int) FuzzyLikeThisQuery {
  52. q.maxQueryTerms = &maxQueryTerms
  53. return q
  54. }
  55. func (q FuzzyLikeThisQuery) IgnoreTF(ignoreTF bool) FuzzyLikeThisQuery {
  56. q.ignoreTF = &ignoreTF
  57. return q
  58. }
  59. func (q FuzzyLikeThisQuery) Analyzer(analyzer string) FuzzyLikeThisQuery {
  60. q.analyzer = analyzer
  61. return q
  62. }
  63. func (q FuzzyLikeThisQuery) Boost(boost float32) FuzzyLikeThisQuery {
  64. q.boost = &boost
  65. return q
  66. }
  67. func (q FuzzyLikeThisQuery) FailOnUnsupportedField(fail bool) FuzzyLikeThisQuery {
  68. q.failOnUnsupportedField = &fail
  69. return q
  70. }
  71. func (q FuzzyLikeThisQuery) QueryName(queryName string) FuzzyLikeThisQuery {
  72. q.queryName = queryName
  73. return q
  74. }
  75. // Creates the query source for the ids query.
  76. func (q FuzzyLikeThisQuery) Source() interface{} {
  77. // {
  78. // "fuzzy_like_this" : {
  79. // "fields" : ["name.first", "name.last"],
  80. // "like_text" : "text like this one",
  81. // "max_query_terms" : 12
  82. // }
  83. source := make(map[string]interface{})
  84. query := make(map[string]interface{})
  85. source["fuzzy_like_this"] = query
  86. if len(q.fields) > 0 {
  87. query["fields"] = q.fields
  88. }
  89. query["like_text"] = q.likeText
  90. if q.maxQueryTerms != nil {
  91. query["max_query_terms"] = *q.maxQueryTerms
  92. }
  93. if q.fuzziness != nil {
  94. query["fuzziness"] = q.fuzziness
  95. }
  96. if q.prefixLength != nil {
  97. query["prefix_length"] = *q.prefixLength
  98. }
  99. if q.ignoreTF != nil {
  100. query["ignore_tf"] = *q.ignoreTF
  101. }
  102. if q.boost != nil {
  103. query["boost"] = *q.boost
  104. }
  105. if q.analyzer != "" {
  106. query["analyzer"] = q.analyzer
  107. }
  108. if q.failOnUnsupportedField != nil {
  109. query["fail_on_unsupported_field"] = *q.failOnUnsupportedField
  110. }
  111. if q.queryName != "" {
  112. query["_name"] = q.queryName
  113. }
  114. return source
  115. }