Test
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_queries_percolator.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/6.2/query-dsl-percolate-query.html
  10. type PercolatorQuery struct {
  11. field string
  12. name string
  13. documentType string // deprecated
  14. documents []interface{}
  15. indexedDocumentIndex string
  16. indexedDocumentType string
  17. indexedDocumentId string
  18. indexedDocumentRouting string
  19. indexedDocumentPreference string
  20. indexedDocumentVersion *int64
  21. }
  22. // NewPercolatorQuery creates and initializes a new Percolator query.
  23. func NewPercolatorQuery() *PercolatorQuery {
  24. return &PercolatorQuery{}
  25. }
  26. func (q *PercolatorQuery) Field(field string) *PercolatorQuery {
  27. q.field = field
  28. return q
  29. }
  30. // Name used for identification purposes in "_percolator_document_slot" response
  31. // field when multiple percolate queries have been specified in the main query.
  32. func (q *PercolatorQuery) Name(name string) *PercolatorQuery {
  33. q.name = name
  34. return q
  35. }
  36. // Deprecated: DocumentType is deprecated as of 6.0.
  37. func (q *PercolatorQuery) DocumentType(typ string) *PercolatorQuery {
  38. q.documentType = typ
  39. return q
  40. }
  41. func (q *PercolatorQuery) Document(docs ...interface{}) *PercolatorQuery {
  42. q.documents = append(q.documents, docs...)
  43. return q
  44. }
  45. func (q *PercolatorQuery) IndexedDocumentIndex(index string) *PercolatorQuery {
  46. q.indexedDocumentIndex = index
  47. return q
  48. }
  49. func (q *PercolatorQuery) IndexedDocumentType(typ string) *PercolatorQuery {
  50. q.indexedDocumentType = typ
  51. return q
  52. }
  53. func (q *PercolatorQuery) IndexedDocumentId(id string) *PercolatorQuery {
  54. q.indexedDocumentId = id
  55. return q
  56. }
  57. func (q *PercolatorQuery) IndexedDocumentRouting(routing string) *PercolatorQuery {
  58. q.indexedDocumentRouting = routing
  59. return q
  60. }
  61. func (q *PercolatorQuery) IndexedDocumentPreference(preference string) *PercolatorQuery {
  62. q.indexedDocumentPreference = preference
  63. return q
  64. }
  65. func (q *PercolatorQuery) IndexedDocumentVersion(version int64) *PercolatorQuery {
  66. q.indexedDocumentVersion = &version
  67. return q
  68. }
  69. // Source returns JSON for the percolate query.
  70. func (q *PercolatorQuery) Source() (interface{}, error) {
  71. if len(q.field) == 0 {
  72. return nil, errors.New("elastic: Field is required in PercolatorQuery")
  73. }
  74. // {
  75. // "percolate" : { ... }
  76. // }
  77. source := make(map[string]interface{})
  78. params := make(map[string]interface{})
  79. source["percolate"] = params
  80. params["field"] = q.field
  81. if q.documentType != "" {
  82. params["document_type"] = q.documentType
  83. }
  84. if q.name != "" {
  85. params["name"] = q.name
  86. }
  87. switch len(q.documents) {
  88. case 0:
  89. case 1:
  90. params["document"] = q.documents[0]
  91. default:
  92. params["documents"] = q.documents
  93. }
  94. if s := q.indexedDocumentIndex; s != "" {
  95. params["index"] = s
  96. }
  97. if s := q.indexedDocumentType; s != "" {
  98. params["type"] = s
  99. }
  100. if s := q.indexedDocumentId; s != "" {
  101. params["id"] = s
  102. }
  103. if s := q.indexedDocumentRouting; s != "" {
  104. params["routing"] = s
  105. }
  106. if s := q.indexedDocumentPreference; s != "" {
  107. params["preference"] = s
  108. }
  109. if v := q.indexedDocumentVersion; v != nil {
  110. params["version"] = *v
  111. }
  112. return source, nil
  113. }