Skip to content

Commit

Permalink
Fix dissect ingest processor parsing empty brackets failed
Browse files Browse the repository at this point in the history
Signed-off-by: Gao Binlong <gbinlong@amazon.com>
  • Loading branch information
gaobinlong committed Aug 11, 2023
1 parent d352f2c commit 29372cb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Fixed
- Fix flaky ResourceAwareTasksTests.testBasicTaskResourceTracking test ([#8993](https://github.com/opensearch-project/OpenSearch/pull/8993))
- Fix dissect ingest processor parsing empty brackets failed

### Security

[Unreleased 3.0]: https://github.com/opensearch-project/OpenSearch/compare/2.x...HEAD
[Unreleased 2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.10...2.x
[Unreleased 2.x]: https://github.com/opensearch-project/OpenSearch/compare/2.10...2.x
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public Map<String, String> parse(String inputString) {
int lookAheadMatches;
// start walking the input string byte by byte, look ahead for matches where needed
// if a match is found jump forward to the end of the match
for (; i < input.length; i++) {
while (i < input.length) {
lookAheadMatches = 0;
// potential match between delimiter and input string
if (delimiter.length > 0 && input[i] == delimiter[0]) {
Expand Down Expand Up @@ -284,7 +284,11 @@ public Map<String, String> parse(String inputString) {
delimiter = dissectPair.getDelimiter().getBytes(StandardCharsets.UTF_8);
// i is always one byte after the last found delimiter, aka the start of the next value
valueStart = i;
} else {
i++;
}
} else {
i++;
}
}
// the last key, grab the rest of the input (unless consecutive delimiters already grabbed the last key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,28 @@ public void testNullValueWithOutIgnoreMissing() {
IngestDocument ingestDocument = new IngestDocument(originalIngestDocument);
expectThrows(IllegalArgumentException.class, () -> processor.execute(ingestDocument));
}

public void testMatchEmptyBrackets() {
IngestDocument ingestDocument = new IngestDocument(
"_index",
"_id",
null,
null,
null,
Collections.singletonMap("message", "[foo],[bar],[]")
);
DissectProcessor dissectProcessor = new DissectProcessor("", null, "message", "[%{a}],[%{b}],[%{c}]", "", true);
dissectProcessor.execute(ingestDocument);
assertEquals("foo", ingestDocument.getFieldValue("a", String.class));
assertEquals("bar", ingestDocument.getFieldValue("b", String.class));
assertEquals("", ingestDocument.getFieldValue("c", String.class));

ingestDocument = new IngestDocument("_index", "_id", null, null, null, Collections.singletonMap("message", "{}{}{}{baz}"));
dissectProcessor = new DissectProcessor("", null, "message", "{%{a}}{%{b}}{%{c}}{%{d}}", "", true);
dissectProcessor.execute(ingestDocument);
assertEquals("", ingestDocument.getFieldValue("a", String.class));
assertEquals("", ingestDocument.getFieldValue("b", String.class));
assertEquals("", ingestDocument.getFieldValue("c", String.class));
assertEquals("baz", ingestDocument.getFieldValue("d", String.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,38 @@ teardown:
}
]
}
---
"Test dissect processor can match empty brackets":
- do:
ingest.put_pipeline:
id: "my_pipeline"
body: >
{
"description": "_description",
"processors": [
{
"dissect" : {
"field" : "message",
"pattern" : "[%{a}][%{b}][%{c}]"
}
}
]
}
- match: { acknowledged: true }

- do:
index:
index: test
id: 1
pipeline: "my_pipeline"
body: {message: "[foo][bar][]"}

- do:
get:
index: test
id: 1
- match: { _source.message: "[foo][bar][]" }
- match: { _source.a: "foo" }
- match: { _source.b: "bar" }
- match: { _source.c: "" }

0 comments on commit 29372cb

Please sign in to comment.