anchor.diff:              %% "More like this id" gives us a search result with 1 doc, but
               %% the interesting stuff is in the 'morelikethis' property of the
               %% search.
  -            Props = case proplists:get_value(morelikethis, Result#search_result.result) of
  +            Props = case proplists:get_value(morelikethis, Result#search_result.result, {obj, []}) of
                           {obj, []} ->
                               [{"numFound", "0"}, {"docs", []}];
  -                        {obj, [{IdS, {obj, Props0}}]} ->
  -                            Props0
  +                        {obj, [{_, {obj, Props0}}]} ->
  +                            Props0;
  +                        Huh ->
  +                            lager:warning("Huh: ~p", [Huh]),
  +                            Huh
                       end,
  -
               %% Extract the matching ids and the total number found
               Ids = [DocId || {obj, [{"id", DocId}]} <- proplists:get_value("docs", Props)],
               {"numFound", Total} = proplists:lookup("numFound", Props),
  
positive.code: %% Give more documents which are similar to <Id>  (solr only)
  map_search_field({morelikethis_id, [Id, Amount]}, _Context) ->
      {["id:", z_convert:to_list(Id), " -category:meta"],
       [{morelikethis, "o,match_title,summary,category", Amount}]};
  
  %% Specifying the return format. Either 'default' (empty) or 'ranked'.
  map_search_field({return_format, _}, _Context) ->
  
negative.code:         {[], _SearchOptions} ->
              #search_result{result=[]};
          {Q, SearchOptions} ->
              case esolr:search(Q, [{fields, "id"}, {start, Offset-1}, {rows, PageLen} | SearchOptions], Solr) of
                  {ok, RespAttrs, Docs, Info} ->
  
                      %% Get the ids
                      Ids = [Id || {doc, [{"id", Id}]} <- Docs],
  
                      %% Map the extended info into a handy format
                      Info1 = filter_empty(lists:flatten([map_info(I) || I <- Info])),
  
                      %% Decide on result format
                      Result = case Info1 of
                                   [] ->
                                       %% No extended info was returned. The result array is just a list of ids.
                                       Ids;
                                   _ ->
                                       %% If extended info was returned, the document
                                       %% ids are in the ".ids" subproperty, alongside
                                       %% the other info (e.g. facetting, highlighting)
                                       [{ids, Ids} | Info1]
                               end,
  
                      %% Compute paging counters
                      {"numFound", Total} = proplists:lookup("numFound", RespAttrs),
                      Pages = mochinum:int_ceil(Total / PageLen),
                      Page = mochinum:int_ceil(Offset / PageLen),
                      Next = if Offset + PageLen < Total -> false; true -> Page+1 end,
                      Prev = if Page > 1 -> Page-1; true -> 1 end,
  
                      Result1 = case proplists:get_value(return_format, Query) of
                                    ranked ->
                                        %% FIXME how to retrieve the ranked value from solr?
                                        [{Id, 1.0} || Id <- Result]; 
                                    _ ->
                                        Result
                                end,
  
                      %% Construct the final search result
                      #search_result{result=Result1, 
                                     total=Total,
                                     pages=Pages,
                                     page=Page,
                                     next=Next,
                                     prev=Prev
                                    };
                  {error, Reason} ->
                      lager:error("Solr search error, reason: ~p", [Reason]),
                      #search_result{result=[], total=0}
              end
      end.
  
  
  