Skip to content

Commit a271313

Browse files
authored
Merge pull request #5737 from apache/score-order-fix
default order should be highest score first
2 parents 3c48d7f + 16852e3 commit a271313

3 files changed

Lines changed: 27 additions & 9 deletions

File tree

src/nouveau/src/nouveau_fabric_search.erl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,9 @@ merge_fun(Sort) ->
197197
compare_order(Sort, OrderA, OrderB)
198198
end.
199199

200-
%% no sort order specified
201-
compare_order(null, [A | ARest], [B | BRest]) ->
202-
case couch_ejson_compare:less(convert_item(A), convert_item(B)) of
203-
0 ->
204-
compare_order(null, ARest, BRest);
205-
Less ->
206-
Less < 1
207-
end;
200+
%% no sort order specified which means sort by relevance (high to low)
201+
compare_order(null, As, Bs) ->
202+
compare_order([<<"-">>], As, Bs);
208203
%% server-side adds _id on the end of sort order if not present
209204
compare_order([], [A], [B]) ->
210205
couch_ejson_compare:less(convert_item(A), convert_item(B)) < 1;

test/elixir/test/config/nouveau.elixir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"search returns all items for GET",
77
"search returns all items for POST",
88
"search returns all items (paginated)",
9+
"search returns all matches for hello by relevance",
910
"search for foo:bar",
1011
"search for numeric ranges with locales",
1112
"multiple values for stored field",

test/elixir/test/nouveau_test.exs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule NouveauTest do
1111
resp = Couch.post("/#{db_name}/_bulk_docs",
1212
headers: ["Content-Type": "application/json"],
1313
body: %{:docs => [
14-
%{"_id" => "doc4", "foo" => "foo", "bar" => 42, "baz" => "hello there"},
14+
%{"_id" => "doc4", "foo" => "foo", "bar" => 42, "baz" => "hello hello there"},
1515
%{"_id" => "doc3", "foo" => "bar", "bar" => 12.0, "baz" => "hello"},
1616
%{"_id" => "doc1", "foo" => "baz", "bar" => 0, "baz" => "there"},
1717
%{"_id" => "doc2", "foo" => "foobar", "bar" => 100, "baz" => "hi"},
@@ -85,6 +85,7 @@ defmodule NouveauTest do
8585
index("string", "foo", doc.foo, {store: true});
8686
index("double", "bar", doc.bar, {store: true});
8787
index("stored", "baz", doc.foo);
88+
if (doc.baz) {index("text", "txt", doc.baz);}
8889
}
8990
"""
9091
}
@@ -117,6 +118,11 @@ defmodule NouveauTest do
117118
Enum.map(hits, fn hit -> hit["doc"]["_id"] end)
118119
end
119120

121+
def get_orders(resp) do
122+
%{:body => %{"hits" => hits}} = resp
123+
Enum.map(hits, fn hit -> hit["order"] end)
124+
end
125+
120126
def get_mango_ids(resp) do
121127
%{:body => %{"docs" => docs}} = resp
122128
Enum.map(docs, fn doc -> doc["_id"] end)
@@ -187,6 +193,22 @@ defmodule NouveauTest do
187193
assert ids == ["doc1", "doc2", "doc3", "doc4"]
188194
end
189195

196+
@tag :with_db
197+
test "search returns all matches for hello by relevance", context do
198+
db_name = context[:db_name]
199+
create_search_docs(db_name)
200+
create_ddoc(db_name)
201+
202+
url = "/#{db_name}/_design/foo/_nouveau/bar"
203+
resp = Couch.get(url, query: %{q: "txt:hello", include_docs: true})
204+
assert_status_code(resp, 200)
205+
ids = get_ids(resp)
206+
orders = get_orders(resp)
207+
# doc4 scores higher (more hello's)
208+
assert ids == ["doc4", "doc3"]
209+
assert Enum.at(Enum.at(orders, 0), 0)["value"] > Enum.at(Enum.at(orders, 1), 0)["value"]
210+
end
211+
190212
@tag :with_db
191213
test "search returns all items for POST", context do
192214
db_name = context[:db_name]

0 commit comments

Comments
 (0)