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 Flat Objects #735

Merged
merged 6 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -29,6 +29,7 @@ This section is for maintaining a changelog for all breaking changes for the cli

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

### Dependencies

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public enum FieldType implements JsonEnum {

RankFeatures("rank_features"),

Flattened("flattened"),
FlatObject("flat_object"),

Shape("shape"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.util.ObjectBuilder;

// typedef: _types.mapping.FlattenedProperty
// typedef: _types.mapping.FlatObjectProperty

@JsonpDeserializable
public class FlattenedProperty extends PropertyBase implements PropertyVariant {
public class FlatObjectProperty extends PropertyBase implements PropertyVariant {
@Nullable
private final Double boost;

Expand Down Expand Up @@ -75,7 +75,7 @@ public class FlattenedProperty extends PropertyBase implements PropertyVariant {

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

private FlattenedProperty(Builder builder) {
private FlatObjectProperty(Builder builder) {
super(builder);

this.boost = builder.boost;
Expand All @@ -90,7 +90,7 @@ private FlattenedProperty(Builder builder) {

}

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

Expand All @@ -99,7 +99,7 @@ public static FlattenedProperty of(Function<Builder, ObjectBuilder<FlattenedProp
*/
@Override
public Property.Kind _propertyKind() {
return Property.Kind.Flattened;
return Property.Kind.FlatObject;
}

/**
Expand Down Expand Up @@ -176,7 +176,7 @@ public final Boolean splitQueriesOnWhitespace() {

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

generator.write("type", "flattened");
generator.write("type", "flat_object");
super.serializeInternal(generator, mapper);
if (this.boost != null) {
generator.writeKey("boost");
Expand Down Expand Up @@ -228,10 +228,10 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
// ---------------------------------------------------------------------------------------------

/**
* Builder for {@link FlattenedProperty}.
* Builder for {@link FlatObjectProperty}.
*/

public static class Builder extends PropertyBase.AbstractBuilder<Builder> implements ObjectBuilder<FlattenedProperty> {
public static class Builder extends PropertyBase.AbstractBuilder<Builder> implements ObjectBuilder<FlatObjectProperty> {
@Nullable
private Double boost;

Expand Down Expand Up @@ -337,29 +337,29 @@ protected Builder self() {
}

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

return new FlattenedProperty(this);
return new FlatObjectProperty(this);
}
}

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

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

protected static void setupFlattenedPropertyDeserializer(ObjectDeserializer<FlattenedProperty.Builder> op) {
protected static void setupFlatObjectPropertyDeserializer(ObjectDeserializer<FlatObjectProperty.Builder> op) {
PropertyBase.setupPropertyBaseDeserializer(op);
op.add(Builder::boost, JsonpDeserializer.doubleDeserializer(), "boost");
op.add(Builder::depthLimit, JsonpDeserializer.integerDeserializer(), "depth_limit");
Bfindlay marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public enum Kind implements JsonEnum {

Alias("alias"),

Flattened("flattened"),
FlatObject("flat_object"),

Float("float"),

Expand Down Expand Up @@ -402,20 +402,20 @@ public FieldAliasProperty alias() {
}

/**
* Is this variant instance of kind {@code flattened}?
* Is this variant instance of kind {@code flat_oject}?
*/
public boolean isFlattened() {
return _kind == Kind.Flattened;
public boolean isFlatObject() {
return _kind == Kind.FlatObject;
}

/**
* Get the {@code flattened} variant value.
* Get the {@code flat_object} variant value.
*
* @throws IllegalStateException
* if the current variant is not of the {@code flattened} kind.
* if the current variant is not of the {@code flat_object} kind.
*/
public FlattenedProperty flattened() {
return TaggedUnionUtils.get(this, Kind.Flattened);
public FlatObjectProperty flatObject() {
return TaggedUnionUtils.get(this, Kind.FlatObject);
}

/**
Expand Down Expand Up @@ -1098,14 +1098,14 @@ public ObjectBuilder<Property> alias(Function<FieldAliasProperty.Builder, Object
return this.alias(fn.apply(new FieldAliasProperty.Builder()).build());
}

public ObjectBuilder<Property> flattened(FlattenedProperty v) {
this._kind = Kind.Flattened;
public ObjectBuilder<Property> flatObject(FlatObjectProperty v) {
this._kind = Kind.FlatObject;
this._value = v;
return this;
}

public ObjectBuilder<Property> flattened(Function<FlattenedProperty.Builder, ObjectBuilder<FlattenedProperty>> fn) {
return this.flattened(fn.apply(new FlattenedProperty.Builder()).build());
public ObjectBuilder<Property> flatObject(Function<FlatObjectProperty.Builder, ObjectBuilder<FlatObjectProperty>> fn) {
return this.flatObject(fn.apply(new FlatObjectProperty.Builder()).build());
}

public ObjectBuilder<Property> float_(FloatNumberProperty v) {
Expand Down Expand Up @@ -1457,7 +1457,7 @@ protected static void setupPropertyDeserializer(ObjectDeserializer<Builder> op)
op.add(Builder::double_, DoubleNumberProperty._DESERIALIZER, "double");
op.add(Builder::doubleRange, DoubleRangeProperty._DESERIALIZER, "double_range");
op.add(Builder::alias, FieldAliasProperty._DESERIALIZER, "alias");
op.add(Builder::flattened, FlattenedProperty._DESERIALIZER, "flattened");
op.add(Builder::flatObject, FlatObjectProperty._DESERIALIZER, "flat_object");
op.add(Builder::float_, FloatNumberProperty._DESERIALIZER, "float");
op.add(Builder::floatRange, FloatRangeProperty._DESERIALIZER, "float_range");
op.add(Builder::geoPoint, GeoPointProperty._DESERIALIZER, "geo_point");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ public static FieldAliasProperty.Builder alias() {
}

/**
* Creates a builder for the {@link FlattenedProperty flattened}
* Creates a builder for the {@link FlatObjectProperty flatObject}
* {@code Property} variant.
*/
public static FlattenedProperty.Builder flattened() {
return new FlattenedProperty.Builder();
public static FlatObjectProperty.Builder flatObject() {
return new FlatObjectProperty.Builder();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.opensearch.Version;
import org.opensearch.client.opensearch.OpenSearchAsyncClient;
import org.opensearch.client.opensearch._types.OpenSearchException;
import org.opensearch.client.opensearch._types.mapping.FlatObjectProperty;
import org.opensearch.client.opensearch._types.mapping.Property;
import org.opensearch.client.opensearch.core.InfoResponse;
import org.opensearch.client.opensearch.indices.CreateDataStreamResponse;
import org.opensearch.client.opensearch.indices.CreateIndexRequest;
import org.opensearch.client.opensearch.indices.CreateIndexResponse;
import org.opensearch.client.opensearch.indices.DataStream;
import org.opensearch.client.opensearch.indices.DataStreamsStatsResponse;
Expand Down Expand Up @@ -192,4 +198,23 @@ public void testGetNotExistingIndexAlias() throws Exception {
assertEquals(ex.getMessage(), "Request failed: [string_error] " + "alias [alias_not_exists] missing");
}
}

@Test
public void createIndex_withFlatObject_IndexCreatesSucessfully() throws IOException {
InfoResponse info = javaClient().info();
String version = info.version().number();
if (version.contains("SNAPSHOT")) {
version = version.split("-")[0];
}
assumeTrue("Flat Object is supported after version 2.7.0 only", Version.fromString(version).onOrAfter(Version.fromString("2.7.0")));
try {
final CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index("flat-object-test")
.mappings(m -> m.properties("sample_flat_object", Property.of(p -> p.flatObject(new FlatObjectProperty.Builder().build()))))
Bfindlay marked this conversation as resolved.
Show resolved Hide resolved
.build();
final CreateIndexResponse createIndexResponse = javaClient().indices().create(createIndexRequest);
assertTrue(createIndexResponse.acknowledged());
} catch (OpenSearchException ex) {
fail(ex.getMessage());
}
}
}