4141import java .util .Arrays ;
4242import java .util .Collections ;
4343import java .util .Comparator ;
44+ import java .util .HashMap ;
4445import java .util .List ;
4546import java .util .Map ;
4647import java .util .Objects ;
@@ -95,6 +96,7 @@ public class DatafeedConfig implements ToXContentObject {
9596 PARSER .declareObject (Builder ::setIndicesOptions ,
9697 (p , c ) -> IndicesOptions .fromMap (p .map (), new IndicesOptions (IndicesOptions .Option .NONE , IndicesOptions .WildcardStates .NONE )),
9798 INDICES_OPTIONS );
99+ PARSER .declareObject (Builder ::setRuntimeMappings , (p , c ) -> p .map (), SearchSourceBuilder .RUNTIME_MAPPINGS_FIELD );
98100 }
99101
100102 private static BytesReference parseBytes (XContentParser parser ) throws IOException {
@@ -116,11 +118,12 @@ private static BytesReference parseBytes(XContentParser parser) throws IOExcepti
116118 private final DelayedDataCheckConfig delayedDataCheckConfig ;
117119 private final Integer maxEmptySearches ;
118120 private final IndicesOptions indicesOptions ;
121+ private final Map <String , Object > runtimeMappings ;
119122
120123 private DatafeedConfig (String id , String jobId , TimeValue queryDelay , TimeValue frequency , List <String > indices , BytesReference query ,
121124 BytesReference aggregations , List <SearchSourceBuilder .ScriptField > scriptFields , Integer scrollSize ,
122125 ChunkingConfig chunkingConfig , DelayedDataCheckConfig delayedDataCheckConfig ,
123- Integer maxEmptySearches , IndicesOptions indicesOptions ) {
126+ Integer maxEmptySearches , IndicesOptions indicesOptions , Map < String , Object > runtimeMappings ) {
124127 this .id = id ;
125128 this .jobId = jobId ;
126129 this .queryDelay = queryDelay ;
@@ -134,6 +137,7 @@ private DatafeedConfig(String id, String jobId, TimeValue queryDelay, TimeValue
134137 this .delayedDataCheckConfig = delayedDataCheckConfig ;
135138 this .maxEmptySearches = maxEmptySearches ;
136139 this .indicesOptions = indicesOptions ;
140+ this .runtimeMappings = Collections .unmodifiableMap (runtimeMappings );
137141 }
138142
139143 public String getId () {
@@ -188,6 +192,10 @@ public IndicesOptions getIndicesOptions() {
188192 return indicesOptions ;
189193 }
190194
195+ public Map <String , Object > getRuntimeMappings () {
196+ return runtimeMappings ;
197+ }
198+
191199 @ Override
192200 public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
193201 builder .startObject ();
@@ -232,6 +240,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
232240 indicesOptions .toXContent (builder , params );
233241 builder .endObject ();
234242 }
243+ if (runtimeMappings .isEmpty () == false ) {
244+ builder .field (SearchSourceBuilder .RUNTIME_MAPPINGS_FIELD .getPreferredName (), runtimeMappings );
245+ }
235246
236247 builder .endObject ();
237248 return builder ;
@@ -274,7 +285,8 @@ public boolean equals(Object other) {
274285 && Objects .equals (this .chunkingConfig , that .chunkingConfig )
275286 && Objects .equals (this .delayedDataCheckConfig , that .delayedDataCheckConfig )
276287 && Objects .equals (this .maxEmptySearches , that .maxEmptySearches )
277- && Objects .equals (this .indicesOptions , that .indicesOptions );
288+ && Objects .equals (this .indicesOptions , that .indicesOptions )
289+ && Objects .equals (this .runtimeMappings , that .runtimeMappings );
278290 }
279291
280292 /**
@@ -285,7 +297,7 @@ public boolean equals(Object other) {
285297 @ Override
286298 public int hashCode () {
287299 return Objects .hash (id , jobId , frequency , queryDelay , indices , asMap (query ), scrollSize , asMap (aggregations ), scriptFields ,
288- chunkingConfig , delayedDataCheckConfig , maxEmptySearches , indicesOptions );
300+ chunkingConfig , delayedDataCheckConfig , maxEmptySearches , indicesOptions , runtimeMappings );
289301 }
290302
291303 public static Builder builder (String id , String jobId ) {
@@ -294,8 +306,8 @@ public static Builder builder(String id, String jobId) {
294306
295307 public static class Builder {
296308
297- private String id ;
298- private String jobId ;
309+ private final String id ;
310+ private final String jobId ;
299311 private TimeValue queryDelay ;
300312 private TimeValue frequency ;
301313 private List <String > indices ;
@@ -307,6 +319,7 @@ public static class Builder {
307319 private DelayedDataCheckConfig delayedDataCheckConfig ;
308320 private Integer maxEmptySearches ;
309321 private IndicesOptions indicesOptions ;
322+ private Map <String , Object > runtimeMappings = Collections .emptyMap ();
310323
311324 public Builder (String id , String jobId ) {
312325 this .id = Objects .requireNonNull (id , ID .getPreferredName ());
@@ -327,6 +340,7 @@ public Builder(DatafeedConfig config) {
327340 this .delayedDataCheckConfig = config .getDelayedDataCheckConfig ();
328341 this .maxEmptySearches = config .getMaxEmptySearches ();
329342 this .indicesOptions = config .indicesOptions ;
343+ this .runtimeMappings = new HashMap <>(config .runtimeMappings );
330344 }
331345
332346 public Builder setIndices (List <String > indices ) {
@@ -419,9 +433,15 @@ public Builder setIndicesOptions(IndicesOptions indicesOptions) {
419433 return this ;
420434 }
421435
436+ public Builder setRuntimeMappings (Map <String , Object > runtimeMappings ) {
437+ this .runtimeMappings = Objects .requireNonNull (runtimeMappings ,
438+ SearchSourceBuilder .RUNTIME_MAPPINGS_FIELD .getPreferredName ());
439+ return this ;
440+ }
441+
422442 public DatafeedConfig build () {
423443 return new DatafeedConfig (id , jobId , queryDelay , frequency , indices , query , aggregations , scriptFields , scrollSize ,
424- chunkingConfig , delayedDataCheckConfig , maxEmptySearches , indicesOptions );
444+ chunkingConfig , delayedDataCheckConfig , maxEmptySearches , indicesOptions , runtimeMappings );
425445 }
426446
427447 private static BytesReference xContentToBytes (ToXContentObject object ) throws IOException {
0 commit comments