search_queries_match_phrase_prefix.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // MatchPhrasePrefixQuery is the same as match_phrase, except that it allows for
  6. // prefix matches on the last term in the text.
  7. //
  8. // For more details, see
  9. // https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-match-query-phrase-prefix.html
  10. type MatchPhrasePrefixQuery struct {
  11. name string
  12. value interface{}
  13. analyzer string
  14. slop *int
  15. maxExpansions *int
  16. boost *float64
  17. queryName string
  18. }
  19. // NewMatchPhrasePrefixQuery creates and initializes a new MatchPhrasePrefixQuery.
  20. func NewMatchPhrasePrefixQuery(name string, value interface{}) *MatchPhrasePrefixQuery {
  21. return &MatchPhrasePrefixQuery{name: name, value: value}
  22. }
  23. // Analyzer explicitly sets the analyzer to use. It defaults to use explicit
  24. // mapping config for the field, or, if not set, the default search analyzer.
  25. func (q *MatchPhrasePrefixQuery) Analyzer(analyzer string) *MatchPhrasePrefixQuery {
  26. q.analyzer = analyzer
  27. return q
  28. }
  29. // Slop sets the phrase slop if evaluated to a phrase query type.
  30. func (q *MatchPhrasePrefixQuery) Slop(slop int) *MatchPhrasePrefixQuery {
  31. q.slop = &slop
  32. return q
  33. }
  34. // MaxExpansions sets the number of term expansions to use.
  35. func (q *MatchPhrasePrefixQuery) MaxExpansions(n int) *MatchPhrasePrefixQuery {
  36. q.maxExpansions = &n
  37. return q
  38. }
  39. // Boost sets the boost to apply to this query.
  40. func (q *MatchPhrasePrefixQuery) Boost(boost float64) *MatchPhrasePrefixQuery {
  41. q.boost = &boost
  42. return q
  43. }
  44. // QueryName sets the query name for the filter that can be used when
  45. // searching for matched filters per hit.
  46. func (q *MatchPhrasePrefixQuery) QueryName(queryName string) *MatchPhrasePrefixQuery {
  47. q.queryName = queryName
  48. return q
  49. }
  50. // Source returns JSON for the function score query.
  51. func (q *MatchPhrasePrefixQuery) Source() (interface{}, error) {
  52. // {"match_phrase_prefix":{"name":{"query":"value","max_expansions":10}}}
  53. source := make(map[string]interface{})
  54. match := make(map[string]interface{})
  55. source["match_phrase_prefix"] = match
  56. query := make(map[string]interface{})
  57. match[q.name] = query
  58. query["query"] = q.value
  59. if q.analyzer != "" {
  60. query["analyzer"] = q.analyzer
  61. }
  62. if q.slop != nil {
  63. query["slop"] = *q.slop
  64. }
  65. if q.maxExpansions != nil {
  66. query["max_expansions"] = *q.maxExpansions
  67. }
  68. if q.boost != nil {
  69. query["boost"] = *q.boost
  70. }
  71. if q.queryName != "" {
  72. query["_name"] = q.queryName
  73. }
  74. return source, nil
  75. }