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