Use ajax without a plugin?

Plugin code is just… code, it can go in a themes functions.php, or a plugin file, there’s no magical or special difference other than the order they’re loaded in, and the API calls are identical, work the same way etc because they’re the same. It’s just how the code is packaged

For example, if you ever want to do something without using a plugin, take a plugin, remove the comment at the top of the main file, and hey presto you’ve got your answer.

The problem you’re facing is that you’ve tried to put your logic inside the search page. But that’s not how the AJAX API works. When the browser makes a request to the AJAX endpoint, no templates are loaded, so your search template is never loaded, and your code to handle the request is never loaded.

Instead, the code needs to be in functions.php which is always loaded.

You can see this here:

add_action( 'init', 'my_script_enqueuer' );

Here, you’re saying “When the init event happens, run this function”, but if you’re in the search page template, then that event has already happened. Remember, templates are loaded after everything else, not before