@@ -54,8 +54,80 @@ defmodule Snap.SearchTest do
54
54
assert first_hit . source [ "title" ] == "Document 1"
55
55
56
56
assert % Snap.SearchResponse {
57
- suggest: % { "title" => [ % { text: "doument" , options: [ % { text: "document" } ] } ] }
57
+ suggest: % {
58
+ "title" => [
59
+ % Snap.Suggest {
60
+ text: "doument" ,
61
+ options: [ % Snap.Suggest.Option { freq: 5 , text: "document" , score: score } ]
62
+ }
63
+ ]
64
+ }
58
65
} = search_response
66
+
67
+ assert is_float ( score )
68
+ end
69
+
70
+ test "search with an autocomplete response" do
71
+ { :ok , _ } =
72
+ Snap.Indexes . create ( Cluster , @ test_index , % {
73
+ mappings: % {
74
+ properties: % {
75
+ name: % {
76
+ type: "completion"
77
+ }
78
+ }
79
+ }
80
+ } )
81
+
82
+ [
83
+ % Action.Index { id: 1 , doc: % { name: "Cat" } } ,
84
+ % Action.Index { id: 2 , doc: % { name: "Caracal" } } ,
85
+ % Action.Index { id: 3 , doc: % { name: "Dog" } }
86
+ ]
87
+ |> Snap.Bulk . perform ( Cluster , @ test_index , refresh: true )
88
+
89
+ query = % {
90
+ suggest: % {
91
+ autocomplete: % {
92
+ prefix: "ca" ,
93
+ completion: % {
94
+ field: "name"
95
+ }
96
+ }
97
+ }
98
+ }
99
+
100
+ { :ok , search_response } = Search . search ( Cluster , @ test_index , query )
101
+
102
+ assert % Snap.SearchResponse {
103
+ suggest: % {
104
+ "autocomplete" => [
105
+ % Snap.Suggest {
106
+ text: "ca" ,
107
+ options: [
108
+ % Snap.Suggest.Option {
109
+ id: "2" ,
110
+ text: "Caracal" ,
111
+ score: caracal_score ,
112
+ index: index ,
113
+ source: % { "name" => "Caracal" }
114
+ } ,
115
+ % Snap.Suggest.Option {
116
+ id: "1" ,
117
+ text: "Cat" ,
118
+ score: cat_score ,
119
+ index: index ,
120
+ source: % { "name" => "Cat" }
121
+ }
122
+ ]
123
+ }
124
+ ]
125
+ }
126
+ } = search_response
127
+
128
+ assert is_float ( caracal_score )
129
+ assert is_float ( cat_score )
130
+ assert is_binary ( index )
59
131
end
60
132
61
133
test "count/2 without a query" do
0 commit comments