Overpass searching for Nodes or Ways

I’m trying to create a function that searches for a set of nodes or ways within a certain radius, and I want to know if there’s a more efficient method of querying these without trying all combinations of ways and nodes. e.g., WN NN NW WW.

database indexes or more generally indexing/caching in various forms is a typical solution