{"record":{"id":"9c90897f562b2cd2","repo":"redis/jedis","slug":"reduce-collect-cannot-mix-fields-with-explicit-f","errorCode":null,"errorMessage":"REDUCE COLLECT cannot mix FIELDS * with explicit field names","messagePattern":"REDUCE COLLECT cannot mix FIELDS \\* with explicit field names","errorType":"validation","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/redis/clients/jedis/search/aggr/CollectReducer.java","lineNumber":76,"sourceCode":"  private boolean allFields = false;\n  private final List<String> fields = new ArrayList<>();\n  private final List<SortedField> sortFields = new ArrayList<>();\n  private Integer limitOffset;\n  private Integer limitCount;\n\n  CollectReducer() {\n    super(\"COLLECT\");\n  }\n\n  /**\n   * Project the named fields for every document in the group. Use {@code @__key}, {@code @__score}\n   * or document field names (e.g. {@code @title}).\n   * <p>\n   * Mutually exclusive with {@link #fieldsAll()}.\n   */\n  public CollectReducer fields(String... fields) {\n    if (this.allFields) {\n      throw new IllegalStateException(\n          \"REDUCE COLLECT cannot mix FIELDS * with explicit field names\");\n    }\n    Collections.addAll(this.fields, fields);\n    return this;\n  }\n\n  /**\n   * Project every field available in the current input row ({@code FIELDS *}).\n   * <p>\n   * Per the COLLECT specification, {@code *} does not trigger an implicit load — fields must\n   * already be in the pipeline (typically via {@code LOAD *} or because they are grouping keys /\n   * reducer aliases).\n   * <p>\n   * Mutually exclusive with {@link #fields(String...)}.\n   */\n  public CollectReducer fieldsAll() {\n    if (!this.fields.isEmpty()) {\n      throw new IllegalStateException(","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/redis/jedis/blob/6dac31d4c224fb3257c216f3985340c6f500cdcb/src/main/java/redis/clients/jedis/search/aggr/CollectReducer.java#L58-L94","documentation":"CollectReducer's REDUCE COLLECT lets you collect either an explicit list of fields or all fields (FIELDS *), but not both. Calling fields(String...) after fieldsAll() was already set throws this IllegalStateException because the two configurations are mutually exclusive in the generated aggregate query.","triggerScenarios":"Calling reducer.fieldsAll() followed by reducer.fields(\"@a\",\"@b\") on the same CollectReducer instance (builder-chain or fluent reuse).","commonSituations":"Building reducers dynamically and appending field names after defaulting to all-fields; copy-pasting reducer configuration where fieldsAll() appears earlier in the chain; reusing a shared reducer builder for multiple queries.","solutions":["Choose one mode: call fields(...) with explicit names OR fieldsAll(), never both on the same reducer","If a default of all-fields exists, clear/reset the reducer (build a new CollectReducer) before adding explicit fields","Refactor dynamic builders so the mode decision happens once before adding field names"],"exampleFix":"// before\nCollectReducer r = new CollectReducer().fieldsAll().fields(\"@title\"); // IllegalStateException\n// after\nCollectReducer r = new CollectReducer().fields(\"@title\", \"@body\");\n// or\nCollectReducer r = new CollectReducer().fieldsAll();","handlingStrategy":"validation","validationCode":"if (useAllFields && explicitFields.length > 0) {\n  throw new IllegalStateException(\"Choose either fieldsAll() or fields(...), not both\");\n}","typeGuard":"null","tryCatchPattern":"try {\n  reducer.fields(\"@title\");\n} catch (IllegalStateException e) {\n  // fieldsAll() already set — rebuild the reducer in a single mode\n}","preventionTips":["Decide the fields mode before building the reducer; use one branch, never both","Construct a fresh CollectReducer when requirements change instead of mutating a shared one","Keep builder chains linear and single-purpose","Add unit tests for dynamic reducer construction covering both modes"],"tags":["jedis","search","aggregation","reducer","validation"],"backgroundTag":"mutually-exclusive-options","analyzedSha":"6dac31d4c224fb3257c216f3985340c6f500cdcb","analyzedAt":"2026-09-08T04:55:01.204Z","contentChangedAt":"2026-09-08T04:55:01.204Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}