Skip to content

Commit

Permalink
Add support for highlight-field options (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeuwr authored and Rick de Leeuw committed Feb 24, 2022
1 parent 74164d8 commit eb64d2f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package co.elastic.clients.elasticsearch.core.search;

import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.json.JsonData;
import co.elastic.clients.json.JsonpDeserializable;
import co.elastic.clients.json.JsonpDeserializer;
import co.elastic.clients.json.JsonpMapper;
Expand All @@ -38,7 +39,7 @@
import java.lang.Integer;
import java.lang.String;
import java.util.List;
import java.util.Objects;
import java.util.Map;
import java.util.function.Function;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -112,6 +113,10 @@ public class HighlightField implements JsonpSerializable {
@Nullable
private final String type;

@Nullable
private final Map<String, JsonData> options;


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

private HighlightField(Builder builder) {
Expand All @@ -137,7 +142,7 @@ private HighlightField(Builder builder) {
this.requireFieldMatch = builder.requireFieldMatch;
this.tagsSchema = builder.tagsSchema;
this.type = builder.type;

this.options = ApiTypeHelper.unmodifiable(builder.options);
}

public static HighlightField of(Function<Builder, ObjectBuilder<HighlightField>> fn) {
Expand Down Expand Up @@ -309,6 +314,14 @@ public final String type() {
return this.type;
}

/**
* API name: {@code options}
*/
@Nullable
public final Map<String, JsonData> options() {
return this.options;
}

/**
* Serialize this object to JSON.
*/
Expand Down Expand Up @@ -436,6 +449,16 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
generator.write(this.type);

}
if (ApiTypeHelper.isDefined(this.options)) {
generator.writeKey("options");
generator.writeStartObject();
for (Map.Entry<String, JsonData> item0 : this.options.entrySet()) {
generator.writeKey(item0.getKey());
item0.getValue().serialize(generator, mapper);

}
generator.writeEnd();
}

}

Expand Down Expand Up @@ -509,6 +532,9 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<H
@Nullable
private String type;

@Nullable
private Map<String, JsonData> options;

/**
* API name: {@code boundary_chars}
*/
Expand Down Expand Up @@ -728,6 +754,26 @@ public final Builder type(@Nullable HighlighterType value) {
return this;
}

/**
* API name: {@code options}
* <p>
* Adds all entries of <code>map</code> to <code>options</code>.
*/
public final Builder options(@Nullable Map<String, JsonData> map) {
this.options = _mapPutAll(this.options, map);
return this;
}

/**
* API name: {@code options}
* <p>
* Adds an entry to <code>options</code>.
*/
public final Builder params(String key, JsonData value) {
this.options = _mapPut(this.options, key, value);
return this;
}

/**
* Builds a {@link HighlightField}.
*
Expand Down Expand Up @@ -775,6 +821,7 @@ protected static void setupHighlightFieldDeserializer(ObjectDeserializer<Highlig
op.add(Builder::requireFieldMatch, JsonpDeserializer.booleanDeserializer(), "require_field_match");
op.add(Builder::tagsSchema, HighlighterTagsSchema._DESERIALIZER, "tags_schema");
op.add(Builder::type, JsonpDeserializer.stringDeserializer(), "type");
op.add(Builder::options, JsonpDeserializer.stringMapDeserializer(JsonData._DESERIALIZER), "options");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
import co.elastic.clients.elasticsearch.cluster.ClusterStatsResponse;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.HighlightField;
import co.elastic.clients.elasticsearch.model.ModelTestCase;
import co.elastic.clients.json.JsonData;
import co.elastic.clients.json.JsonpDeserializer;
import jakarta.json.stream.JsonParser;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
* Test issues related to the API specifications.
Expand Down Expand Up @@ -101,6 +104,17 @@ public void i0056_hitsMetadataTotal() throws Exception {
.trackTotalHits(thb -> thb.enabled(false)), JsonData.class);
}

@Test
public void i0168_highlightFieldOptions() {
// https://github.com/elastic/elasticsearch-java/issues/168
Map<String, JsonData> options = new HashMap<>();
options.put("foo", JsonData.of("bar"));

HighlightField field = HighlightField.of(_0 -> _0.field("tst").options(options));

assertEquals("{\"field\":\"tst\",\"options\":{\"foo\":\"bar\"}}", toJson(field));
}

private <T> T loadRsrc(String res, JsonpDeserializer<T> deser) {
InputStream is = this.getClass().getResourceAsStream(res);
assertNotNull("Resource not found: " + res, is);
Expand Down

0 comments on commit eb64d2f

Please sign in to comment.