anchor.diff:  %% Return true if all of the terms exist within Proximity words from
   %% eachother.
   is_exact_match(Positions) ->
  -    is_exact_match_1(undefined, Positions).
  -is_exact_match_1(LastMin, Positions) ->
  -    case remove_smallest_position(LastMin, Positions) of
  -        undefined -> 
  +    case get_position_heads(Positions) of
  +        undefined ->
  +            %% We've come to the end of a position list. Not an exact match.
               false;
  -        {Min, Max, NextPass} ->
  -            case is_exact_match_2(lists:seq(Min, Max), [hd(X) || X <- NextPass]) of
  -                true -> 
  +        Heads ->
  +            %% This position list represents an exact match if it is
  +            %% sequential, ie: it's in ascending order with no gaps.
  +            case is_sequential(Heads) of
  +                true ->
                       true;
                   false ->
  -                    is_exact_match_1(Min, NextPass)
  +                    NewPositions = remove_next_term_position(Positions),
  +                    is_exact_match(NewPositions)
               end
       end.
  -%% Return true if the two lists match. Broken out because we'll
  -%% eventually need to handle skipped terms in here.
  -%% TODO - Handle skipped terms.
  -is_exact_match_2(L1, L2) ->
  -    L1 == L2.
  -
  +
  +%% Return true if the provided list of integers is sequential.
  +is_sequential(List) ->
  +    Min = lists:min(List),
  +    Max = lists:max(List),
  +    List == lists:seq(Min, Max).
   
   %% Given a result iterator, only return results that are within a
   %% certain proximity of eachother.
  
positive.code:          chain_op/4
          ]).
  -include("riak_search.hrl").
  -define(INDEX_DOCID(Term), ({element(1, Term), element(2, Term)})).
  
  %%% Conduct a proximity match over the terms. The #proximity operator
  
negative.code:     end;
  proximity_iterator({eof, _}, _) ->
      {eof, ignore}.
  
  %% Return true if all of the terms exist within Proximity words from
  %% eachother.
  within_proximity(Proximity, Positions) ->
      case get_position_heads(Positions) of
          undefined ->
              %% We've come to the end of a position list. Not a proximity match.
              false;
          Heads ->
              %% This position list represents a phrase match if the Max
              %% minus the Min value is less than our Proximity target.
              IsInProximity  = abs(lists:max(Heads) - lists:min(Heads)) < Proximity,
              case IsInProximity of
                  true ->
                      true;
                  false ->
                      NewPositions = remove_next_term_position(Positions),
                      within_proximity(Proximity, NewPositions)
              end
      end.
  
  %% Given a list of term positions, remove a single term position
  %% according to a set of rules, and return the new list.
  %%
  %% The term position to remove is either the smallest duplicated
  %% position, or if there are no duplicates, the smallest position
  %% overall.
  %%
  %% This essentially mimics walking through the document from start to
  %% finish, continually removing the earliest occurring term from
  %% consideration. The functions that call this one then check to see
  %% if the new set of positions represent a successfull proximity match
  %% or exact phrase match, depending upon the search query.
  remove_next_term_position(Positions) ->
      %% Figure out which value to remove. This is the smallest
      %% duplicated value, or if no values are duplicated, then the
      %% smallest value.
      ToRemove =
          case get_position_heads(Positions) of
              undefined ->
                  undefined;
              List ->
                  get_smallest_duplicate_or_not(List)
          end,
      remove_next_term_position(ToRemove, lists:reverse(Positions), []).
  remove_next_term_position(ToRemove, [[ToRemove|Ps]|Rest], NextPass) ->
      %% We've found the value to remove, so toss it and don't remove anything else.
      remove_next_term_position(undefined, [Ps|Rest], NextPass);
  remove_next_term_position(_,  [[]|_], _) ->
      %% Reached the end of a position list, no way to continue.
      undefined;
  remove_next_term_position(ToRemove, [Ps|Rest], NextPass) ->
      %% Just loop...
      remove_next_term_position(ToRemove, Rest, [Ps|NextPass]);
  remove_next_term_position(_ToRemove, [], NextPass) ->
      %% We've processed all the position lists for this pass, so continue.
      NextPass.
  
  get_position_heads(undefined) ->
      undefined;
  get_position_heads(List) ->
      get_position_heads(List, []).
  get_position_heads([[]|_Rest], _Acc) ->
      undefined;
  get_position_heads([[H|_]|Rest], Acc) ->
      get_position_heads(Rest, [H|Acc]);
  get_position_heads([], Acc) ->
      lists:reverse(Acc).
  
  %% Given a list of integers, return the smallest integer that is a duplicate.
  get_smallest_duplicate_or_not(undefined) ->
      undefined;
  get_smallest_duplicate_or_not(List) ->
      get_smallest_duplicate_or_not(lists:sort(List), undefined, undefined).
  get_smallest_duplicate_or_not([H,H|Rest], SmallestDup, SmallestVal)
    when SmallestDup == undefined orelse H < SmallestDup ->
      %% We found a duplicate, and it's the first duplicate we've seen,
      %% or it's smaller than the previous one we found, so use this as
      %% the new duplicate value.
      get_smallest_duplicate_or_not(Rest, H, SmallestVal);
  get_smallest_duplicate_or_not([H|Rest], SmallestDup, SmallestVal) 
    when SmallestVal == undefined orelse H < SmallestVal ->
      %% We found a new smallest value.
      get_smallest_duplicate_or_not(Rest, SmallestDup, H);
  get_smallest_duplicate_or_not([_|Rest], SmallestDup, SmallestVal) ->
      %% Next result is not special, ignore it.
      get_smallest_duplicate_or_not(Rest, SmallestDup, SmallestVal);
  get_smallest_duplicate_or_not([], SmallestDup, SmallestVal) ->
      %% Nothing left to process. Return either the smallest duplicate,
      %% the smallest value, or undefined.
      if
          SmallestDup /= undefined -> SmallestDup;
          SmallestVal /= undefined -> SmallestVal;
          true -> undefined
      end.
  
  %% Given a pair of iterators, combine into a single iterator returning
  %% results of the form {Term, PositionLists, NewIterator}. Apart from
  %% the PositionLists, this is very similar to the #intersection
  