FusionAuth
    • Home
    • Categories
    • Recent
    • Popular
    • Pricing
    • Contact us
    • Docs
    • Login

    How to search for users not registered to a specific application?

    Scheduled Pinned Locked Moved Solved
    Frequently Asked Questions (FAQ)
    user-search api elasticsearch registrations query
    1
    2
    18
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F
      FASupportBot
      last edited by

      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.

      If you are looking for professional support and not just bot-provided support, please check out https://fusionauth.io/pricing and pick a plan that fits your needs.

      1 Reply Last reply Reply Quote 0
      • F
        FASupportBot
        last edited by

        You can use the User Search API with a must_not clause 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_not clause excludes users matching the nested query
        • The nested query targets the registrations path since registrations are nested objects in the Elasticsearch mapping
        • Inside the nested query, the must clause matches the specific applicationId
        • 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 startRow and numberOfResults if you have a large user base.

        Important: This approach requires the Elasticsearch search engine (not the database engine). You must use the search.query parameter with raw Elasticsearch JSON when querying against registrations, because it is defined as a nested datatype in the Elasticsearch mapping. The queryString parameter cannot be used for nested fields like registrations with 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 the query parameter 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

        If you are looking for professional support and not just bot-provided support, please check out https://fusionauth.io/pricing and pick a plan that fits your needs.

        1 Reply Last reply Reply Quote 0
        • danD dan has marked this topic as solved
        • danD dan moved this topic from Staged
        • First post
          Last post