Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for icu_collation_keyword field type #725

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
## [Unreleased 2.x]

### Added
- Added support for icu_collation_keyword type ([#725](https://github.com/opensearch-project/opensearch-java/pull/725))

### Dependencies

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* 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.mapping;

import jakarta.json.stream.JsonGenerator;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.opensearch.client.json.*;
import org.opensearch.client.util.ObjectBuilder;

// typedef: _types.mapping.ICUCollationKeywordProperty

@JsonpDeserializable
public class IcuCollationKeywordProperty extends DocValuesPropertyBase implements PropertyVariant {
Bfindlay marked this conversation as resolved.
Show resolved Hide resolved

@Nullable
private final Boolean index;

@Nullable
private final String nullValue;

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

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

this.index = builder.index;
this.nullValue = builder.nullValue;
}

public static IcuCollationKeywordProperty of(Function<Builder, ObjectBuilder<IcuCollationKeywordProperty>> fn) {
return fn.apply(new Builder()).build();
}

@Override
public Property.Kind _propertyKind() {
return Property.Kind.IcuCollationKeyword;
}

/**
* API name: {@code index}
*/
@Nullable
public final Boolean index() {
return this.index;
}

/**
* API name: {@code null_value}
*/
@Nullable
public final String nullValue() {
return this.nullValue;
}

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
generator.write("type", "icu_collation_keyword");
super.serializeInternal(generator, mapper);
if (this.index != null) {
generator.writeKey("index");
generator.write(this.index);
}
if (this.nullValue != null) {
generator.writeKey("null_value");
generator.write(this.nullValue);
}
}

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

/**
* Builder for {@link IcuCollationKeywordProperty}.
*/

public static class Builder extends DocValuesPropertyBase.AbstractBuilder<Builder>
implements
ObjectBuilder<IcuCollationKeywordProperty> {
@Nullable
private Boolean index;

@Nullable
private String nullValue;

/**
* API name: {@code index}
*/
public final Builder index(@Nullable Boolean value) {
this.index = value;
return this;
}

/**
* API name: {@code null_value}
*/
public final Builder nullValue(@Nullable String value) {
this.nullValue = value;
return this;
}

@Override
protected Builder self() {
return this;
}

/**
* Builds a {@link IcuCollationKeywordProperty}.
*
* @throws NullPointerException
* if some of the required fields are null.
*/
public IcuCollationKeywordProperty build() {
_checkSingleUse();
return new IcuCollationKeywordProperty(this);
}
}

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

/**
* Json deserializer for {@link IcuCollationKeywordProperty}
*/
public static final JsonpDeserializer<IcuCollationKeywordProperty> _DESERIALIZER = ObjectBuilderDeserializer.lazy(
Builder::new,
IcuCollationKeywordProperty::setupIcuCollationKeywordPropertyDeserializer
);

protected static void setupIcuCollationKeywordPropertyDeserializer(ObjectDeserializer<IcuCollationKeywordProperty.Builder> op) {
DocValuesPropertyBase.setupDocValuesPropertyBaseDeserializer(op);
op.add(Builder::index, JsonpDeserializer.booleanDeserializer(), "index");
op.add(Builder::nullValue, JsonpDeserializer.stringDeserializer(), "null_value");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public enum Kind implements JsonEnum {

Histogram("histogram"),

IcuCollationKeyword("icu_collation_keyword"),

Integer("integer"),

IntegerRange("integer_range"),
Expand Down Expand Up @@ -518,6 +520,23 @@ public HistogramProperty histogram() {
return TaggedUnionUtils.get(this, Kind.Histogram);
}

/**
* Is this variant instance of kind {@code icu_collation_keyword}?
*/
public boolean isIcuCollationKeyword() {
return _kind == Kind.IcuCollationKeyword;
}

/**
* Get the {@code icu_collation_keyword} variant value.
*
* @throws IllegalStateException
* if the current variant is not of the {@code icu_collation_keyword} kind.
*/
public IcuCollationKeywordProperty icuCollationKeyword() {
return TaggedUnionUtils.get(this, Kind.IcuCollationKeyword);
}

/**
* Is this variant instance of kind {@code integer}?
*/
Expand Down Expand Up @@ -1149,6 +1168,18 @@ public ObjectBuilder<Property> histogram(Function<HistogramProperty.Builder, Obj
return this.histogram(fn.apply(new HistogramProperty.Builder()).build());
}

public ObjectBuilder<Property> icuCollationKeyword(IcuCollationKeywordProperty v) {
this._kind = Kind.IcuCollationKeyword;
this._value = v;
return this;
}

public ObjectBuilder<Property> icuCollationKeyword(
Function<IcuCollationKeywordProperty.Builder, ObjectBuilder<IcuCollationKeywordProperty>> fn
) {
return this.icuCollationKeyword(fn.apply(new IcuCollationKeywordProperty.Builder()).build());
}

public ObjectBuilder<Property> integer(IntegerNumberProperty v) {
this._kind = Kind.Integer;
this._value = v;
Expand Down Expand Up @@ -1433,6 +1464,7 @@ protected static void setupPropertyDeserializer(ObjectDeserializer<Builder> op)
op.add(Builder::geoShape, GeoShapeProperty._DESERIALIZER, "geo_shape");
op.add(Builder::halfFloat, HalfFloatNumberProperty._DESERIALIZER, "half_float");
op.add(Builder::histogram, HistogramProperty._DESERIALIZER, "histogram");
op.add(Builder::icuCollationKeyword, IcuCollationKeywordProperty._DESERIALIZER, "icu_collation_keyword");
op.add(Builder::integer, IntegerNumberProperty._DESERIALIZER, "integer");
op.add(Builder::integerRange, IntegerRangeProperty._DESERIALIZER, "integer_range");
op.add(Builder::ip, IpProperty._DESERIALIZER, "ip");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.opensearch.client.opensearch.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.StringReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.jsonb.JsonbJsonpMapper;
import org.opensearch.client.opensearch._types.mapping.IcuCollationKeywordProperty;
import org.opensearch.client.opensearch._types.mapping.Property;
import org.opensearch.client.opensearch.indices.GetTemplateResponse;

public class GetMappingsResponseTest extends Assert {

@Test
public void deserialize_IcuCollationKeywordExists_propertyDeserializes() throws JsonProcessingException {
Map<String, Object> mappingTemplate = new HashMap<>();
mappingTemplate.put(
"test-index",
Map.of(
"aliases",
Collections.emptyMap(),
"index_patterns",
Collections.singletonList("test-pattern*"),
"mappings",
Map.of(
"properties",
Map.of(
"icu_test_field",
Map.of(
"type",
"icu_collation_keyword",
"ignore_above",
1,
"index",
true,
"null_value",
"not-null-value",
"store",
true
)
)
),
"order",
0,
"settings",
Collections.emptyMap(),
"version",
1
)
);
final JsonpMapper mapper = new JsonbJsonpMapper();
final String indexTemplate = new ObjectMapper().writeValueAsString(mappingTemplate);
final var parser = mapper.jsonProvider().createParser(new StringReader(indexTemplate));

final GetTemplateResponse response = GetTemplateResponse._DESERIALIZER.deserialize(parser, mapper);
final var template = response.get("test-index");
final var mappings = template.mappings();
final var properties = mappings.properties();
final var property = properties.get("icu_test_field");
final IcuCollationKeywordProperty icu = property.icuCollationKeyword();

assertEquals(property._kind(), Property.Kind.IcuCollationKeyword);
assertTrue(property.isIcuCollationKeyword());
assertEquals(icu.nullValue(), "not-null-value");
assertTrue(icu.index());
assertTrue(icu.store());
assertEquals((int) icu.ignoreAbove(), 1);
}
}
Loading