private function sortObjects(array $objects, $serialize = true)
    {
        $documents = array();
        $serializer = $this->sm->getSerializer();

        foreach ($objects as $object) {
            $document = $serialize ? $serializer->serialize($object) : $object;

            $id = (string) $object->getId();
            if (!$id) {
                throw new DoctrineSearchException('Entity must have an id to be indexed');
            }

            $documents[get_class($object)][$id] = $document;
        }

        return $documents;
    }