Skip to content

Commit

Permalink
Fix pattern replace by making flag optional as on api (#895) (#896)
Browse files Browse the repository at this point in the history
* Fix pattern replace by making flag optional as on api



* Update changelog



* Add unit test for pattern replace char filter analyzer



* fix changelog



* fix char pattern replace by making replcement optional



* Add deserialze test with only pattern set



---------


(cherry picked from commit e240b27)

Signed-off-by: Grouh <spamart@lafourchette.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 1b2ff84 commit 0be1935
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Fixed
- Fix integer overflow for variables in indices stats response ([#877](https://github.com/opensearch-project/opensearch-java/pull/877))
- Support weight function in function score query ([#880](https://github.com/opensearch-project/opensearch-java/pull/880))
- Fix pattern replace by making flag and replacement optional as on api ([#895](https://github.com/opensearch-project/opensearch-java/pull/895))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import jakarta.json.stream.JsonGenerator;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
Expand All @@ -46,21 +47,22 @@

@JsonpDeserializable
public class PatternReplaceCharFilter extends CharFilterBase implements CharFilterDefinitionVariant {
@Nullable
private final String flags;

private final String pattern;

@Nullable
private final String replacement;

// ---------------------------------------------------------------------------------------------

private PatternReplaceCharFilter(Builder builder) {
super(builder);

this.flags = ApiTypeHelper.requireNonNull(builder.flags, this, "flags");
this.flags = builder.flags;
this.pattern = ApiTypeHelper.requireNonNull(builder.pattern, this, "pattern");
this.replacement = ApiTypeHelper.requireNonNull(builder.replacement, this, "replacement");

this.replacement = builder.replacement;
}

public static PatternReplaceCharFilter of(Function<Builder, ObjectBuilder<PatternReplaceCharFilter>> fn) {
Expand All @@ -76,8 +78,9 @@ public CharFilterDefinition.Kind _charFilterDefinitionKind() {
}

/**
* Required - API name: {@code flags}
* API name: {@code flags}
*/
@Nullable
public final String flags() {
return this.flags;
}
Expand All @@ -90,7 +93,7 @@ public final String pattern() {
}

/**
* Required - API name: {@code replacement}
* API name: {@code replacement}
*/
public final String replacement() {
return this.replacement;
Expand All @@ -100,15 +103,19 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

generator.write("type", "pattern_replace");
super.serializeInternal(generator, mapper);
generator.writeKey("flags");
generator.write(this.flags);

generator.writeKey("pattern");
generator.write(this.pattern);

generator.writeKey("replacement");
generator.write(this.replacement);
if (this.replacement != null) {
generator.writeKey("replacement");
generator.write(this.replacement);
}

if (this.flags != null) {
generator.writeKey("flags");
generator.write(this.flags);
}
}

// ---------------------------------------------------------------------------------------------
Expand All @@ -118,16 +125,18 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
*/

public static class Builder extends CharFilterBase.AbstractBuilder<Builder> implements ObjectBuilder<PatternReplaceCharFilter> {
@Nullable
private String flags;

private String pattern;

@Nullable
private String replacement;

/**
* Required - API name: {@code flags}
* API name: {@code flags}
*/
public final Builder flags(String value) {
public final Builder flags(@Nullable String value) {
this.flags = value;
return this;
}
Expand All @@ -141,9 +150,9 @@ public final Builder pattern(String value) {
}

/**
* Required - API name: {@code replacement}
* API name: {@code replacement}
*/
public final Builder replacement(String value) {
public final Builder replacement(@Nullable String value) {
this.replacement = value;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.client.opensearch._types.analysis;

import static org.junit.Assert.assertEquals;

import jakarta.json.stream.JsonParser;
import java.io.StringReader;
import org.junit.Test;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;

public class PatternReplaceCharFilterTest {
@Test
public void testCreatePatternReplaceCharFilter() {
PatternReplaceCharFilter patternReplaceCharFilter = new PatternReplaceCharFilter.Builder().pattern("pattern").build();
assertEquals("pattern", patternReplaceCharFilter.pattern());
}

@Test
public void testCreatePatternReplaceCharFilterWithReplacement() {
PatternReplaceCharFilter patternReplaceCharFilter = new PatternReplaceCharFilter.Builder().pattern("pattern")
.replacement("replacement")
.build();
assertEquals("pattern", patternReplaceCharFilter.pattern());
assertEquals("replacement", patternReplaceCharFilter.replacement());
}

@Test
public void testCreatePatternReplaceCharFilterWithFlags() {
PatternReplaceCharFilter patternReplaceCharFilter = new PatternReplaceCharFilter.Builder().pattern("pattern")
.replacement("replacement")
.flags("flags")
.build();
assertEquals("pattern", patternReplaceCharFilter.pattern());
assertEquals("replacement", patternReplaceCharFilter.replacement());
assertEquals("flags", patternReplaceCharFilter.flags());
}

@Test
public void testDeserializePatternReplaceCharFilterWithAllFields() {
String jsonString =
"{\"type\": \"pattern_replace\", \"pattern\": \"pattern\", \"replacement\": \"replacement\", \"flags\": \"flags\"}";

StringReader reader = new StringReader(jsonString);
JacksonJsonpMapper mapper = new JacksonJsonpMapper();
JsonParser parser = mapper.jsonProvider().createParser(reader);

PatternReplaceCharFilter patternReplaceCharFilter = PatternReplaceCharFilter._DESERIALIZER.deserialize(parser, mapper);
assertEquals("pattern", patternReplaceCharFilter.pattern());
assertEquals("replacement", patternReplaceCharFilter.replacement());
assertEquals("flags", patternReplaceCharFilter.flags());
}

@Test
public void testDeserializePatternReplaceCharFilterWithPatternOnly() {
String jsonString = "{\"type\": \"pattern_replace\", \"pattern\": \"pattern\"}";

StringReader reader = new StringReader(jsonString);
JacksonJsonpMapper mapper = new JacksonJsonpMapper();
JsonParser parser = mapper.jsonProvider().createParser(reader);

PatternReplaceCharFilter patternReplaceCharFilter = PatternReplaceCharFilter._DESERIALIZER.deserialize(parser, mapper);
assertEquals("pattern", patternReplaceCharFilter.pattern());
}
}

0 comments on commit 0be1935

Please sign in to comment.