code: def create_document_batches(jsonlines, id_field, max_batch_size=CLOUDSEARCH_MAX_BATCH_SIZE):
      
      batch = []
      fixed_initial_size = 2
  
      def create_entry(line):
          try:
              record = json.loads(line)
          except:
              raise ValueError('Could not parse JSON from: %s' % line)
          key = record[id_field]
          return '{"type":"add","id":%s,"fields":%s}' % (json.dumps(key), line)
  
      current_size = fixed_initial_size
      for line in jsonlines:
          entry = create_entry(line)
          entry_size = len(entry) + 1
  
          if max_batch_size > (current_size + entry_size):
              current_size += entry_size
              batch.append(entry)
          else:
              yield '[' + ','.join(batch) + ']'
              batch = [entry]
              current_size = fixed_initial_size + entry_size
  
      if batch:
          yield '[' + ','.join(batch) + ']'
docs: Create batches in expected AWS Cloudsearch format, limiting the
      byte size per batch according to given max_batch_size
  
      See: http://docs.aws.amazon.com/cloudsearch/latest/developerguide/preparing-data.html