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

[Backport 2.x] Add support for Flat Objects #739

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -3,6 +3,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
## [Unreleased 2.x]
### 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
13 changes: 13 additions & 0 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [Creating an index](#creating-an-index)
- [With default settings](#with-default-settings)
- [With custom settings and mappings](#with-custom-settings-and-mappings)
- [With FlatObject mappings](#with-flat-object-mappings)
- [Indexing data](#indexing-data)
- [Searching for a document](#searching-for-a-document)
- [Deleting a document](#deleting-a-document)
Expand Down Expand Up @@ -127,6 +128,18 @@ CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder()
.build();
client.indices().create(createIndexRequest);
```
#### With flat object mappings
OpenSearch supports FlatObject mappings from version 2.7.0 without additional parameters.

```java
final CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(indexName)
.mappings(m -> m.properties("issue", Property.of(p -> p.flatObject(new FlatObjectProperty.Builder().build()))))
.build();
client.indices().create(createIndexRequest);
```

You can find a working sample of the above code in [FlatObjectBasics.java](./samples/src/main/java/org/opensearch/client/samples/FlatObjectBasics.java).


### Indexing data

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
@@ -0,0 +1,116 @@
/*
* 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.
*/

/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.client.opensearch._types.mapping;

import jakarta.json.stream.JsonGenerator;
import java.util.function.Function;
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.util.ObjectBuilder;

// typedef: _types.mapping.FlatObjectProperty

@JsonpDeserializable
public class FlatObjectProperty extends PropertyBase implements PropertyVariant {

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

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

}

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

/**
* Property variant kind.
*/
@Override
public Property.Kind _propertyKind() {
return Property.Kind.FlatObject;
}

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

generator.write("type", "flat_object");
super.serializeInternal(generator, mapper);
}

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

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

public static class Builder extends PropertyBase.AbstractBuilder<Builder> implements ObjectBuilder<FlatObjectProperty> {

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

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

return new FlatObjectProperty(this);
}
}

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

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

protected static void setupFlatObjectPropertyDeserializer(ObjectDeserializer<FlatObjectProperty.Builder> op) {
PropertyBase.setupPropertyBaseDeserializer(op);

op.ignore("type");
}

}
Loading
Loading