indices_analyze_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package elastic
  2. import (
  3. "context"
  4. "testing"
  5. )
  6. func TestIndicesAnalyzeURL(t *testing.T) {
  7. client := setupTestClient(t)
  8. tests := []struct {
  9. Index string
  10. Expected string
  11. }{
  12. {
  13. "",
  14. "/_analyze",
  15. },
  16. {
  17. "tweets",
  18. "/tweets/_analyze",
  19. },
  20. }
  21. for _, test := range tests {
  22. path, _, err := client.IndexAnalyze().Index(test.Index).buildURL()
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. if path != test.Expected {
  27. t.Errorf("expected %q; got: %q", test.Expected, path)
  28. }
  29. }
  30. }
  31. func TestIndicesAnalyze(t *testing.T) {
  32. client := setupTestClient(t)
  33. // client := setupTestClientAndCreateIndexAndLog(t, SetTraceLog(log.New(os.Stdout, "", 0)))
  34. res, err := client.IndexAnalyze().Text("hello hi guy").Do(context.TODO())
  35. if err != nil {
  36. t.Fatalf("expected no error, got %v", err)
  37. }
  38. if len(res.Tokens) != 3 {
  39. t.Fatalf("expected %d, got %d (%+v)", 3, len(res.Tokens), res.Tokens)
  40. }
  41. }
  42. func TestIndicesAnalyzeDetail(t *testing.T) {
  43. client := setupTestClient(t)
  44. // client := setupTestClientAndCreateIndexAndLog(t, SetTraceLog(log.New(os.Stdout, "", 0)))
  45. res, err := client.IndexAnalyze().Text("hello hi guy").Explain(true).Do(context.TODO())
  46. if err != nil {
  47. t.Fatalf("expected no error, got %v", err)
  48. }
  49. if len(res.Detail.Analyzer.Tokens) != 3 {
  50. t.Fatalf("expected %d tokens, got %d (%+v)", 3, len(res.Detail.Tokenizer.Tokens), res.Detail.Tokenizer.Tokens)
  51. }
  52. }
  53. func TestIndicesAnalyzeWithIndex(t *testing.T) {
  54. client := setupTestClient(t)
  55. _, err := client.IndexAnalyze().Index("foo").Text("hello hi guy").Do(context.TODO())
  56. if err == nil {
  57. t.Fatal("expected error, got nil")
  58. }
  59. if want, have := "elastic: Error 404 (Not Found): no such index [type=index_not_found_exception]", err.Error(); want != have {
  60. t.Fatalf("expected error %q, got %q", want, have)
  61. }
  62. }
  63. func TestIndicesAnalyzeValidate(t *testing.T) {
  64. client := setupTestClient(t)
  65. _, err := client.IndexAnalyze().Do(context.TODO())
  66. if err == nil {
  67. t.Fatal("expected error, got nil")
  68. }
  69. if want, have := "missing required fields: [Text]", err.Error(); want != have {
  70. t.Fatalf("expected error %q, got %q", want, have)
  71. }
  72. }