Skip to content

Commit eb64d2f

Browse files
leeuwrRick de Leeuw
authored and
Rick de Leeuw
committed
Add support for highlight-field options (elastic#168)
1 parent 74164d8 commit eb64d2f

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

java-client/src/main/java/co/elastic/clients/elasticsearch/core/search/HighlightField.java

+49-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package co.elastic.clients.elasticsearch.core.search;
2525

2626
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
27+
import co.elastic.clients.json.JsonData;
2728
import co.elastic.clients.json.JsonpDeserializable;
2829
import co.elastic.clients.json.JsonpDeserializer;
2930
import co.elastic.clients.json.JsonpMapper;
@@ -38,7 +39,7 @@
3839
import java.lang.Integer;
3940
import java.lang.String;
4041
import java.util.List;
41-
import java.util.Objects;
42+
import java.util.Map;
4243
import java.util.function.Function;
4344
import javax.annotation.Nullable;
4445

@@ -112,6 +113,10 @@ public class HighlightField implements JsonpSerializable {
112113
@Nullable
113114
private final String type;
114115

116+
@Nullable
117+
private final Map<String, JsonData> options;
118+
119+
115120
// ---------------------------------------------------------------------------------------------
116121

117122
private HighlightField(Builder builder) {
@@ -137,7 +142,7 @@ private HighlightField(Builder builder) {
137142
this.requireFieldMatch = builder.requireFieldMatch;
138143
this.tagsSchema = builder.tagsSchema;
139144
this.type = builder.type;
140-
145+
this.options = ApiTypeHelper.unmodifiable(builder.options);
141146
}
142147

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

317+
/**
318+
* API name: {@code options}
319+
*/
320+
@Nullable
321+
public final Map<String, JsonData> options() {
322+
return this.options;
323+
}
324+
312325
/**
313326
* Serialize this object to JSON.
314327
*/
@@ -436,6 +449,16 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
436449
generator.write(this.type);
437450

438451
}
452+
if (ApiTypeHelper.isDefined(this.options)) {
453+
generator.writeKey("options");
454+
generator.writeStartObject();
455+
for (Map.Entry<String, JsonData> item0 : this.options.entrySet()) {
456+
generator.writeKey(item0.getKey());
457+
item0.getValue().serialize(generator, mapper);
458+
459+
}
460+
generator.writeEnd();
461+
}
439462

440463
}
441464

@@ -509,6 +532,9 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<H
509532
@Nullable
510533
private String type;
511534

535+
@Nullable
536+
private Map<String, JsonData> options;
537+
512538
/**
513539
* API name: {@code boundary_chars}
514540
*/
@@ -728,6 +754,26 @@ public final Builder type(@Nullable HighlighterType value) {
728754
return this;
729755
}
730756

757+
/**
758+
* API name: {@code options}
759+
* <p>
760+
* Adds all entries of <code>map</code> to <code>options</code>.
761+
*/
762+
public final Builder options(@Nullable Map<String, JsonData> map) {
763+
this.options = _mapPutAll(this.options, map);
764+
return this;
765+
}
766+
767+
/**
768+
* API name: {@code options}
769+
* <p>
770+
* Adds an entry to <code>options</code>.
771+
*/
772+
public final Builder params(String key, JsonData value) {
773+
this.options = _mapPut(this.options, key, value);
774+
return this;
775+
}
776+
731777
/**
732778
* Builds a {@link HighlightField}.
733779
*
@@ -775,6 +821,7 @@ protected static void setupHighlightFieldDeserializer(ObjectDeserializer<Highlig
775821
op.add(Builder::requireFieldMatch, JsonpDeserializer.booleanDeserializer(), "require_field_match");
776822
op.add(Builder::tagsSchema, HighlighterTagsSchema._DESERIALIZER, "tags_schema");
777823
op.add(Builder::type, JsonpDeserializer.stringDeserializer(), "type");
824+
op.add(Builder::options, JsonpDeserializer.stringMapDeserializer(JsonData._DESERIALIZER), "options");
778825

779826
}
780827

java-client/src/test/java/co/elastic/clients/elasticsearch/spec_issues/SpecIssuesTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
import co.elastic.clients.elasticsearch.cluster.ClusterStatsResponse;
2525
import co.elastic.clients.elasticsearch.core.SearchRequest;
2626
import co.elastic.clients.elasticsearch.core.SearchResponse;
27+
import co.elastic.clients.elasticsearch.core.search.HighlightField;
2728
import co.elastic.clients.elasticsearch.model.ModelTestCase;
2829
import co.elastic.clients.json.JsonData;
2930
import co.elastic.clients.json.JsonpDeserializer;
3031
import jakarta.json.stream.JsonParser;
3132
import org.junit.Test;
3233

3334
import java.io.InputStream;
35+
import java.util.HashMap;
36+
import java.util.Map;
3437

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

107+
@Test
108+
public void i0168_highlightFieldOptions() {
109+
// https://github.com/elastic/elasticsearch-java/issues/168
110+
Map<String, JsonData> options = new HashMap<>();
111+
options.put("foo", JsonData.of("bar"));
112+
113+
HighlightField field = HighlightField.of(_0 -> _0.field("tst").options(options));
114+
115+
assertEquals("{\"field\":\"tst\",\"options\":{\"foo\":\"bar\"}}", toJson(field));
116+
}
117+
104118
private <T> T loadRsrc(String res, JsonpDeserializer<T> deser) {
105119
InputStream is = this.getClass().getResourceAsStream(res);
106120
assertNotNull("Resource not found: " + res, is);

0 commit comments

Comments
 (0)