Avoid classCast exception when indexing messages