How to search for users not registered to a specific application?
-
I need to identify users in FusionAuth who are not registered to a particular application. The User Search API supports querying for users with registrations, but how can I construct a query to find users that don't have a registration for a given application ID?
Specifically, I want to use the Elasticsearch query syntax to exclude users who have a registration matching a certain
applicationId. -
You can use the User Search API with a
must_notclause in the Elasticsearch query to exclude users registered to a specific application. Here's an example:curl -X POST https://sandbox.fusionauth.io/api/user/search \ -H 'Authorization: <your-api-key>' \ -H 'Content-Type: application/json' \ -d '{ "search": { "accurateTotal": true, "numberOfResults": 100, "startRow": 0, "query": "{\"bool\":{\"must_not\":[{\"nested\":{\"path\":\"registrations\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"registrations.applicationId\":\"<your-application-id>\"}}]}}}}]}}" } }'Key points:
- The
must_notclause excludes users matching the nested query - The
nestedquery targets theregistrationspath since registrations are nested objects in the Elasticsearch mapping - Inside the nested query, the
mustclause matches the specificapplicationId - Replace
<your-application-id>with the UUID of your target application - This returns users who either have no registrations at all, or have registrations to other applications but not the specified one
You can paginate through results using
startRowandnumberOfResultsif you have a large user base.Important: This approach requires the Elasticsearch search engine (not the database engine). You must use the
search.queryparameter with raw Elasticsearch JSON when querying againstregistrations, because it is defined as a nested datatype in the Elasticsearch mapping. ThequeryStringparameter cannot be used for nested fields likeregistrationswith expected results.Tip: You can use the Admin UI → Users → Advanced → Show Elasticsearch Query toggle to build and test queries interactively before using them in the API.
Related Documentation
- User Search API - Elasticsearch Search Engine - Main API reference for searching users with Elasticsearch
- Searching Users With Elasticsearch or OpenSearch - Searching With
query- Guide on using thequeryparameter for advanced searches - User Search API - Additional Query Examples - Examples of various query patterns including nested queries
- Registrations - Core concepts documentation explaining the relationship between Users, Applications, and Registrations
- Field Mappings - Available fields for matching in Elasticsearch searches
- The
-
D dan has marked this topic as solved on
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login