Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/nouveau/src/nouveau_fabric_search.erl
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,9 @@ merge_fun(Sort) ->
compare_order(Sort, OrderA, OrderB)
end.

%% no sort order specified
compare_order(null, [A | ARest], [B | BRest]) ->
case couch_ejson_compare:less(convert_item(A), convert_item(B)) of
0 ->
compare_order(null, ARest, BRest);
Less ->
Less < 1
end;
%% no sort order specified which means sort by relevance (high to low)
compare_order(null, As, Bs) ->
compare_order([<<"-">>], As, Bs);
%% server-side adds _id on the end of sort order if not present
compare_order([], [A], [B]) ->
couch_ejson_compare:less(convert_item(A), convert_item(B)) < 1;
Expand Down
1 change: 1 addition & 0 deletions test/elixir/test/config/nouveau.elixir
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"search returns all items for GET",
"search returns all items for POST",
"search returns all items (paginated)",
"search returns all matches for hello by relevance",
"search for foo:bar",
"search for numeric ranges with locales",
"multiple values for stored field",
Expand Down
24 changes: 23 additions & 1 deletion test/elixir/test/nouveau_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule NouveauTest do
resp = Couch.post("/#{db_name}/_bulk_docs",
headers: ["Content-Type": "application/json"],
body: %{:docs => [
%{"_id" => "doc4", "foo" => "foo", "bar" => 42, "baz" => "hello there"},
%{"_id" => "doc4", "foo" => "foo", "bar" => 42, "baz" => "hello hello there"},
%{"_id" => "doc3", "foo" => "bar", "bar" => 12.0, "baz" => "hello"},
%{"_id" => "doc1", "foo" => "baz", "bar" => 0, "baz" => "there"},
%{"_id" => "doc2", "foo" => "foobar", "bar" => 100, "baz" => "hi"},
Expand Down Expand Up @@ -85,6 +85,7 @@ defmodule NouveauTest do
index("string", "foo", doc.foo, {store: true});
index("double", "bar", doc.bar, {store: true});
index("stored", "baz", doc.foo);
if (doc.baz) {index("text", "txt", doc.baz);}
}
"""
}
Expand Down Expand Up @@ -117,6 +118,11 @@ defmodule NouveauTest do
Enum.map(hits, fn hit -> hit["doc"]["_id"] end)
end

def get_orders(resp) do
%{:body => %{"hits" => hits}} = resp
Enum.map(hits, fn hit -> hit["order"] end)
end

def get_mango_ids(resp) do
%{:body => %{"docs" => docs}} = resp
Enum.map(docs, fn doc -> doc["_id"] end)
Expand Down Expand Up @@ -187,6 +193,22 @@ defmodule NouveauTest do
assert ids == ["doc1", "doc2", "doc3", "doc4"]
end

@tag :with_db
test "search returns all matches for hello by relevance", context do
db_name = context[:db_name]
create_search_docs(db_name)
create_ddoc(db_name)

url = "/#{db_name}/_design/foo/_nouveau/bar"
resp = Couch.get(url, query: %{q: "txt:hello", include_docs: true})
assert_status_code(resp, 200)
ids = get_ids(resp)
orders = get_orders(resp)
# doc4 scores higher (more hello's)
assert ids == ["doc4", "doc3"]
assert Enum.at(Enum.at(orders, 0), 0)["value"] > Enum.at(Enum.at(orders, 1), 0)["value"]
end

@tag :with_db
test "search returns all items for POST", context do
db_name = context[:db_name]
Expand Down