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

Fixed GetFieldMappingResponse. #641

Merged
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 @@ -29,6 +29,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Fixed
- Fix version and build ([#254](https://github.com/opensearch-project/opensearch-java/pull/254))
- Fix PutMappingRequest by removing unsupported fields ([#597](https://github.com/opensearch-project/opensearch-java/pull/597))
- Fix parsing of GetFieldMappingResponse ([#641](https://github.com/opensearch-project/opensearch-java/pull/641))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@

@JsonpDeserializable
public class FieldMapping implements JsonpSerializable {
// Single key dictionary
private final String field;

private final String fullName;

private final Map<String, Property> mapping;
Expand All @@ -61,8 +58,6 @@ public class FieldMapping implements JsonpSerializable {

private FieldMapping(Builder builder) {

this.field = ApiTypeHelper.requireNonNull(builder.field, this, "field");

this.fullName = ApiTypeHelper.requireNonNull(builder.fullName, this, "fullName");
this.mapping = ApiTypeHelper.unmodifiableRequired(builder.mapping, this, "mapping");

Expand All @@ -72,13 +67,6 @@ public static FieldMapping of(Function<Builder, ObjectBuilder<FieldMapping>> fn)
return fn.apply(new Builder()).build();
}

/**
* Required - The target field
*/
public final String field() {
return this.field;
}

/**
* Required - API name: {@code full_name}
*/
Expand All @@ -103,8 +91,6 @@ public void serialize(JsonGenerator generator, JsonpMapper mapper) {
}

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
generator.writeStartObject(this.field);

generator.writeKey("full_name");
generator.write(this.fullName);

Expand All @@ -120,8 +106,6 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

}

generator.writeEnd();

}

// ---------------------------------------------------------------------------------------------
Expand All @@ -131,15 +115,6 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
*/

public static class Builder extends ObjectBuilderBase implements ObjectBuilder<FieldMapping> {
private String field;

/**
* Required - The target field
*/
public final Builder field(String value) {
this.field = value;
return this;
}

private String fullName;

Expand Down Expand Up @@ -208,8 +183,6 @@ protected static void setupFieldMappingDeserializer(ObjectDeserializer<FieldMapp
op.add(Builder::fullName, JsonpDeserializer.stringDeserializer(), "full_name");
op.add(Builder::mapping, JsonpDeserializer.stringMapDeserializer(Property._DESERIALIZER), "mapping");

op.setKey(Builder::field, JsonpDeserializer.stringDeserializer());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@

package org.opensearch.client.opensearch.indices.get_field_mapping;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.opensearch.client.opensearch._types.mapping.FieldMapping;
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
Expand All @@ -51,13 +54,13 @@

@JsonpDeserializable
public class TypeFieldMappings implements JsonpSerializable {
private final FieldMapping mappings;
private final Map<String, FieldMapping> mappings;

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

private TypeFieldMappings(Builder builder) {

this.mappings = ApiTypeHelper.requireNonNull(builder.mappings, this, "mappings");
this.mappings = ApiTypeHelper.unmodifiableRequired(builder.mappings, this, "mappings");

}

Expand All @@ -68,7 +71,7 @@ public static TypeFieldMappings of(Function<Builder, ObjectBuilder<TypeFieldMapp
/**
* Required - API name: {@code mappings}
*/
public final FieldMapping mappings() {
public final Map<String, FieldMapping> mappings() {
return this.mappings;
}

Expand All @@ -84,7 +87,13 @@ public void serialize(JsonGenerator generator, JsonpMapper mapper) {
protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

generator.writeKey("mappings");
this.mappings.serialize(generator, mapper);
generator.writeStartObject();
for (Map.Entry<String, FieldMapping> item0 : this.mappings.entrySet()) {
generator.writeKey(item0.getKey());
item0.getValue().serialize(generator, mapper);

}
generator.writeEnd();

}

Expand All @@ -95,21 +104,39 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
*/

public static class Builder extends ObjectBuilderBase implements ObjectBuilder<TypeFieldMappings> {
private FieldMapping mappings;
private Map<String, FieldMapping> mappings;

/**
* Required - API name: {@code mappings}
*/
public final Builder mappings(FieldMapping value) {
public Builder mappings(Map<String, FieldMapping> value) {
this.mappings = value;
return this;
}

/**
* Required - API name: {@code mappings}
* Add a key/value to {@link #mappings(Map)}, creating the map if needed.
*/
public Builder putMappings(String key, FieldMapping value) {
if (this.mappings == null) {
this.mappings = new HashMap<>();
}
this.mappings.put(key, value);
return this;
}

/**
* Set {@link #mappings(Map)} to a singleton map.
*/
public Builder mappings(String key, Function<FieldMapping.Builder, ObjectBuilder<FieldMapping>> fn) {
return this.mappings(Collections.singletonMap(key, fn.apply(new FieldMapping.Builder()).build()));
}

/**
* Add a key/value to {@link #mappings(Map)}, creating the map if needed.
*/
public final Builder mappings(Function<FieldMapping.Builder, ObjectBuilder<FieldMapping>> fn) {
return this.mappings(fn.apply(new FieldMapping.Builder()).build());
public Builder putMappings(String key, Function<FieldMapping.Builder, ObjectBuilder<FieldMapping>> fn) {
return this.putMappings(key, fn.apply(new FieldMapping.Builder()).build());
}

/**
Expand All @@ -135,7 +162,7 @@ public TypeFieldMappings build() {

protected static void setupTypeFieldMappingsDeserializer(ObjectDeserializer<TypeFieldMappings.Builder> op) {

op.add(Builder::mappings, FieldMapping._DESERIALIZER, "mappings");
op.add(Builder::mappings, JsonpDeserializer.stringMapDeserializer(FieldMapping._DESERIALIZER), "mappings");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@
package org.opensearch.client.opensearch.experiments;

import java.util.List;

import java.util.Map;
import org.junit.Test;
import org.opensearch.client.opensearch._types.Time;
import org.opensearch.client.opensearch._types.analysis.Analyzer;
import org.opensearch.client.opensearch._types.analysis.TokenFilterDefinition;
import org.opensearch.client.opensearch._types.analysis.TokenizerBuilders;
import org.opensearch.client.opensearch._types.analysis.TokenizerDefinition;
import org.opensearch.client.opensearch._types.mapping.FieldMapping;
import org.opensearch.client.opensearch._types.mapping.Property;
import org.opensearch.client.opensearch._types.mapping.TermVectorOption;
import org.opensearch.client.opensearch._types.mapping.TextProperty;
import org.opensearch.client.opensearch.experiments.api.FooRequest;
import org.opensearch.client.opensearch.indices.GetFieldMappingResponse;
import org.opensearch.client.opensearch.indices.IndexSettings;
import org.opensearch.client.opensearch.indices.IndexSettingsMapping;
import org.opensearch.client.opensearch.indices.Translog;
import org.opensearch.client.opensearch.indices.get_field_mapping.TypeFieldMappings;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class ParsingTests extends ModelTestCase {
Expand Down Expand Up @@ -173,4 +179,74 @@ public void testCjk_Analyzer() {
assertEquals(analyzer.cjk().stopwords(), analyzer2.cjk().stopwords());
assertEquals(analyzer.cjk().stopwordsPath(), analyzer2.cjk().stopwordsPath());
}

@Test
public void testFieldMappingResponse() {
final String indexName = "indexName";
final String field1Name = "field1";
final String field1Analyzer = "my_analyzer";
final FieldMapping field1 = FieldMapping.of(fmb -> fmb
.fullName(field1Name)
.mapping(field1Name, pb -> pb
.text(tpb -> tpb
.store(true)
.termVector(TermVectorOption.WithPositionsOffsets)
.analyzer(field1Analyzer)
.positionIncrementGap(10))
)
);
final String field3Name = "field3";
// Build aFieldMappingResponse with several fields
final GetFieldMappingResponse response = GetFieldMappingResponse.of(b -> b
.putResult(indexName, TypeFieldMappings.of(tfmb -> tfmb
.putMappings(field1Name, field1)
.putMappings("field2", fmb -> fmb
.fullName("field2")
.mapping("field2", pb -> pb
.text(tpb -> tpb
.store(true)
.termVector(TermVectorOption.WithPositionsOffsets)
.analyzer("another_analyzer")
.positionIncrementGap(10))
)
)
.putMappings(field3Name, fmb -> fmb
.fullName(field3Name)
.mapping(field3Name, pb -> pb
.text(tpb -> tpb
.store(true)
.termVector(TermVectorOption.WithPositionsOffsets)
.analyzer("this_analyzer")
.positionIncrementGap(10))
)
)
)
)
);
String str = toJson(response);
assertEquals("{\"indexName\":{\"mappings\":{\"field1\":{\"full_name\":\"field1\",\"mapping\""
+":{\"field1\":{\"type\":\"text\",\"store\":true,\"analyzer\":\"my_analyzer\"," +
"\"position_increment_gap\":10,\"term_vector\":\"with_positions_offsets\"}}},"
+"\"field3\":{\"full_name\":\"field3\",\"mapping\":{\"field3\":{\"type\":\"text\",\"store\":true," +
"\"analyzer\":\"this_analyzer\",\"position_increment_gap\":10,\"term_vector\":\"with_positions_offsets\"}}},"
+"\"field2\":{\"full_name\":\"field2\",\"mapping\":{\"field2\":{\"type\":\"text\",\"store\":true,"
+"\"analyzer\":\"another_analyzer\",\"position_increment_gap\":10,\"term_vector\":\"with_positions_offsets\"}}}}}}", str);

final GetFieldMappingResponse response2 = fromJson(str, GetFieldMappingResponse._DESERIALIZER);
final TypeFieldMappings typeFieldMappings = response2.get(indexName);
assertNotNull(typeFieldMappings);
final Map<String, FieldMapping> mappings = typeFieldMappings.mappings();
assertFalse(mappings.isEmpty());
final FieldMapping field1_des = mappings.get(field1Name);
assertNotNull(field1_des);
assertEquals(field1Name, field1_des.fullName());
final Property field1Prop = field1_des.mapping().get(field1Name);
assertNotNull(field1Prop);
final TextProperty textProperty = field1Prop.text();
assertNotNull(textProperty);
assertEquals(field1Analyzer, textProperty.analyzer());

assertNotNull(mappings.get(field3Name));
}

}
Loading