Skip to content

Commit

Permalink
Add support for Flat Objects (#735)
Browse files Browse the repository at this point in the history
* add flat object support

Signed-off-by: bfindlay <[email protected]>

* add flat object support

Signed-off-by: bfindlay <[email protected]>

* update changelog

Signed-off-by: bfindlay <[email protected]>

* update flatobject and user guides

Signed-off-by: bfindlay <[email protected]>

* fix typo

Signed-off-by: bfindlay <[email protected]>

* remove unused comments

Signed-off-by: bfindlay <[email protected]>

---------

Signed-off-by: bfindlay <[email protected]>
  • Loading branch information
Bfindlay authored Nov 21, 2023
1 parent 72d2b05 commit a4dc4f0
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 394 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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
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 @@ -136,6 +137,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

0 comments on commit a4dc4f0

Please sign in to comment.