Removed duplicate logic by creating general getIdsFromES function