{
  "id": "ciq-21",
  "title": "ContX IQ: How do I aggregate data with WHERE, WITH and COUNT?",
  "description": "Demonstrates advanced Cypher query patterns including WHERE filtering, WITH for intermediate results, and COUNT for aggregation. Returns Person data with contract counts.",
  "category": "ContX IQ",
  "tags": [
    "ContX IQ Policy",
    "ContX IQ Query",
    "ContX IQ Execution",
    "Aggregation",
    "COUNT",
    "WHERE",
    "WITH"
  ],
  "api": "ContX IQ",
  "language": "Json",
  "lastUpdated": "2026-03-20",
  "openApiEndpoints": [
    "/capture/v1/nodes",
    "/capture/v1/relationships",
    "/configs/v1/authorization-policies",
    "/configs/v1/knowledge-queries",
    "/contx-iq/v1/execute"
  ],
  "relatedGuides": [
    "/guides/guide-contx-iq",
    "/guides/guide-sandbox"
  ],
  "commonErrors": [
    {
      "code": "401",
      "message": "UNAUTHENTICATED",
      "solution": "Check credentials: ServiceAccount Bearer token for config APIs, X-IK-ClientKey for execution"
    },
    {
      "code": "404",
      "message": "NOT_FOUND",
      "solution": "Verify the query/policy ID exists and belongs to your project"
    }
  ],
  "summary": "This example demonstrates aggregation and filtering in ContX IQ queries:\n\nCypher operators demonstrated:\n1. WHERE - produced by the policy filter (numContracts > 1) on the aggregated value\n2. WITH - Pass results between query parts (intermediate projection)\n3. COUNT - Aggregate and count related nodes\n\nPolicy cypher:\nMATCH (subject:Person)-[r1:ACCEPTED]->(contract:Contract)\nWITH subject, COUNT(contract) AS numContracts\nPolicy filter: subject.external_id = $subjectId AND numContracts > 1\nAllowed reads: subject.*, aggregate_values numContracts\n\nUse cases:\n- Dashboard summaries (count related entities)\n- Filtered aggregations (count only matching items)\n- Multi-step queries with intermediate results",
  "usecase": "Scenario: Return a Person together with how many Contracts they accepted, but only when they accepted more than one.\n\nGraph structure:\nPerson(alice) -[ACCEPTED]-> Contract(ct123)\nPerson(alice) -[ACCEPTED]-> Contract(ct234)\nPerson(alice) -[ACCEPTED]-> Contract(ct985)\nPerson(ryan) and Person(tilda) exist but accepted no contract in this dataset\n\nQuery flow:\n1. User authenticates with access token (Person subject)\n2. Execute with input_params subjectId = alice\n3. The policy cypher matches the subject's contracts and aggregates COUNT(contract) AS numContracts with WITH\n4. The policy filter keeps the row only if numContracts > 1\n5. The query returns subject.external_id and the aggregate value numContracts\n6. Returns: [{subject.external_id: \"alice\", numContracts: 3}]\n\nKey concepts:\n- WITH acts as a \"checkpoint\" in the query, projecting results forward\n- COUNT aggregates across relationships\n- Filtering happens before or after WITH as needed",
  "requirements": "Prerequisites:\n- ServiceAccount credentials: For creating policies and queries (Bearer token)\n- AppAgent credentials: For data ingestion and query execution (X-IK-ClientKey)\n- User access token: JWT for the authenticated user\n\nRequired API access:\n- POST /capture/v1/nodes/ and /capture/v1/relationships/ (data ingestion)\n- POST /configs/v1/authorization-policies (create policy)\n- POST /configs/v1/knowledge-queries (create query)\n- POST /contx-iq/v1/execute (run query)",
  "steps": "Step 1: Ingest Graph Data\n- Authentication: AppAgent credential (X-IK-ClientKey header)\n- Action: POST the contracts dataset (Person, PaymentMethod, Contract, Vehicle, LicenseNumber, Company, Application nodes); alice accepts ct123, ct234 and ct985\n- Result: Graph with a person who accepted several contracts\n\nStep 2: Create Aggregation Policy\n- Authentication: ServiceAccount credential (Bearer token)\n- Action: POST policy with subject type Person and cypher MATCH (subject:Person)-[r1:ACCEPTED]->(contract:Contract) WITH subject, COUNT(contract) AS numContracts, filter subject.external_id = $subjectId AND numContracts > 1, allowing:\n  - READ on subject.*\n  - aggregate_values numContracts\n- Result: Policy ID returned\n\nStep 3: Create Aggregation Query\n- Authentication: ServiceAccount credential (Bearer token)\n- Action: POST query with nodes [\"subject.external_id\"] and aggregate_values [\"numContracts\"]\n- Result: Query ID returned\n\nStep 4: Execute Aggregation Query\n- Authentication: AppAgent credential + User token (Bearer header)\n- Action: POST to /contx-iq/v1/execute with input_params subjectId = alice\n- Result: one row with subject.external_id alice and aggregate_values numContracts = 3",
  "graph": {
    "caption": "The graph the example runs on: 18 nodes (3 Person, 2 PaymentMethod, 3 Contract, 3 Vehicle, 3 LicenseNumber, 2 Company, 2 Application) and 17 relationships (HAS, ACCEPTED, COVERS, OWNS, HAS_AGREEMENT_WITH). Hover a node for its properties, click it to highlight its neighbourhood.",
    "nodes": [
      {
        "external_id": "alice",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "alice@email.com"
          },
          {
            "type": "given_name",
            "value": "Alice"
          },
          {
            "type": "last_name",
            "value": "Smith"
          }
        ]
      },
      {
        "external_id": "ryan",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "ryan@yahoo.co.uk"
          },
          {
            "type": "given_name",
            "value": "ryan"
          },
          {
            "type": "last_name",
            "value": "mushu"
          }
        ]
      },
      {
        "external_id": "tilda",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "tilda@yahoo.co.uk"
          },
          {
            "type": "given_name",
            "value": "tilda"
          },
          {
            "type": "last_name",
            "value": "mushu"
          }
        ]
      },
      {
        "external_id": "cb123",
        "type": "PaymentMethod",
        "is_identity": false,
        "properties": [
          {
            "type": "payment_name",
            "value": "Credit Card"
          }
        ]
      },
      {
        "external_id": "kl123",
        "type": "PaymentMethod",
        "is_identity": false,
        "properties": [
          {
            "type": "payment_name",
            "value": "Klarna"
          }
        ]
      },
      {
        "external_id": "ct123",
        "type": "Contract",
        "is_identity": false,
        "properties": [
          {
            "type": "category",
            "value": "Insurance"
          },
          {
            "type": "status",
            "value": "Active"
          },
          {
            "type": "number",
            "value": "hfgrten123"
          }
        ]
      },
      {
        "external_id": "ct234",
        "type": "Contract",
        "is_identity": false,
        "properties": [
          {
            "type": "category",
            "value": "Insurance"
          },
          {
            "type": "status",
            "value": "Active"
          },
          {
            "type": "number",
            "value": "hfgrten234"
          }
        ]
      },
      {
        "external_id": "ct985",
        "type": "Contract",
        "is_identity": false,
        "properties": [
          {
            "type": "category",
            "value": "Insurance"
          },
          {
            "type": "status",
            "value": "Active"
          },
          {
            "type": "number",
            "value": "hfgrten985"
          }
        ]
      },
      {
        "external_id": "car1",
        "type": "Vehicle",
        "is_identity": false,
        "properties": [
          {
            "type": "category",
            "value": "Car"
          },
          {
            "type": "is_active",
            "value": true
          },
          {
            "type": "vin",
            "value": "rtfhcnvjt471"
          }
        ]
      },
      {
        "external_id": "car2",
        "type": "Vehicle",
        "is_identity": false,
        "properties": [
          {
            "type": "category",
            "value": "Car"
          },
          {
            "type": "is_active",
            "value": true
          },
          {
            "type": "vin",
            "value": "kdcbfrt178"
          }
        ]
      },
      {
        "external_id": "truck1",
        "type": "Vehicle",
        "is_identity": false,
        "properties": [
          {
            "type": "category",
            "value": "Truck"
          },
          {
            "type": "is_active",
            "value": true
          },
          {
            "type": "vin",
            "value": "sncnrkcldp"
          }
        ]
      },
      {
        "external_id": "license1",
        "type": "LicenseNumber",
        "is_identity": false,
        "properties": [
          {
            "type": "status",
            "value": "Active"
          },
          {
            "type": "number",
            "value": "AX123456"
          }
        ]
      },
      {
        "external_id": "license2",
        "type": "LicenseNumber",
        "is_identity": false,
        "properties": [
          {
            "type": "status",
            "value": "Active"
          },
          {
            "type": "number",
            "value": "OL123456"
          }
        ]
      },
      {
        "external_id": "license3",
        "type": "LicenseNumber",
        "is_identity": false,
        "properties": [
          {
            "type": "status",
            "value": "Active"
          },
          {
            "type": "number",
            "value": "VN123456"
          }
        ]
      },
      {
        "external_id": "company1",
        "type": "Company",
        "is_identity": false,
        "properties": [
          {
            "type": "name",
            "value": "Company1"
          },
          {
            "type": "registration",
            "value": "256314523"
          }
        ]
      },
      {
        "external_id": "company2",
        "type": "Company",
        "is_identity": false,
        "properties": [
          {
            "type": "name",
            "value": "Company2"
          },
          {
            "type": "registration",
            "value": "942365123"
          }
        ]
      },
      {
        "external_id": "application1",
        "type": "Application",
        "is_identity": false,
        "properties": [
          {
            "type": "name",
            "value": "Application"
          }
        ]
      },
      {
        "external_id": "application2",
        "type": "Application",
        "is_identity": false,
        "properties": [
          {
            "type": "name",
            "value": "Application2"
          }
        ]
      }
    ],
    "relationships": [
      {
        "source": "ryan",
        "target": "cb123",
        "type": "HAS",
        "properties": []
      },
      {
        "source": "tilda",
        "target": "kl123",
        "type": "HAS",
        "properties": []
      },
      {
        "source": "alice",
        "target": "cb123",
        "type": "HAS",
        "properties": []
      },
      {
        "source": "alice",
        "target": "ct123",
        "type": "ACCEPTED",
        "properties": []
      },
      {
        "source": "alice",
        "target": "ct234",
        "type": "ACCEPTED",
        "properties": []
      },
      {
        "source": "alice",
        "target": "ct985",
        "type": "ACCEPTED",
        "properties": []
      },
      {
        "source": "ct123",
        "target": "car1",
        "type": "COVERS",
        "properties": []
      },
      {
        "source": "ct985",
        "target": "car1",
        "type": "COVERS",
        "properties": []
      },
      {
        "source": "ct234",
        "target": "truck1",
        "type": "COVERS",
        "properties": []
      },
      {
        "source": "car1",
        "target": "license1",
        "type": "HAS",
        "properties": []
      },
      {
        "source": "truck1",
        "target": "license2",
        "type": "HAS",
        "properties": []
      },
      {
        "source": "car2",
        "target": "license3",
        "type": "HAS",
        "properties": []
      },
      {
        "source": "company1",
        "target": "car1",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "company1",
        "target": "car2",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "company1",
        "target": "truck1",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "application1",
        "target": "company1",
        "type": "HAS_AGREEMENT_WITH",
        "properties": []
      },
      {
        "source": "application2",
        "target": "company1",
        "type": "HAS_AGREEMENT_WITH",
        "properties": []
      }
    ]
  },
  "code": [
    {
      "step": "Step 1",
      "description": "Capture the nodes needed for this use case.",
      "filename": "POST https://eu.api.indykite.com/capture/v1/nodes/",
      "language": "Json",
      "ingest": {
        "nodes": [
          {
            "external_id": "alice",
            "is_identity": true,
            "type": "Person",
            "properties": [
              {
                "type": "email",
                "value": "alice@email.com"
              },
              {
                "type": "given_name",
                "value": "Alice"
              },
              {
                "type": "last_name",
                "value": "Smith"
              }
            ]
          },
          {
            "external_id": "ryan",
            "is_identity": true,
            "type": "Person",
            "properties": [
              {
                "type": "email",
                "value": "ryan@yahoo.co.uk"
              },
              {
                "type": "given_name",
                "value": "ryan"
              },
              {
                "type": "last_name",
                "value": "mushu"
              }
            ]
          },
          {
            "external_id": "tilda",
            "is_identity": true,
            "type": "Person",
            "properties": [
              {
                "type": "email",
                "value": "tilda@yahoo.co.uk"
              },
              {
                "type": "given_name",
                "value": "tilda"
              },
              {
                "type": "last_name",
                "value": "mushu"
              }
            ]
          },
          {
            "external_id": "cb123",
            "type": "PaymentMethod",
            "properties": [
              {
                "type": "payment_name",
                "value": "Credit Card"
              }
            ]
          },
          {
            "external_id": "kl123",
            "type": "PaymentMethod",
            "properties": [
              {
                "type": "payment_name",
                "value": "Klarna"
              }
            ]
          },
          {
            "external_id": "ct123",
            "type": "Contract",
            "properties": [
              {
                "type": "category",
                "value": "Insurance"
              },
              {
                "type": "status",
                "value": "Active"
              },
              {
                "type": "number",
                "value": "hfgrten123",
                "metadata": {
                  "assurance_level": 3,
                  "source": "BRREG"
                }
              }
            ]
          },
          {
            "external_id": "ct234",
            "type": "Contract",
            "properties": [
              {
                "type": "category",
                "value": "Insurance"
              },
              {
                "type": "status",
                "value": "Active"
              },
              {
                "type": "number",
                "value": "hfgrten234",
                "metadata": {
                  "assurance_level": 3,
                  "source": "BRREG"
                }
              }
            ]
          },
          {
            "external_id": "ct985",
            "type": "Contract",
            "properties": [
              {
                "type": "category",
                "value": "Insurance"
              },
              {
                "type": "status",
                "value": "Active"
              },
              {
                "type": "number",
                "value": "hfgrten985",
                "metadata": {
                  "assurance_level": 3,
                  "source": "BRREG"
                }
              }
            ]
          },
          {
            "external_id": "car1",
            "type": "Vehicle",
            "properties": [
              {
                "type": "category",
                "value": "Car"
              },
              {
                "type": "is_active",
                "value": true
              },
              {
                "type": "vin",
                "value": "rtfhcnvjt471"
              }
            ]
          },
          {
            "external_id": "car2",
            "type": "Vehicle",
            "properties": [
              {
                "type": "category",
                "value": "Car"
              },
              {
                "type": "is_active",
                "value": true
              },
              {
                "type": "vin",
                "value": "kdcbfrt178"
              }
            ]
          },
          {
            "external_id": "truck1",
            "type": "Vehicle",
            "properties": [
              {
                "type": "category",
                "value": "Truck"
              },
              {
                "type": "is_active",
                "value": true
              },
              {
                "type": "vin",
                "value": "sncnrkcldp"
              }
            ]
          },
          {
            "external_id": "license1",
            "type": "LicenseNumber",
            "properties": [
              {
                "type": "status",
                "value": "Active"
              },
              {
                "type": "number",
                "value": "AX123456",
                "metadata": {
                  "assurance_level": 3,
                  "source": "BRREG"
                }
              }
            ]
          },
          {
            "external_id": "license2",
            "type": "LicenseNumber",
            "properties": [
              {
                "type": "status",
                "value": "Active"
              },
              {
                "type": "number",
                "value": "OL123456",
                "metadata": {
                  "assurance_level": 3,
                  "source": "BRREG"
                }
              }
            ]
          },
          {
            "external_id": "license3",
            "type": "LicenseNumber",
            "properties": [
              {
                "type": "status",
                "value": "Active"
              },
              {
                "type": "number",
                "value": "VN123456",
                "metadata": {
                  "assurance_level": 3,
                  "source": "BRREG"
                }
              }
            ]
          },
          {
            "external_id": "company1",
            "type": "Company",
            "properties": [
              {
                "type": "name",
                "value": "Company1"
              },
              {
                "type": "registration",
                "value": "256314523"
              }
            ]
          },
          {
            "external_id": "company2",
            "type": "Company",
            "properties": [
              {
                "type": "name",
                "value": "Company2"
              },
              {
                "type": "registration",
                "value": "942365123"
              }
            ]
          },
          {
            "external_id": "application1",
            "type": "Application",
            "properties": [
              {
                "type": "name",
                "value": "Application"
              }
            ]
          },
          {
            "external_id": "application2",
            "type": "Application",
            "properties": [
              {
                "type": "name",
                "value": "Application2"
              }
            ]
          }
        ]
      }
    },
    {
      "description": "Capture the relationships needed for this use case.",
      "filename": "POST https://eu.api.indykite.com/capture/v1/relationships/",
      "language": "Json",
      "ingest": {
        "relationships": [
          {
            "source": {
              "external_id": "ryan",
              "type": "Person"
            },
            "target": {
              "external_id": "cb123",
              "type": "PaymentMethod"
            },
            "type": "HAS"
          },
          {
            "source": {
              "external_id": "tilda",
              "type": "Person"
            },
            "target": {
              "external_id": "kl123",
              "type": "PaymentMethod"
            },
            "type": "HAS"
          },
          {
            "source": {
              "external_id": "alice",
              "type": "Person"
            },
            "target": {
              "external_id": "cb123",
              "type": "PaymentMethod"
            },
            "type": "HAS"
          },
          {
            "source": {
              "external_id": "alice",
              "type": "Person"
            },
            "target": {
              "external_id": "ct123",
              "type": "Contract"
            },
            "type": "ACCEPTED"
          },
          {
            "source": {
              "external_id": "alice",
              "type": "Person"
            },
            "target": {
              "external_id": "ct234",
              "type": "Contract"
            },
            "type": "ACCEPTED"
          },
          {
            "source": {
              "external_id": "alice",
              "type": "Person"
            },
            "target": {
              "external_id": "ct985",
              "type": "Contract"
            },
            "type": "ACCEPTED"
          },
          {
            "source": {
              "external_id": "ct123",
              "type": "Contract"
            },
            "target": {
              "external_id": "car1",
              "type": "Vehicle"
            },
            "type": "COVERS"
          },
          {
            "source": {
              "external_id": "ct985",
              "type": "Contract"
            },
            "target": {
              "external_id": "car1",
              "type": "Vehicle"
            },
            "type": "COVERS"
          },
          {
            "source": {
              "external_id": "ct234",
              "type": "Contract"
            },
            "target": {
              "external_id": "truck1",
              "type": "Vehicle"
            },
            "type": "COVERS"
          },
          {
            "source": {
              "external_id": "car1",
              "type": "Vehicle"
            },
            "target": {
              "external_id": "license1",
              "type": "LicenseNumber"
            },
            "type": "HAS"
          },
          {
            "source": {
              "external_id": "truck1",
              "type": "Vehicle"
            },
            "target": {
              "external_id": "license2",
              "type": "LicenseNumber"
            },
            "type": "HAS"
          },
          {
            "source": {
              "external_id": "car2",
              "type": "Vehicle"
            },
            "target": {
              "external_id": "license3",
              "type": "LicenseNumber"
            },
            "type": "HAS"
          },
          {
            "source": {
              "external_id": "company1",
              "type": "Company"
            },
            "target": {
              "external_id": "car1",
              "type": "Vehicle"
            },
            "type": "OWNS"
          },
          {
            "source": {
              "external_id": "company1",
              "type": "Company"
            },
            "target": {
              "external_id": "car2",
              "type": "Vehicle"
            },
            "type": "OWNS"
          },
          {
            "source": {
              "external_id": "company1",
              "type": "Company"
            },
            "target": {
              "external_id": "truck1",
              "type": "Vehicle"
            },
            "type": "OWNS"
          },
          {
            "source": {
              "external_id": "application1",
              "type": "Application"
            },
            "target": {
              "external_id": "company1",
              "type": "Company"
            },
            "type": "HAS_AGREEMENT_WITH"
          },
          {
            "source": {
              "external_id": "application2",
              "type": "Application"
            },
            "target": {
              "external_id": "company1",
              "type": "Company"
            },
            "type": "HAS_AGREEMENT_WITH"
          }
        ]
      }
    },
    {
      "step": "Step 2",
      "description": "Create a CIQ Policy which designates the Subject node, the cypher, and the nodes allowed to be read. ",
      "filename": "policy.json",
      "language": "Json",
      "ingest": {
        "meta": {
          "policy_version": "1.0-ciq"
        },
        "subject": {
          "type": "Person"
        },
        "condition": {
          "cypher": "MATCH (subject:Person)-[r1:ACCEPTED]->(contract:Contract) WITH subject, COUNT(contract) AS numContracts",
          "filter": [
            {
              "app": "app1",
              "operator": "AND",
              "operands": [
                {
                  "attribute": "subject.external_id",
                  "operator": "=",
                  "value": "$subjectId"
                },
                {
                  "attribute": "numContracts",
                  "operator": ">",
                  "value": 1
                }
              ]
            }
          ]
        },
        "allowed_reads": {
          "nodes": [
            "subject.*"
          ],
          "aggregate_values": [
            "numContracts"
          ]
        }
      }
    },
    {
      "description": "Request to create the CIQ Policy configuration using REST.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/authorization-policies",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "description": "description of policy",
        "display_name": "policy name",
        "name": "policy-name",
        "policy": "{\"meta\":{\"policy_version\":\"1.0-ciq\"},\"subject\":{\"type\":\"Person\"},\"condition\":{\"cypher\":\"MATCH (subject:Person)-[r1:ACCEPTED]->(contract:Contract) WITH subject, COUNT(contract) AS numContracts\",\"filter\":[{\"app\":\"app1\",\"operator\":\"AND\",\"operands\":[{\"attribute\":\"subject.external_id\",\"operator\":\"=\",\"value\":\"$subjectId\"},{\"attribute\":\"numContracts\",\"operator\":\">\",\"value\":1}]}]},\"allowed_reads\":{\"nodes\":[\"subject.*\"],\"aggregate_values\":[\"numContracts\"]}}",
        "status": "ACTIVE",
        "tags": []
      }
    },
    {
      "description": "Request to read the CIQ Policy configuration using REST.",
      "filename": "policy_request.json",
      "language": "Json",
      "ingest": {
        "id": "your_policy_configuration_gid"
      }
    },
    {
      "step": "Step 3",
      "description": "Create a CIQ Query in the context of the policy to retrieve the designated properties. ",
      "filename": "knowledge_query.json",
      "language": "Json",
      "ingest": {
        "nodes": [
          "subject.external_id"
        ],
        "aggregate_values": [
          "numContracts"
        ]
      }
    },
    {
      "description": "Request to create a CIQ Query configuration using REST.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/knowledge-queries",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "description": "description of knowledge query",
        "display_name": "knowledge query name",
        "name": "knowledge-query-name",
        "policy_id": "your_policy_gid",
        "query": "{\"nodes\":[\"subject.external_id\"],\"aggregate_values\":[\"numContracts\"]}",
        "status": "ACTIVE"
      }
    },
    {
      "description": "Read the CIQ Query Configuration.",
      "filename": "GET https://eu.api.indykite.com/configs/v1/knowledge-queries/{id}",
      "language": "Json",
      "ingest": {
        "id": "your_knowledge_query_configuration_gid"
      }
    },
    {
      "step": "Step 4",
      "description": "Run a CIQ Execution to get the designated information. ",
      "filename": "POST https://eu.api.indykite.com/contx-iq/v1/execute",
      "language": "Json",
      "ingest": {
        "id": "knowledge_query_gid",
        "input_params": {
          "subjectId": "alice"
        },
        "page_token": 1
      }
    },
    {
      "description": "CIQ Execution response.",
      "filename": "response.json",
      "language": "Json",
      "ingest": {
        "data": [
          {
            "nodes": {
              "subject.external_id": "alice"
            },
            "aggregate_values": {
              "numContracts": 3
            }
          }
        ]
      }
    }
  ],
  "links": {
    "html": "/resources/ciq-21",
    "markdown": "/resources/ciq-21.md",
    "openApi": "https://openapi.indykite.com/"
  }
}