How to use CQLSearchService with permissions?

I am having a JobRunner that tries to use CQL using the CQLSearchService instance:

int count = this.getSearchService().countContent(cqlQuery);

However, this job fails to run every single time, as the SearchService seems to lack permissions:
PermissionException: Not permitted to use confluence: null

How can I give permission to that service? Is there any other way to count certain content elements like macros without having to have certain permissions?

Thank you.

Dear Philip,

I don’t know whether this is the only way but in our apps we are using AuthenticatedUserThreadLocal.set to execute the search as a user (e.g. an one of the admins).

Kind regards

Andreas

Hey Andreas,
thanks for your response. Since this is happening in a JobRunner and there is no requesting user,
what are you setting the AuthenticatedUserThreadLocal to?

Dear Philip,

in our app we are using the first admin we find.

Kind regards

Andreas