| 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 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"encoding/json" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func TestPhraseSuggesterSource(t *testing.T) { |
| 13 |
s := NewPhraseSuggester("name"). |
| 14 |
Text("Xor the Got-Jewel"). |
| 15 |
Analyzer("body"). |
| 16 |
Field("bigram"). |
| 17 |
Size(1). |
| 18 |
RealWordErrorLikelihood(0.95). |
| 19 |
MaxErrors(0.5). |
| 20 |
GramSize(2). |
| 21 |
Highlight("<em>", "</em>") |
| 22 |
src, err := s.Source(true) |
| 23 |
if err != nil { |
| 24 |
t.Fatal(err) |
| 25 |
} |
| 26 |
data, err := json.Marshal(src) |
| 27 |
if err != nil { |
| 28 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 29 |
} |
| 30 |
got := string(data) |
| 31 |
expected := `{"name":{"text":"Xor the Got-Jewel","phrase":{"analyzer":"body","field":"bigram","gram_size":2,"highlight":{"post_tag":"\u003c/em\u003e","pre_tag":"\u003cem\u003e"},"max_errors":0.5,"real_word_error_likelihood":0.95,"size":1}}}` |
| 32 |
if got != expected { |
| 33 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
func TestPhraseSuggesterSourceWithContextQuery(t *testing.T) { |
| 38 |
geomapQ := NewSuggesterGeoMapping("location"). |
| 39 |
Precision("1km", "5m"). |
| 40 |
Neighbors(true). |
| 41 |
FieldName("pin"). |
| 42 |
DefaultLocations(GeoPointFromLatLon(0.0, 0.0)) |
| 43 |
|
| 44 |
s := NewPhraseSuggester("name"). |
| 45 |
Text("Xor the Got-Jewel"). |
| 46 |
Analyzer("body"). |
| 47 |
Field("bigram"). |
| 48 |
Size(1). |
| 49 |
RealWordErrorLikelihood(0.95). |
| 50 |
MaxErrors(0.5). |
| 51 |
GramSize(2). |
| 52 |
Highlight("<em>", "</em>"). |
| 53 |
ContextQuery(geomapQ) |
| 54 |
src, err := s.Source(true) |
| 55 |
if err != nil { |
| 56 |
t.Fatal(err) |
| 57 |
} |
| 58 |
data, err := json.Marshal(src) |
| 59 |
if err != nil { |
| 60 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 61 |
} |
| 62 |
got := string(data) |
| 63 |
expected := `{"name":{"text":"Xor the Got-Jewel","phrase":{"analyzer":"body","context":{"location":{"default":{"lat":0,"lon":0},"neighbors":true,"path":"pin","precision":["1km","5m"],"type":"geo"}},"field":"bigram","gram_size":2,"highlight":{"post_tag":"\u003c/em\u003e","pre_tag":"\u003cem\u003e"},"max_errors":0.5,"real_word_error_likelihood":0.95,"size":1}}}` |
| 64 |
if got != expected { |
| 65 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func TestPhraseSuggesterComplexSource(t *testing.T) { |
| 70 |
g1 := NewDirectCandidateGenerator("body"). |
| 71 |
SuggestMode("always"). |
| 72 |
MinWordLength(1) |
| 73 |
|
| 74 |
g2 := NewDirectCandidateGenerator("reverse"). |
| 75 |
SuggestMode("always"). |
| 76 |
MinWordLength(1). |
| 77 |
PreFilter("reverse"). |
| 78 |
PostFilter("reverse") |
| 79 |
|
| 80 |
s := NewPhraseSuggester("simple_phrase"). |
| 81 |
Text("Xor the Got-Jewel"). |
| 82 |
Analyzer("body"). |
| 83 |
Field("bigram"). |
| 84 |
Size(4). |
| 85 |
RealWordErrorLikelihood(0.95). |
| 86 |
Confidence(2.0). |
| 87 |
GramSize(2). |
| 88 |
CandidateGenerators(g1, g2). |
| 89 |
CollateQuery(`"match":{"{{field_name}}" : "{{suggestion}}"}`). |
| 90 |
CollateParams(map[string]interface{}{"field_name": "title"}). |
| 91 |
CollatePreference("_primary"). |
| 92 |
CollatePrune(true) |
| 93 |
src, err := s.Source(true) |
| 94 |
if err != nil { |
| 95 |
t.Fatal(err) |
| 96 |
} |
| 97 |
data, err := json.Marshal(src) |
| 98 |
if err != nil { |
| 99 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 100 |
} |
| 101 |
got := string(data) |
| 102 |
expected := `{"simple_phrase":{"text":"Xor the Got-Jewel","phrase":{"analyzer":"body","collate":{"params":{"field_name":"title"},"preference":"_primary","prune":true,"query":"\"match\":{\"{{field_name}}\" : \"{{suggestion}}\"}"},"confidence":2,"direct_generator":[{"field":"body","min_word_length":1,"suggest_mode":"always"},{"field":"reverse","min_word_length":1,"post_filter":"reverse","pre_filter":"reverse","suggest_mode":"always"}],"field":"bigram","gram_size":2,"real_word_error_likelihood":0.95,"size":4}}}` |
| 103 |
if got != expected { |
| 104 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func TestPhraseStupidBackoffSmoothingModel(t *testing.T) { |
| 109 |
s := NewStupidBackoffSmoothingModel(0.42) |
| 110 |
src, err := s.Source() |
| 111 |
if err != nil { |
| 112 |
t.Fatal(err) |
| 113 |
} |
| 114 |
data, err := json.Marshal(src) |
| 115 |
if err != nil { |
| 116 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 117 |
} |
| 118 |
got := string(data) |
| 119 |
// The source does NOT include the smoothing model type! |
| 120 |
expected := `{"discount":0.42}` |
| 121 |
if got != expected { |
| 122 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 123 |
} |
| 124 |
if s.Type() != "stupid_backoff" { |
| 125 |
t.Errorf("expected %q, got: %q", "stupid_backoff", s.Type()) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func TestPhraseLaplaceSmoothingModel(t *testing.T) { |
| 130 |
s := NewLaplaceSmoothingModel(0.63) |
| 131 |
src, err := s.Source() |
| 132 |
if err != nil { |
| 133 |
t.Fatal(err) |
| 134 |
} |
| 135 |
data, err := json.Marshal(src) |
| 136 |
if err != nil { |
| 137 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 138 |
} |
| 139 |
got := string(data) |
| 140 |
// The source does NOT include the smoothing model type! |
| 141 |
expected := `{"alpha":0.63}` |
| 142 |
if got != expected { |
| 143 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 144 |
} |
| 145 |
if s.Type() != "laplace" { |
| 146 |
t.Errorf("expected %q, got: %q", "laplace", s.Type()) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
func TestLinearInterpolationSmoothingModel(t *testing.T) { |
| 151 |
s := NewLinearInterpolationSmoothingModel(0.3, 0.2, 0.05) |
| 152 |
src, err := s.Source() |
| 153 |
if err != nil { |
| 154 |
t.Fatal(err) |
| 155 |
} |
| 156 |
data, err := json.Marshal(src) |
| 157 |
if err != nil { |
| 158 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 159 |
} |
| 160 |
got := string(data) |
| 161 |
// The source does NOT include the smoothing model type! |
| 162 |
expected := `{"bigram_lambda":0.2,"trigram_lambda":0.3,"unigram_lambda":0.05}` |
| 163 |
if got != expected { |
| 164 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 165 |
} |
| 166 |
if s.Type() != "linear_interpolation" { |
| 167 |
t.Errorf("expected %q, got: %q", "linear_interpolation", s.Type()) |
| 168 |
} |
| 169 |
} |