{"record":{"id":"1b40590dfdb28fea","repo":"elastic/elasticsearch","slug":"project-routing-already-set","errorCode":null,"errorMessage":"project_routing already set","messagePattern":"project_routing already set","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateRequest.java","lineNumber":170,"sourceCode":"        for (SearchTemplateRequest templateRequest : multiSearchTemplateRequest.requests()) {\n            final SearchRequest searchRequest = templateRequest.getRequest();\n            try (XContentBuilder xContentBuilder = XContentBuilder.builder(xContent)) {\n                MultiSearchRequest.writeSearchRequestParams(searchRequest, xContentBuilder);\n                BytesReference.bytes(xContentBuilder).writeTo(output);\n            }\n            output.write(xContent.bulkSeparator());\n            try (XContentBuilder xContentBuilder = XContentBuilder.builder(xContent)) {\n                templateRequest.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);\n                BytesReference.bytes(xContentBuilder).writeTo(output);\n            }\n            output.write(xContent.bulkSeparator());\n        }\n        return output.toByteArray();\n    }\n\n    public void setProjectRouting(@Nullable String projectRouting) {\n        if (this.projectRouting != null) {\n            throw new IllegalArgumentException(\"project_routing already set\");\n        }\n\n        this.projectRouting = projectRouting;\n    }\n\n    @Nullable\n    public String getProjectRouting() {\n        return projectRouting;\n    }\n\n}\n","sourceCodeStart":152,"sourceCodeEnd":182,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/MultiSearchTemplateRequest.java#L152-L182","documentation":"Thrown by MultiSearchTemplateRequest.setProjectRouting() when the method is called more than once. The project_routing field is initialized to null and setProjectRouting checks if it is already non-null before assigning. This prevents accidental double-setting, which could occur if project_routing is specified both as a URL query parameter and elsewhere in the request processing pipeline.","triggerScenarios":"Calling setProjectRouting() twice on the same MultiSearchTemplateRequest instance. In practice, this can happen when the RestMultiSearchTemplateAction.parseRequest() method sets project_routing from the rest request parameter (line 81) and then another code path attempts to set it again. This is more likely in cross-project (Serverless) mode where project_routing handling is active.","commonSituations":"Cross-project search is enabled and project_routing is set from multiple sources (e.g., both URL param and internal pipeline). A bug in request processing that calls setProjectRouting twice. Using the API programmatically and accidentally calling the setter more than once.","solutions":["Set project_routing only once per request — typically via the URL query parameter ?project_routing=<value>","Check if project_routing is already set before calling setProjectRouting: if (request.getProjectRouting() == null) request.setProjectRouting(value)","If using cross-project mode, ensure only one code path sets the routing","Review client code to avoid duplicate setter calls"],"exampleFix":"// before — double set:\nrequest.setProjectRouting(\"project-a\");\nrequest.setProjectRouting(\"project-b\"); // throws\n\n// after — guard or set once:\nif (request.getProjectRouting() == null) {\n    request.setProjectRouting(\"project-a\");\n}","handlingStrategy":"validation","validationCode":"// Guard against double-setting project_routing\npublic void safeSetProjectRouting(MultiSearchTemplateRequest req, String routing) {\n    if (req.getProjectRouting() != null) {\n        throw new IllegalStateException(\"project_routing already set to: \" + req.getProjectRouting());\n    }\n    req.setProjectRouting(routing);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set project_routing exactly once per request, ideally via URL parameter","Check getProjectRouting() == null before calling setProjectRouting()","Ensure only one code path handles project routing in your pipeline"],"tags":["elasticsearch","lang-mustache","project-routing","cross-project","validation"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}