{"record":{"id":"2b06724b242bd342","repo":"elastic/elasticsearch","slug":"length-parameter-must-be-provided","errorCode":null,"errorMessage":"length parameter must be provided","messagePattern":"length parameter must be provided","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/TruncateTokenFilterFactory.java","lineNumber":27,"sourceCode":"\npackage org.elasticsearch.analysis.common;\n\nimport org.apache.lucene.analysis.TokenStream;\nimport org.apache.lucene.analysis.miscellaneous.TruncateTokenFilter;\nimport org.elasticsearch.common.settings.Settings;\nimport org.elasticsearch.env.Environment;\nimport org.elasticsearch.index.IndexSettings;\nimport org.elasticsearch.index.analysis.AbstractTokenFilterFactory;\n\npublic class TruncateTokenFilterFactory extends AbstractTokenFilterFactory {\n\n    private final int length;\n\n    TruncateTokenFilterFactory(IndexSettings indexSettings, Environment environment, String name, Settings settings) {\n        super(name);\n        this.length = settings.getAsInt(\"length\", -1);\n        if (length <= 0) {\n            throw new IllegalArgumentException(\"length parameter must be provided\");\n        }\n    }\n\n    @Override\n    public TokenStream create(TokenStream tokenStream) {\n        return new TruncateTokenFilter(tokenStream, length);\n    }\n}\n","sourceCodeStart":9,"sourceCodeEnd":36,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/TruncateTokenFilterFactory.java#L9-L36","documentation":"The truncate token filter requires a positive integer length. The constructor reads settings.getAsInt(\"length\", -1) and rejects any value <= 0. Because the default is -1, simply omitting the key is sufficient to trip the check, as is explicitly passing 0 or a negative number.","triggerScenarios":"Creating an analyzer whose filter is type 'truncate' without a 'length' key, or with length <= 0.","commonSituations":"Copying a truncate example that omitted length; mistyping the key as 'max' or 'size'; intending 'no truncation' and passing 0.","solutions":["Add \"length\": N with N >= 1 (it is the maximum token length in characters; tokens longer than N are truncated to N).","Verify the key is spelled exactly 'length' and lives inside the filter object.","If you want no truncation, remove the filter entirely instead of passing 0."],"exampleFix":"// before\n\"filter\": { \"my_trunc\": { \"type\": \"truncate\" } }\n// after\n\"filter\": { \"my_trunc\": { \"type\": \"truncate\", \"length\": 10 } }","handlingStrategy":"validation","validationCode":"// Require a positive length on every truncate filter\nstatic String checkTruncate(Map<String,Object> filter) {\n  if (\"truncate\".equals(filter.get(\"type\"))) {\n    Object len = filter.get(\"length\");\n    if (!(len instanceof Number) || ((Number) len).intValue() <= 0)\n      return \"truncate filter requires length > 0\";\n  }\n  return null;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat 'length' as required for any truncate filter in templates.","Lint analyzer definitions in CI for missing/invalid length values."],"tags":["analysis","configuration","validation"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}