Skip to content

Commit efcc614

Browse files
authored
Merge pull request #146 from breakroom/add-support-for-completion-suggests
Add support for completion suggests
2 parents 5e31725 + 4504d3e commit efcc614

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

lib/snap/responses/suggest/option.ex

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,30 @@ defmodule Snap.Suggest.Option do
44
"""
55
defstruct ~w[
66
freq
7+
id
8+
index
79
score
10+
source
811
text
912
]a
1013

1114
def new(response) do
1215
%__MODULE__{
1316
freq: response["freq"],
14-
score: response["score"],
17+
id: response["_id"],
18+
index: response["_index"],
19+
score: response["score"] || response["_score"],
20+
source: response["_source"],
1521
text: response["text"]
1622
}
1723
end
1824

1925
@type t :: %__MODULE__{
2026
freq: integer(),
27+
id: String.t() | nil,
28+
index: String.t() | nil,
2129
score: float(),
30+
source: map() | nil,
2231
text: String.t()
2332
}
2433
end

test/search_test.exs

+73-1
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,80 @@ defmodule Snap.SearchTest do
5454
assert first_hit.source["title"] == "Document 1"
5555

5656
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+
}
5865
} = 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)
59131
end
60132

61133
test "count/2 without a query" do

0 commit comments

Comments
 (0)