You can use pre_get_posts action to exclude categories from search query.
function wcs_exclude_category_search( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if ( $query->is_search ) {
$query->set( 'cat', '-22, -21' );
}
}
add_action( 'pre_get_posts', 'wcs_exclude_category_search', 1 );
You should paste this code in your theme’s functions.php file.