search_queries_nested_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 TestNestedQuery(t *testing.T) {
  10. bq := NewBoolQuery()
  11. bq = bq.Must(NewTermQuery("obj1.name", "blue"))
  12. bq = bq.Must(NewRangeQuery("obj1.count").Gt(5))
  13. q := NewNestedQuery("obj1", bq).QueryName("qname")
  14. src, err := q.Source()
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. data, err := json.Marshal(src)
  19. if err != nil {
  20. t.Fatalf("marshaling to JSON failed: %v", err)
  21. }
  22. got := string(data)
  23. expected := `{"nested":{"_name":"qname","path":"obj1","query":{"bool":{"must":[{"term":{"obj1.name":"blue"}},{"range":{"obj1.count":{"from":5,"include_lower":false,"include_upper":true,"to":null}}}]}}}}`
  24. if got != expected {
  25. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  26. }
  27. }
  28. func TestNestedQueryWithInnerHit(t *testing.T) {
  29. bq := NewBoolQuery()
  30. bq = bq.Must(NewTermQuery("obj1.name", "blue"))
  31. bq = bq.Must(NewRangeQuery("obj1.count").Gt(5))
  32. q := NewNestedQuery("obj1", bq)
  33. q = q.QueryName("qname")
  34. q = q.InnerHit(NewInnerHit().Name("comments").Query(NewTermQuery("user", "olivere")))
  35. src, err := q.Source()
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. data, err := json.Marshal(src)
  40. if err != nil {
  41. t.Fatalf("marshaling to JSON failed: %v", err)
  42. }
  43. got := string(data)
  44. expected := `{"nested":{"_name":"qname","inner_hits":{"name":"comments","query":{"term":{"user":"olivere"}}},"path":"obj1","query":{"bool":{"must":[{"term":{"obj1.name":"blue"}},{"range":{"obj1.count":{"from":5,"include_lower":false,"include_upper":true,"to":null}}}]}}}}`
  45. if got != expected {
  46. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  47. }
  48. }
  49. func TestNestedQueryWithIgnoreUnmapped(t *testing.T) {
  50. var tests = []struct {
  51. query *BoolQuery
  52. expected string
  53. }{
  54. {
  55. NewBoolQuery().Must(NewNestedQuery("path", NewTermQuery("test", "test"))),
  56. `{"bool":{"must":{"nested":{"path":"path","query":{"term":{"test":"test"}}}}}}`,
  57. },
  58. {
  59. NewBoolQuery().Must(NewNestedQuery("path", NewTermQuery("test", "test")).IgnoreUnmapped(true)),
  60. `{"bool":{"must":{"nested":{"ignore_unmapped":true,"path":"path","query":{"term":{"test":"test"}}}}}}`,
  61. },
  62. {
  63. NewBoolQuery().Must(NewNestedQuery("path", NewTermQuery("test", "test")).IgnoreUnmapped(false)),
  64. `{"bool":{"must":{"nested":{"ignore_unmapped":false,"path":"path","query":{"term":{"test":"test"}}}}}}`,
  65. },
  66. }
  67. for _, test := range tests {
  68. src, err := test.query.Source()
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. data, err := json.Marshal(src)
  73. if err != nil {
  74. t.Fatalf("marshaling to JSON failed: %v", err)
  75. }
  76. got := string(data)
  77. if got != test.expected {
  78. t.Errorf("expected\n%s\n,got:\n%s", test.expected, got)
  79. }
  80. }
  81. }