search_queries_regexp_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 TestRegexpQuery(t *testing.T) {
  10. q := NewRegexpQuery("name.first", "s.*y")
  11. src, err := q.Source()
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. data, err := json.Marshal(src)
  16. if err != nil {
  17. t.Fatalf("marshaling to JSON failed: %v", err)
  18. }
  19. got := string(data)
  20. expected := `{"regexp":{"name.first":{"value":"s.*y"}}}`
  21. if got != expected {
  22. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  23. }
  24. }
  25. func TestRegexpQueryWithOptions(t *testing.T) {
  26. q := NewRegexpQuery("name.first", "s.*y").
  27. Boost(1.2).
  28. Flags("INTERSECTION|COMPLEMENT|EMPTY").
  29. QueryName("my_query_name")
  30. src, err := q.Source()
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. data, err := json.Marshal(src)
  35. if err != nil {
  36. t.Fatalf("marshaling to JSON failed: %v", err)
  37. }
  38. got := string(data)
  39. expected := `{"regexp":{"name.first":{"boost":1.2,"flags":"INTERSECTION|COMPLEMENT|EMPTY","name":"my_query_name","value":"s.*y"}}}`
  40. if got != expected {
  41. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  42. }
  43. }