Skip to content

Commit

Permalink
update flatobject and user guides
Browse files Browse the repository at this point in the history
Signed-off-by: bfindlay <[email protected]>
  • Loading branch information
Bfindlay committed Nov 21, 2023
1 parent 0048b40 commit 10df60f
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 268 deletions.
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 @@ -34,7 +34,6 @@

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,48 +45,12 @@

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

@Nullable
private final Integer depthLimit;

@Nullable
private final Boolean docValues;

@Nullable
private final Boolean eagerGlobalOrdinals;

@Nullable
private final Boolean index;

@Nullable
private final IndexOptions indexOptions;

@Nullable
private final String nullValue;

@Nullable
private final String similarity;

@Nullable
private final Boolean splitQueriesOnWhitespace;

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

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

this.boost = builder.boost;
this.depthLimit = builder.depthLimit;
this.docValues = builder.docValues;
this.eagerGlobalOrdinals = builder.eagerGlobalOrdinals;
this.index = builder.index;
this.indexOptions = builder.indexOptions;
this.nullValue = builder.nullValue;
this.similarity = builder.similarity;
this.splitQueriesOnWhitespace = builder.splitQueriesOnWhitespace;

}

public static FlatObjectProperty of(Function<Builder, ObjectBuilder<FlatObjectProperty>> fn) {
Expand All @@ -102,127 +65,10 @@ public Property.Kind _propertyKind() {
return Property.Kind.FlatObject;
}

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

/**
* API name: {@code depth_limit}
*/
@Nullable
public final Integer depthLimit() {
return this.depthLimit;
}

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

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

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

/**
* API name: {@code index_options}
*/
@Nullable
public final IndexOptions indexOptions() {
return this.indexOptions;
}

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

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

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

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

generator.write("type", "flat_object");
super.serializeInternal(generator, mapper);
if (this.boost != null) {
generator.writeKey("boost");
generator.write(this.boost);

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

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

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

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

}
if (this.indexOptions != null) {
generator.writeKey("index_options");
this.indexOptions.serialize(generator, mapper);
}
if (this.nullValue != null) {
generator.writeKey("null_value");
generator.write(this.nullValue);

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

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

}

}

// ---------------------------------------------------------------------------------------------
Expand All @@ -232,104 +78,6 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
*/

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

@Nullable
private Integer depthLimit;

@Nullable
private Boolean docValues;

@Nullable
private Boolean eagerGlobalOrdinals;

@Nullable
private Boolean index;

@Nullable
private IndexOptions indexOptions;

@Nullable
private String nullValue;

@Nullable
private String similarity;

@Nullable
private Boolean splitQueriesOnWhitespace;

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

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

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

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

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

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

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

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

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

@Override
protected Builder self() {
Expand Down Expand Up @@ -361,15 +109,6 @@ public FlatObjectProperty build() {

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");
op.add(Builder::docValues, JsonpDeserializer.booleanDeserializer(), "doc_values");
op.add(Builder::eagerGlobalOrdinals, JsonpDeserializer.booleanDeserializer(), "eager_global_ordinals");
op.add(Builder::index, JsonpDeserializer.booleanDeserializer(), "index");
op.add(Builder::indexOptions, IndexOptions._DESERIALIZER, "index_options");
op.add(Builder::nullValue, JsonpDeserializer.stringDeserializer(), "null_value");
op.add(Builder::similarity, JsonpDeserializer.stringDeserializer(), "similarity");
op.add(Builder::splitQueriesOnWhitespace, JsonpDeserializer.booleanDeserializer(), "split_queries_on_whitespace");

op.ignore("type");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.opensearch.client.opensearch.indices.GetIndexRequest;
import org.opensearch.client.opensearch.indices.GetIndexResponse;
import org.opensearch.client.opensearch.indices.GetIndicesSettingsRequest;
import org.opensearch.client.opensearch.indices.GetMappingRequest;
import org.opensearch.client.opensearch.indices.GetMappingResponse;
import org.opensearch.client.opensearch.indices.IndexState;
import org.opensearch.client.opensearch.indices.PutIndexTemplateResponse;
import org.opensearch.common.settings.Settings;
Expand Down Expand Up @@ -200,13 +202,9 @@ public void testGetNotExistingIndexAlias() throws Exception {
}

@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")));
public void createIndex_withFlatObject_indexCreatesSuccessfully() throws IOException {
assumeFlatObjectSupport();

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()))))
Expand All @@ -217,4 +215,33 @@ public void createIndex_withFlatObject_IndexCreatesSucessfully() throws IOExcept
fail(ex.getMessage());
}
}

@Test
public void createIndex_withFlatObject_mappingCanBeRetrieved() throws IOException {
assumeFlatObjectSupport();

try {
final String indexName = "flat-object-mapping-index";
final CreateIndexRequest createIndexRequest = new CreateIndexRequest.Builder().index(indexName)
.mappings(m -> m.properties("sample_flat_object", Property.of(p -> p.flatObject(new FlatObjectProperty.Builder().build()))))
.build();
final CreateIndexResponse createIndexResponse = javaClient().indices().create(createIndexRequest);
assertTrue(createIndexResponse.acknowledged());

final GetMappingResponse response = javaClient().indices().getMapping(GetMappingRequest.of(m -> m.index(indexName)));
final Property.Kind mappingKind = response.result().get(indexName).mappings().properties().get("sample_flat_object")._kind();
assertEquals(mappingKind, Property.Kind.FlatObject);
} catch (OpenSearchException ex) {
fail(ex.getMessage());
}
}

private void assumeFlatObjectSupport() 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")));
}
}
Loading

0 comments on commit 10df60f

Please sign in to comment.