If search matches taxonomy

I only wanted to match tags in a single taxonomy, so I was able to simplify the code as follows. My taxonomy is ‘post_tag’ — just swap yours out as needed.

$i = 0;
$search_query = get_search_query();

$term = get_term_by( 'name', $search_query, 'post_tag' );
if( $term !== false ) {
    $i++;
    $single_result = $term;
}    

if( $i == 1 && is_object( $single_result ) ) {
    $single_found = true;
} else {
$single_found = false;
unset( $single_result );
}

if( $single_found ) {
    wp_redirect( get_bloginfo( 'url' ) . "https://wordpress.stackexchange.com/" . $single_result->slug );
}