Added kimchi's changes for https://github.com/elasticsearch/elasticsearch/issues/4887.