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 5package elastic 6 7import ( 8 "context" 9 "testing" 10 "time" 11) 12 13func TestTermVectorsBuildURL(t *testing.T) { 14 client := setupTestClientAndCreateIndex(t) 15 16 tests := []struct { 17 Index string 18 Type string 19 Id string 20 Expected string 21 }{ 22 { 23 "twitter", 24 "tweet", 25 "", 26 "/twitter/tweet/_termvectors", 27 }, 28 { 29 "twitter", 30 "tweet", 31 "1", 32 "/twitter/tweet/1/_termvectors", 33 }, 34 } 35 36 for _, test := range tests { 37 builder := client.TermVectors(test.Index, test.Type) 38 if test.Id != "" { 39 builder = builder.Id(test.Id) 40 } 41 path, _, err := builder.buildURL() 42 if err != nil { 43 t.Fatal(err) 44 } 45 if path != test.Expected { 46 t.Errorf("expected %q; got: %q", test.Expected, path) 47 } 48 } 49} 50 51func TestTermVectorsWithId(t *testing.T) { 52 client := setupTestClientAndCreateIndex(t) 53 54 tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} 55 56 // Add a document 57 indexResult, err := client.Index(). 58 Index(testIndexName). 59 Type("tweet"). 60 Id("1"). 61 BodyJson(&tweet1). 62 Refresh("true"). 63 Do(context.TODO()) 64 if err != nil { 65 t.Fatal(err) 66 } 67 if indexResult == nil { 68 t.Errorf("expected result to be != nil; got: %v", indexResult) 69 } 70 71 // TermVectors by specifying ID 72 field := "Message" 73 result, err := client.TermVectors(testIndexName, "tweet"). 74 Id("1"). 75 Fields(field). 76 FieldStatistics(true). 77 TermStatistics(true). 78 Do(context.TODO()) 79 if err != nil { 80 t.Fatal(err) 81 } 82 if result == nil { 83 t.Fatal("expected to return information and statistics") 84 } 85 if !result.Found { 86 t.Errorf("expected found to be %v; got: %v", true, result.Found) 87 } 88} 89 90func TestTermVectorsWithDoc(t *testing.T) { 91 client := setupTestClientAndCreateIndex(t) 92 93 // Travis lags sometimes 94 if isTravis() { 95 time.Sleep(2 * time.Second) 96 } 97 98 // TermVectors by specifying Doc 99 var doc = map[string]interface{}{ 100 "fullname": "John Doe", 101 "text": "twitter test test test", 102 } 103 var perFieldAnalyzer = map[string]string{ 104 "fullname": "keyword", 105 } 106 107 result, err := client.TermVectors(testIndexName, "tweet"). 108 Doc(doc). 109 PerFieldAnalyzer(perFieldAnalyzer). 110 FieldStatistics(true). 111 TermStatistics(true). 112 Do(context.TODO()) 113 if err != nil { 114 t.Fatal(err) 115 } 116 if result == nil { 117 t.Fatal("expected to return information and statistics") 118 } 119 if !result.Found { 120 t.Errorf("expected found to be %v; got: %v", true, result.Found) 121 } 122} 123 124func TestTermVectorsWithFilter(t *testing.T) { 125 client := setupTestClientAndCreateIndex(t) 126 127 // Travis lags sometimes 128 if isTravis() { 129 time.Sleep(2 * time.Second) 130 } 131 132 // TermVectors by specifying Doc 133 var doc = map[string]interface{}{ 134 "fullname": "John Doe", 135 "text": "twitter test test test", 136 } 137 var perFieldAnalyzer = map[string]string{ 138 "fullname": "keyword", 139 } 140 141 result, err := client.TermVectors(testIndexName, "tweet"). 142 Doc(doc). 143 PerFieldAnalyzer(perFieldAnalyzer). 144 FieldStatistics(true). 145 TermStatistics(true). 146 Filter(NewTermvectorsFilterSettings().MinTermFreq(1)). 147 Do(context.TODO()) 148 if err != nil { 149 t.Fatal(err) 150 } 151 if result == nil { 152 t.Fatal("expected to return information and statistics") 153 } 154 if !result.Found { 155 t.Errorf("expected found to be %v; got: %v", true, result.Found) 156 } 157} 158