I am performing spotlight like search using NSMetadataQuery, the problem i am facing is that i am not able to restrict NSMetadataQuery from searching the particular hidden folder like /Users/username/Library (as library is hidden folder).
Any help would be appreciated.
The issue is that the index created by Spotlight is not designed to restrict by paths. The index is optimized to quickly find the terms, and then you can get the corresponding URL for the result (or other metadata). The index is not optimized to search based on the path.
The search index (built using SearchKit) is anyway very much optimized, and results are returned very fast even with large number of files. One you get the results, you have to filter these results yourself.
Note that most files in ~/Library won't be included in the index, like caches, plist files, plugins, etc...