code: def geolocation_sort(field_name, argument, unit, mode=None,
                       distance_type=None):
      
      def inner(asc):
          locations = request.values.getlist(argument, type=str)
          field = {
              '_geo_distance': {
                  field_name: locations,
                  'order': 'asc' if asc else 'desc',
                  'unit': unit,
              }
          }
          if mode:
              field['_geo_distance']['mode'] = mode
          if distance_type:
              field['_geo_distance']['distance_type'] = distance_type
          return field
      return inner
docs: Sort field factory for geo-location based sorting.
  
      :param argument: Name of URL query string field to parse pin location from.
          Multiple locations can be provided. Each location can be either a
          string "latitude,longitude" or a geohash.
      :param unit: Distance unit (e.g. km).
      :param mode: Sort mode (avg, min, max).
      :param distance_type: Distance calculation mode.
      :returns: Function that returns geolocation sort field.