Skip to content

Commit

Permalink
Fix an issue where the next was not set in m_search_result (#3206)
Browse files Browse the repository at this point in the history
* Fix an issue where the next was not set in m_search_result

Also ensure that next is set for queries with a Limit < PageLen.

* Fix get_result/2
  • Loading branch information
mworrell committed Dec 6, 2022
1 parent 9fe053a commit 0bc7668
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 26 deletions.
66 changes: 45 additions & 21 deletions src/models/m_search.erl
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,13 @@
%% @spec m_find_value(Key, Source, Context) -> term()
m_find_value(paged, #m{value=undefined} = M, _Context) ->
M#m{value=paged};
m_find_value(SearchProps, #m{value=paged} = M, Context) ->
M#m{value=search_pager(SearchProps, Context)};
m_find_value(SearchProps, #m{value=undefined} = M, Context) ->
M#m{value=search(SearchProps, Context)};
m_find_value(Key, #m{value=#m_search_result{}} = M, Context) ->
get_result(Key, M#m.value, Context).
m_find_value(SearchProps, #m{value=paged}, Context) ->
search_pager(SearchProps, Context);
m_find_value(SearchProps, #m{value=undefined}, Context) ->
search(SearchProps, Context).

%% @doc Transform a model value to a list, used for template loops
%% @spec m_to_list(Source, Context) -> List
m_to_list(#m{value=#m_search_result{result=undefined}}, _Context) ->
[];
m_to_list(#m{value=#m_search_result{result=Result}}, _Context) ->
Result#search_result.result;
m_to_list(#m{}, _Context) ->
[].

Expand All @@ -84,7 +78,17 @@ search({SearchName, Props}, Context) ->
undefined -> length(Result#search_result.result);
Total -> Total
end,
#m_search_result{result=Result, total=Total1, search_name=SearchName, search_props=Props}
#m_search_result{
result=Result,
total=Total1,
prev=Result#search_result.prev,
next=Result#search_result.next,
page=Result#search_result.page,
pages=Result#search_result.pages,
pagelen=Result#search_result.pagelen,
search_name=SearchName,
search_props=Props
}
catch
throw:Error ->
lager:error("Error in m.search[~p] error: ~p",
Expand All @@ -105,7 +109,17 @@ search_pager({SearchName, Props}, Context) ->
undefined -> length(Result#search_result.result);
Total -> Total
end,
#m_search_result{result=Result, total=Total1, search_name=SearchName, search_props=Props1}
#m_search_result{
result=Result,
total=Total1,
prev=Result#search_result.prev,
next=Result#search_result.next,
page=Result#search_result.page,
pages=Result#search_result.pages,
pagelen=Result#search_result.pagelen,
search_name=SearchName,
search_props=Props1
}
catch
throw:Error ->
lager:error("Error in m.search[~p] error: ~p",
Expand All @@ -121,10 +135,18 @@ empty_result(SearchName, Props, PageLen) ->
result=[],
page=1,
pagelen=PageLen,
next=false,
prev=false,
is_total_estimated=false,
total=0,
pages=1
},
total=0,
next=false,
prev=false,
page=1,
pages=0,
pagelen=PageLen,
search_name=SearchName,
search_props=Props
}.
Expand All @@ -150,18 +172,20 @@ get_result(is_total_estimated, Result, _Context) ->
undefined -> false
end;
get_result(facets, Result, _Context) ->
#search_result{facets = Facets} = Result#m_search_result.result,
Facets;
get_result(pages, Result, _Context) ->
case Result#m_search_result.result of
#search_result{pages=Pages} -> Pages;
undefined -> Result#m_search_result.pages
#search_result{facets = Facets} -> Facets;
undefined -> []
end;
get_result(pages, Result, _Context) ->
Result#m_search_result.pages;
get_result(page, Result, _Context) ->
case Result#m_search_result.result of
#search_result{page=Page} -> Page;
undefined -> Result#m_search_result.page
end;
Result#m_search_result.page;
get_result(pagelen, Result, _Context) ->
Result#m_search_result.pagelen;
get_result(next, Result, _Context) ->
Result#m_search_result.next;
get_result(prev, Result, _Context) ->
Result#m_search_result.prev;
get_result(_Key, _Result, _Context) ->
undefined.

Expand Down
15 changes: 10 additions & 5 deletions src/support/z_search.erl
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,12 @@ sublist(L, Offset, Limit) ->
end.


search_1({SearchName, Props}, Page, PageLen, {Offset, Limit} = OffsetLimit, Context) when is_atom(SearchName), is_list(Props) ->
search_1({SearchName, Props}, Page, PageLen, {Offset, Limit}, Context) when is_atom(SearchName), is_list(Props) ->
Limit1 = if
PageLen < Limit -> Limit;
true -> Limit + 1
end,
OffsetLimit1 = {Offset, Limit1},
Props1 = case proplists:get_all_values(cat, Props) of
[] -> Props;
[[]] -> Props;
Expand All @@ -253,19 +258,19 @@ search_1({SearchName, Props}, Page, PageLen, {Offset, Limit} = OffsetLimit, Cont
CatsX -> [{cat_exclude, CatsX} | proplists:delete(cat_exclude, Props1)]
end,
PropsSorted = lists:keysort(1, Props2),
Q = #search_query{search={SearchName, PropsSorted}, offsetlimit=OffsetLimit},
Q = #search_query{search={SearchName, PropsSorted}, offsetlimit=OffsetLimit1},
PageRest = (Offset - 1) rem PageLen,
case z_notifier:first(Q, Context) of
undefined ->
lager:info("[~p] Unknown search query ~p with ~p", [z_context:site(Context), SearchName, Props]),
#search_result{};
Result when Page =/= undefined ->
handle_search_result(Result, Page, PageLen, OffsetLimit, SearchName, PropsSorted, Context);
handle_search_result(Result, Page, PageLen, OffsetLimit1, SearchName, PropsSorted, Context);
Result when PageRest =:= 0 ->
PageNr = (Offset - 1) div Limit + 1,
handle_search_result(Result, PageNr, Limit, OffsetLimit, SearchName, PropsSorted, Context);
handle_search_result(Result, PageNr, Limit, OffsetLimit1, SearchName, PropsSorted, Context);
Result ->
S = search_result(Result, OffsetLimit, Context),
S = search_result(Result, OffsetLimit1, Context),
S#search_result{
search_name = SearchName,
search_args = PropsSorted
Expand Down

0 comments on commit 0bc7668

Please sign in to comment.