elastic/elasticsearch · error · ParseException
expected word but found: {}
Error message
expected word but found: {} What it means
Thrown by WellKnownText.nextWord when the StreamTokenizer's next token is not TT_WORD and not one of the structural characters '(', ')', ','. This helper expects a keyword or a delimiter; anything else (EOF, EOL, a number, or an unexpected symbol) is rejected.
Source
Thrown at libs/geo/src/main/java/org/elasticsearch/geometry/utils/WellKnownText.java:697
return circle;
}
/**
* next word in the stream
*/
static String nextWord(StreamTokenizer stream) throws ParseException, IOException {
switch (stream.nextToken()) {
case StreamTokenizer.TT_WORD:
final String word = stream.sval;
return word.equalsIgnoreCase(EMPTY) ? EMPTY : word;
case '(':
return LPAREN;
case ')':
return RPAREN;
case ',':
return COMMA;
}
throw new ParseException("expected word but found: " + tokenString(stream), stream.lineno());
}
static double nextNumber(StreamTokenizer stream) throws IOException, ParseException {
if (stream.nextToken() == StreamTokenizer.TT_WORD) {
if (stream.sval.equalsIgnoreCase(NAN)) {
return Double.NaN;
} else {
try {
return Double.parseDouble(stream.sval);
} catch (NumberFormatException e) {
throw new ParseException("invalid number found: " + stream.sval, stream.lineno());
}
}
}
throw new ParseException("expected number but found: " + tokenString(stream), stream.lineno());
}
static String tokenString(StreamTokenizer stream) {View on GitHub (pinned to db6a809a66)
Solutions
- Inspect the full WKT string for truncation or stray characters around the position reported by stream.lineno().
- Ensure the string is complete: balanced parens, all keywords present.
- Re-tokenize from a clean StreamTokenizer if a prior parse failed mid-stream.
Example fix
// before: truncated WKT (keyword with no body)
Geometry g = WellKnownText.fromWKT("POINT");
// after: complete WKT
Geometry g = WellKnownText.fromWKT("POINT (1 2)"); Defensive patterns
Strategy: try-catch
Try / catch
try {
Geometry g = WellKnownText.fromWKT(wkt);
} catch (java.text.ParseException e) {
if (e.getMessage().startsWith("expected word")) {
// report the unexpected token and line number to the user
}
throw e;
} Prevention
- Ensure WKT strings are complete and not truncated.
- Remove stray characters before keywords and delimiters.
- Use a fresh tokenizer per parse.
When it happens
Trigger: Any WKT sub-parse that calls nextWord and encounters EOF (truncated string), a number where a keyword/delimiter is expected, or a stray symbol. For example, after reading a type keyword, nextEmptyOrOpen calls nextWord expecting EMPTY or '(' but hits EOF or a number.
Common situations: Truncated WKT string (missing the body); a stray character inserted by copy-paste; a tokenizer left mid-stream after a prior error; encoding issues producing unexpected bytes.
Related errors
- When specifying 'Z' or 'M', coordinates must include three v
- maximum nested depth of 1000 exceeded
- coordinate dimensions do not match: {}
- invalid number found: {}
- expected number but found: {}
AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12).
Data as JSON: /api/errors/6cf29da43b3b41a2.
Report an issue: GitHub.