{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "searchServiceName": {
      "type": "string",
      "metadata": {
        "description": "The name of the target Azure AI Search service."
      }
    },
    "actionGroupId": {
      "type": "string",
      "metadata": {
        "description": "Resource ID of the Azure Monitor Action Group for alert notifications."
      }
    },
    "latencyThresholdSec": {
      "type": "number",
      "defaultValue": 0.2,
      "metadata": {
        "description": "Average query latency threshold in seconds (team default: 0.2 s, i.e. 200 ms). SearchLatency is reported in seconds by Azure Monitor."
      }
    },
    "storageThresholdPercent": {
      "type": "int",
      "defaultValue": 80,
      "metadata": {
        "description": "Storage capacity utilization threshold percentage (team default: 80%)."
      }
    },
    "vectorQuotaThresholdPercent": {
      "type": "int",
      "defaultValue": 75,
      "metadata": {
        "description": "Vector index memory quota utilization threshold percentage (team default: 75%)."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Insights/metricAlerts",
      "apiVersion": "2018-03-01",
      "name": "[concat(parameters('searchServiceName'), '-alert-throttled-queries')]",
      "location": "global",
      "properties": {
        "description": "Alert when search queries are throttled (ThrottledSearchQueriesPercentage > 0%). Scale replicas immediately.",
        "severity": 1,
        "enabled": true,
        "scopes": [
          "[resourceId('Microsoft.Search/searchServices', parameters('searchServiceName'))]"
        ],
        "evaluationFrequency": "PT1M",
        "windowSize": "PT5M",
        "criteria": {
          "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria",
          "allOf": [
            {
              "name": "ThrottledQueriesCriterion",
              "metricName": "ThrottledSearchQueriesPercentage",
              "metricNamespace": "Microsoft.Search/searchServices",
              "operator": "GreaterThan",
              "threshold": 0,
              "timeAggregation": "Average",
              "criterionType": "StaticThresholdCriterion"
            }
          ]
        },
        "actions": [
          {
            "actionGroupId": "[parameters('actionGroupId')]"
          }
        ]
      }
    },
    {
      "type": "Microsoft.Insights/metricAlerts",
      "apiVersion": "2018-03-01",
      "name": "[concat(parameters('searchServiceName'), '-alert-high-latency')]",
      "location": "global",
      "properties": {
        "description": "Alert when average query latency (seconds) exceeds the agreed SLO baseline for 5 consecutive minutes.",
        "severity": 2,
        "enabled": true,
        "scopes": [
          "[resourceId('Microsoft.Search/searchServices', parameters('searchServiceName'))]"
        ],
        "evaluationFrequency": "PT1M",
        "windowSize": "PT5M",
        "criteria": {
          "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria",
          "allOf": [
            {
              "name": "SearchLatencyCriterion",
              "metricName": "SearchLatency",
              "metricNamespace": "Microsoft.Search/searchServices",
              "operator": "GreaterThan",
              "threshold": "[parameters('latencyThresholdSec')]",
              "timeAggregation": "Average",
              "criterionType": "StaticThresholdCriterion"
            }
          ]
        },
        "actions": [
          {
            "actionGroupId": "[parameters('actionGroupId')]"
          }
        ]
      }
    },
    {
      "type": "Microsoft.Insights/metricAlerts",
      "apiVersion": "2018-03-01",
      "name": "[concat(parameters('searchServiceName'), '-alert-storage-headroom')]",
      "location": "global",
      "properties": {
        "description": "Alert when search index storage exceeds 80% partition quota. Plan partition expansion before cut-over.",
        "severity": 2,
        "enabled": true,
        "scopes": [
          "[resourceId('Microsoft.Search/searchServices', parameters('searchServiceName'))]"
        ],
        "evaluationFrequency": "PT5M",
        "windowSize": "PT15M",
        "criteria": {
          "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria",
          "allOf": [
            {
              "name": "StorageUsageCriterion",
              "metricName": "IndexStorageUsage",
              "metricNamespace": "Microsoft.Search/searchServices",
              "operator": "GreaterThan",
              "threshold": "[parameters('storageThresholdPercent')]",
              "timeAggregation": "Maximum",
              "criterionType": "StaticThresholdCriterion"
            }
          ]
        },
        "actions": [
          {
            "actionGroupId": "[parameters('actionGroupId')]"
          }
        ]
      }
    },
    {
      "type": "Microsoft.Insights/metricAlerts",
      "apiVersion": "2018-03-01",
      "name": "[concat(parameters('searchServiceName'), '-alert-vector-quota')]",
      "location": "global",
      "properties": {
        "description": "Alert when vector index size exceeds 75% vector memory quota. Plan partition expansion or vector quantization.",
        "severity": 2,
        "enabled": true,
        "scopes": [
          "[resourceId('Microsoft.Search/searchServices', parameters('searchServiceName'))]"
        ],
        "evaluationFrequency": "PT5M",
        "windowSize": "PT15M",
        "criteria": {
          "odata.type": "Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria",
          "allOf": [
            {
              "name": "VectorUsageCriterion",
              "metricName": "IndexVectorUsage",
              "metricNamespace": "Microsoft.Search/searchServices",
              "operator": "GreaterThan",
              "threshold": "[parameters('vectorQuotaThresholdPercent')]",
              "timeAggregation": "Maximum",
              "criterionType": "StaticThresholdCriterion"
            }
          ]
        },
        "actions": [
          {
            "actionGroupId": "[parameters('actionGroupId')]"
          }
        ]
      }
    }
  ]
}
