search_queries_percolator_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 (
  6. "encoding/json"
  7. "testing"
  8. )
  9. func TestPercolatorQuery(t *testing.T) {
  10. q := NewPercolatorQuery().
  11. Field("query").
  12. DocumentType("doctype").
  13. Document(map[string]interface{}{
  14. "message": "Some message",
  15. })
  16. src, err := q.Source()
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. data, err := json.Marshal(src)
  21. if err != nil {
  22. t.Fatalf("marshaling to JSON failed: %v", err)
  23. }
  24. got := string(data)
  25. expected := `{"percolate":{"document":{"message":"Some message"},"document_type":"doctype","field":"query"}}`
  26. if got != expected {
  27. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  28. }
  29. }
  30. func TestPercolatorQueryWithDetails(t *testing.T) {
  31. q := NewPercolatorQuery().
  32. Field("query").
  33. DocumentType("doctype").
  34. Document(map[string]interface{}{
  35. "message": "Some message",
  36. }).
  37. IndexedDocumentIndex("index").
  38. IndexedDocumentType("type").
  39. IndexedDocumentId("1").
  40. IndexedDocumentRouting("route").
  41. IndexedDocumentPreference("one").
  42. IndexedDocumentVersion(1)
  43. src, err := q.Source()
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. data, err := json.Marshal(src)
  48. if err != nil {
  49. t.Fatalf("marshaling to JSON failed: %v", err)
  50. }
  51. got := string(data)
  52. expected := `{"percolate":{"document":{"message":"Some message"},"document_type":"doctype","field":"query","id":"1","index":"index","preference":"one","routing":"route","type":"type","version":1}}`
  53. if got != expected {
  54. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  55. }
  56. }
  57. func TestPercolatorQueryWithMissingFields(t *testing.T) {
  58. q := NewPercolatorQuery() // no Field, Document, or Query
  59. _, err := q.Source()
  60. if err == nil {
  61. t.Fatal("expected error, got nil")
  62. }
  63. }