Solution for preventing indexing of content types?
Rather than coding a custom solution, I would use a supported module. The answer I believe will help you: Restrict Drupal Search to few content types

So for Drupal 7 you can use Custom Search, Search Config or Search API Exclude if you are using Search API. For Drupal 8 you can use Search Exclude.