Closes elastic/elasticsearch#837 Closes elastic/elasticsearch#310 Original commit: elastic/x-pack-elasticsearch@164817c0c8