Skip to content

Instantly share code, notes, and snippets.

@dot-mike
Created March 18, 2026 16:21
Show Gist options
  • Select an option

  • Save dot-mike/a4535df2aa20f2f7472d196d7ebdeb8c to your computer and use it in GitHub Desktop.

Select an option

Save dot-mike/a4535df2aa20f2f7472d196d7ebdeb8c to your computer and use it in GitHub Desktop.
Elasticsearch ingest pipeline split message KV list by space and then each by "=" taking into account quoted values
def splitOnceByToken(String input, String sep) {
def tokens = ["", ""];
def startPosition = 0;
def isInQuotes = false;
char quote = (char)"\"";
for (def currentPosition = 0; currentPosition < input.length(); currentPosition++) {
if (input.charAt(currentPosition) == quote) {
isInQuotes = !isInQuotes;
}
else if (input.charAt(currentPosition) == (char)sep && !isInQuotes) {
def token = input.substring(startPosition, currentPosition).trim();
if (!token.equals("")) {
tokens[0] = token;
}
startPosition = currentPosition + 1;
break;
}
}
def lastToken = input.substring(startPosition);
if (!lastToken.equals(sep) && !lastToken.equals("")) {
tokens[1] = lastToken.trim();
}
return tokens;
}
def splitUnquoted(String input, String sep) {
def tokens = [];
def cur = input;
def last = cur;
while (cur.length() > 0) {
def res = splitOnceByToken(cur, sep);
last = cur;
cur = res[1].trim();
def token = res[0].trim();
if (token.length() > 0) {
tokens.add(token);
} else {
if (cur.length() > 0) {
tokens.add(cur);
}
break;
}
}
return tokens;
}
def arr = splitUnquoted(ctx.message, " ");
Map map = new HashMap();
Pattern pattern = /^\"|\"$/;
for (def i = 0; i < arr?.length; i++) {
def kv = splitOnceByToken(arr[i], "=");
if (kv.length == 2 && kv[0].length() > 0) {
map[kv[0]] = pattern.matcher(kv[1]).replaceAll("");
}
}
if (ctx.fac == null) {
ctx.fac = new HashMap();
}
ctx.fac = map;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment