anchor.diff:          qilr:close_analyzer(AnalyzerPid)
       end.
   
  +
   %% Run the Query, return the list of keys.
   %% Timeout is in milliseconds.
   %% Return the {Length, Results}.
   search(IndexOrSchema, QueryOps, QueryStart, QueryRows, Timeout) ->
  -    %% Execute the search and collect the results.
  -    SearchRef = stream_search(IndexOrSchema, QueryOps),
  -    Results = collect_results(SearchRef, Timeout, []),
  +    %% Execute the search, collect the results,.
  +    SearchRef1 = stream_search(IndexOrSchema, QueryOps),
  +    F = fun(Results, Acc) -> Acc ++ Results end,
  +    {ok, SearchRef2, Results} = fold_results(SearchRef1, Timeout, F, []),
  +    SortedResults = sort_by_score(SearchRef2, Results),
   
       %% Dedup, and handle start and max results. Return matching
       %% documents.
  -    Results1 = truncate_list(QueryStart, QueryRows, Results),
  -    Length = length(Results),
  +    Results1 = truncate_list(QueryStart, QueryRows, SortedResults),
  +    Length = length(SortedResults),
       {Length, Results1}.
  +
  +%% Run the search query, fold results through function, return final
  +%% accumulator.
  +search_fold(IndexOrSchema, QueryOps, Fun, AccIn, Timeout) ->
  +    %% Execute the search, collect the results,.
  +    SearchRef = stream_search(IndexOrSchema, QueryOps),
  +    {ok, _NewSearchRef, AccOut} = fold_results(SearchRef, Timeout, Fun, AccIn),
  +    AccOut.
   
   search_doc(IndexOrSchema, QueryOps, QueryStart, QueryRows, Timeout) ->
       %% Get results...
  
positive.code:         id=Ref, termcount=NumTerms,
          inputcount=NumInputs, querynorm=QueryNorm }.
  
  %% Gather all results from the provided SearchRef, return the list of
  %% results sorted in descending order by score.
  collect_results(SearchRef, Timeout, Acc) ->
      M = collect_result(SearchRef, Timeout),
      case M of
          {done, _} ->
              sort_by_score(SearchRef, Acc);
          {[], Ref} ->
              collect_results(Ref, Timeout, Acc);
          {Results, Ref} ->
              collect_results(Ref, Timeout, Acc ++ Results)
          %% Dialyzer says this clause is impossible
          % Error ->
          %     Error
      end.
  
  %% Collect one or more individual results in as non-blocking of a
  
negative.code: -define(OPTIMIZER_PROC_CT, 32).
  
  -export([
      %% MapReduce Searching...
      mapred_search/5, mapred_search_stream/6,
  
      %% Searching...
      parse_query/2,
      search/5,
      search_fold/5,
      search_doc/5,
  
      %% Explain...
      explain/2,
  