search_queries_percolator.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. import "errors"
  6. // PercolatorQuery can be used to match queries stored in an index.
  7. //
  8. // For more details, see
  9. // https://www.elastic.co/guide/en/elasticsearch/reference/5.x/query-dsl-percolate-query.html
  10. type PercolatorQuery struct {
  11. field string
  12. documentType string
  13. document interface{}
  14. indexedDocumentIndex string
  15. indexedDocumentType string
  16. indexedDocumentId string
  17. indexedDocumentRouting string
  18. indexedDocumentPreference string
  19. indexedDocumentVersion *int64
  20. }
  21. // NewPercolatorQuery creates and initializes a new Percolator query.
  22. func NewPercolatorQuery() *PercolatorQuery {
  23. return &PercolatorQuery{}
  24. }
  25. func (q *PercolatorQuery) Field(field string) *PercolatorQuery {
  26. q.field = field
  27. return q
  28. }
  29. func (q *PercolatorQuery) DocumentType(typ string) *PercolatorQuery {
  30. q.documentType = typ
  31. return q
  32. }
  33. func (q *PercolatorQuery) Document(doc interface{}) *PercolatorQuery {
  34. q.document = doc
  35. return q
  36. }
  37. func (q *PercolatorQuery) IndexedDocumentIndex(index string) *PercolatorQuery {
  38. q.indexedDocumentIndex = index
  39. return q
  40. }
  41. func (q *PercolatorQuery) IndexedDocumentType(typ string) *PercolatorQuery {
  42. q.indexedDocumentType = typ
  43. return q
  44. }
  45. func (q *PercolatorQuery) IndexedDocumentId(id string) *PercolatorQuery {
  46. q.indexedDocumentId = id
  47. return q
  48. }
  49. func (q *PercolatorQuery) IndexedDocumentRouting(routing string) *PercolatorQuery {
  50. q.indexedDocumentRouting = routing
  51. return q
  52. }
  53. func (q *PercolatorQuery) IndexedDocumentPreference(preference string) *PercolatorQuery {
  54. q.indexedDocumentPreference = preference
  55. return q
  56. }
  57. func (q *PercolatorQuery) IndexedDocumentVersion(version int64) *PercolatorQuery {
  58. q.indexedDocumentVersion = &version
  59. return q
  60. }
  61. // Source returns JSON for the percolate query.
  62. func (q *PercolatorQuery) Source() (interface{}, error) {
  63. if len(q.field) == 0 {
  64. return nil, errors.New("elastic: Field is required in PercolatorQuery")
  65. }
  66. if len(q.documentType) == 0 {
  67. return nil, errors.New("elastic: DocumentType is required in PercolatorQuery")
  68. }
  69. if q.document == nil {
  70. return nil, errors.New("elastic: Document is required in PercolatorQuery")
  71. }
  72. // {
  73. // "percolate" : { ... }
  74. // }
  75. source := make(map[string]interface{})
  76. params := make(map[string]interface{})
  77. source["percolate"] = params
  78. params["field"] = q.field
  79. params["document_type"] = q.documentType
  80. params["document"] = q.document
  81. if len(q.indexedDocumentIndex) > 0 {
  82. params["index"] = q.indexedDocumentIndex
  83. }
  84. if len(q.indexedDocumentType) > 0 {
  85. params["type"] = q.indexedDocumentType
  86. }
  87. if len(q.indexedDocumentId) > 0 {
  88. params["id"] = q.indexedDocumentId
  89. }
  90. if len(q.indexedDocumentRouting) > 0 {
  91. params["routing"] = q.indexedDocumentRouting
  92. }
  93. if len(q.indexedDocumentPreference) > 0 {
  94. params["preference"] = q.indexedDocumentPreference
  95. }
  96. if q.indexedDocumentVersion != nil {
  97. params["version"] = *q.indexedDocumentVersion
  98. }
  99. return source, nil
  100. }