search_queries_regexp_test.go 1.1 KB

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