From dc674ea863d27db05ef82a9463d274934efd0d9a Mon Sep 17 00:00:00 2001
From: Quentin Pradet <quentin.pradet@elastic.co>
Date: Mon, 6 Nov 2023 14:05:21 +0400
Subject: [PATCH] [7.17] Allow single fields in fields and _source parameters
 (#2332)

(cherry picked from commit 9f40be8b64f80a8602c362aae25ff27be71e1b9a)
---
 output/schema/schema.json                     | 111 +++++++++++++-----
 output/typescript/types.ts                    |  10 +-
 specification/_global/msearch/types.ts        |   5 +-
 specification/_global/search/SearchRequest.ts |   7 +-
 .../_global/search/_types/SourceFilter.ts     |   4 +-
 5 files changed, 99 insertions(+), 38 deletions(-)

diff --git a/output/schema/schema.json b/output/schema/schema.json
index 59bb69f964..f490ef18fc 100644
--- a/output/schema/schema.json
+++ b/output/schema/schema.json
@@ -18709,7 +18709,7 @@
           }
         }
       ],
-      "specLocation": "_global/msearch/types.ts#L207-L210"
+      "specLocation": "_global/msearch/types.ts#L208-L211"
     },
     {
       "generics": [
@@ -18759,7 +18759,7 @@
           }
         }
       ],
-      "specLocation": "_global/msearch/types.ts#L197-L200"
+      "specLocation": "_global/msearch/types.ts#L198-L201"
     },
     {
       "kind": "interface",
@@ -18867,11 +18867,23 @@
           "type": {
             "kind": "array_of",
             "value": {
-              "kind": "instance_of",
-              "type": {
-                "name": "FieldAndFormat",
-                "namespace": "_types.query_dsl"
-              }
+              "items": [
+                {
+                  "kind": "instance_of",
+                  "type": {
+                    "name": "Field",
+                    "namespace": "_types"
+                  }
+                },
+                {
+                  "kind": "instance_of",
+                  "type": {
+                    "name": "FieldAndFormat",
+                    "namespace": "_types.query_dsl"
+                  }
+                }
+              ],
+              "kind": "union_of"
             }
           }
         },
@@ -19063,11 +19075,23 @@
           "type": {
             "kind": "array_of",
             "value": {
-              "kind": "instance_of",
-              "type": {
-                "name": "FieldAndFormat",
-                "namespace": "_types.query_dsl"
-              }
+              "items": [
+                {
+                  "kind": "instance_of",
+                  "type": {
+                    "name": "Field",
+                    "namespace": "_types"
+                  }
+                },
+                {
+                  "kind": "instance_of",
+                  "type": {
+                    "name": "FieldAndFormat",
+                    "namespace": "_types.query_dsl"
+                  }
+                }
+              ],
+              "kind": "union_of"
             }
           }
         },
@@ -19197,7 +19221,7 @@
           }
         }
       ],
-      "specLocation": "_global/msearch/types.ts#L70-L195"
+      "specLocation": "_global/msearch/types.ts#L71-L196"
     },
     {
       "description": "Contains parameters used to limit or change the subsequent search body request.",
@@ -19329,7 +19353,7 @@
           }
         }
       ],
-      "specLocation": "_global/msearch/types.ts#L52-L67"
+      "specLocation": "_global/msearch/types.ts#L53-L68"
     },
     {
       "attachedBehaviors": [
@@ -19551,7 +19575,7 @@
         "name": "RequestItem",
         "namespace": "_global.msearch"
       },
-      "specLocation": "_global/msearch/types.ts#L47-L50",
+      "specLocation": "_global/msearch/types.ts#L48-L51",
       "type": {
         "items": [
           {
@@ -19621,7 +19645,7 @@
         "name": "ResponseItem",
         "namespace": "_global.msearch"
       },
-      "specLocation": "_global/msearch/types.ts#L202-L205",
+      "specLocation": "_global/msearch/types.ts#L203-L206",
       "type": {
         "items": [
           {
@@ -22950,11 +22974,23 @@
             "type": {
               "kind": "array_of",
               "value": {
-                "kind": "instance_of",
-                "type": {
-                  "name": "FieldAndFormat",
-                  "namespace": "_types.query_dsl"
-                }
+                "items": [
+                  {
+                    "kind": "instance_of",
+                    "type": {
+                      "name": "Field",
+                      "namespace": "_types"
+                    }
+                  },
+                  {
+                    "kind": "instance_of",
+                    "type": {
+                      "name": "FieldAndFormat",
+                      "namespace": "_types.query_dsl"
+                    }
+                  }
+                ],
+                "kind": "union_of"
               }
             }
           },
@@ -23119,15 +23155,28 @@
             "type": {
               "kind": "array_of",
               "value": {
-                "kind": "instance_of",
-                "type": {
-                  "name": "FieldAndFormat",
-                  "namespace": "_types.query_dsl"
-                }
+                "items": [
+                  {
+                    "kind": "instance_of",
+                    "type": {
+                      "name": "FieldAndFormat",
+                      "namespace": "_types.query_dsl"
+                    }
+                  },
+                  {
+                    "kind": "instance_of",
+                    "type": {
+                      "name": "Field",
+                      "namespace": "_types"
+                    }
+                  }
+                ],
+                "kind": "union_of"
               }
             }
           },
           {
+            "description": "Defines a suggester that provides similar looking terms based on a provided text.",
             "name": "suggest",
             "required": false,
             "type": {
@@ -23828,7 +23877,7 @@
           }
         }
       ],
-      "specLocation": "_global/search/SearchRequest.ts#L51-L240"
+      "specLocation": "_global/search/SearchRequest.ts#L51-L243"
     },
     {
       "body": {
@@ -27784,6 +27833,7 @@
     {
       "codegenNames": [
         "fetch",
+        "fields",
         "filter"
       ],
       "description": "Defines how to fetch a source. Fetching can be disabled entirely, or the source can be filtered.",
@@ -27802,6 +27852,13 @@
               "namespace": "_builtins"
             }
           },
+          {
+            "kind": "instance_of",
+            "type": {
+              "name": "Fields",
+              "namespace": "_types"
+            }
+          },
           {
             "kind": "instance_of",
             "type": {
diff --git a/output/typescript/types.ts b/output/typescript/types.ts
index c352776067..d8773b239d 100644
--- a/output/typescript/types.ts
+++ b/output/typescript/types.ts
@@ -553,7 +553,7 @@ export interface MsearchMultisearchBody {
   explain?: boolean
   ext?: Record<string, any>
   stored_fields?: Fields
-  docvalue_fields?: (QueryDslFieldAndFormat | Field)[]
+  docvalue_fields?: (Field | QueryDslFieldAndFormat | Field)[]
   from?: integer
   highlight?: SearchHighlight
   indices_boost?: Record<IndexName, double>[]
@@ -566,7 +566,7 @@ export interface MsearchMultisearchBody {
   size?: integer
   sort?: Sort
   _source?: SearchSourceConfig
-  fields?: (QueryDslFieldAndFormat | Field)[]
+  fields?: (Field | QueryDslFieldAndFormat | Field)[]
   terminate_after?: long
   stats?: string[]
   timeout?: string
@@ -1018,7 +1018,7 @@ export interface SearchRequest extends RequestBase {
     highlight?: SearchHighlight
     track_total_hits?: SearchTrackHits
     indices_boost?: Record<IndexName, double>[]
-    docvalue_fields?: (QueryDslFieldAndFormat | Field)[]
+    docvalue_fields?: (Field | QueryDslFieldAndFormat | Field)[]
     min_score?: double
     post_filter?: QueryDslQueryContainer
     profile?: boolean
@@ -1030,7 +1030,7 @@ export interface SearchRequest extends RequestBase {
     slice?: SlicedScroll
     sort?: Sort
     _source?: SearchSourceConfig
-    fields?: (QueryDslFieldAndFormat | Field)[]
+    fields?: (QueryDslFieldAndFormat | Field | Field)[]
     suggest?: SearchSuggester
     terminate_after?: long
     timeout?: string
@@ -1455,7 +1455,7 @@ export interface SearchSmoothingModelContainer {
   stupid_backoff?: SearchStupidBackoffSmoothingModel
 }
 
-export type SearchSourceConfig = boolean | SearchSourceFilter | Fields
+export type SearchSourceConfig = boolean | Fields | SearchSourceFilter | Fields
 
 export type SearchSourceConfigParam = boolean | Fields
 
diff --git a/specification/_global/msearch/types.ts b/specification/_global/msearch/types.ts
index fe3bce08cf..41f2c4f556 100644
--- a/specification/_global/msearch/types.ts
+++ b/specification/_global/msearch/types.ts
@@ -23,6 +23,7 @@ import { Dictionary } from '@spec_utils/Dictionary'
 import { AggregationContainer } from '@_types/aggregations/AggregationContainer'
 import {
   ExpandWildcards,
+  Field,
   Fields,
   IndexName,
   Indices,
@@ -95,7 +96,7 @@ export class MultisearchBody {
    * Array of wildcard (*) patterns. The request returns doc values for field
    * names matching these patterns in the hits.fields property of the response.
    */
-  docvalue_fields?: FieldAndFormat[]
+  docvalue_fields?: Array<Field | FieldAndFormat>
   /**
    * Starting document offset. By default, you cannot page through more than 10,000
    * hits using the from and size parameters. To page through more hits, use the
@@ -139,7 +140,7 @@ export class MultisearchBody {
    * Array of wildcard (*) patterns. The request returns values for field names
    * matching these patterns in the hits.fields property of the response.
    */
-  fields?: Array<FieldAndFormat>
+  fields?: Array<Field | FieldAndFormat>
   /**
    * Maximum number of documents to collect for each shard. If a query reaches this
    * limit, Elasticsearch terminates the query early. Elasticsearch collects documents
diff --git a/specification/_global/search/SearchRequest.ts b/specification/_global/search/SearchRequest.ts
index 428d92bdba..bcabb28327 100644
--- a/specification/_global/search/SearchRequest.ts
+++ b/specification/_global/search/SearchRequest.ts
@@ -146,7 +146,7 @@ export interface Request extends RequestBase {
      * Array of wildcard (*) patterns. The request returns doc values for field
      * names matching these patterns in the hits.fields property of the response.
      */
-    docvalue_fields?: FieldAndFormat[]
+    docvalue_fields?: Array<Field | FieldAndFormat>
     /**
      * Minimum _score for matching documents. Documents with a lower _score are
      * not included in the search results.
@@ -183,7 +183,10 @@ export interface Request extends RequestBase {
      * Array of wildcard (*) patterns. The request returns values for field names
      * matching these patterns in the hits.fields property of the response.
      */
-    fields?: Array<FieldAndFormat>
+    fields?: Array<FieldAndFormat | Field>
+    /**
+     * Defines a suggester that provides similar looking terms based on a provided text.
+     */
     suggest?: Suggester
     /**
      * Maximum number of documents to collect for each shard. If a query reaches this
diff --git a/specification/_global/search/_types/SourceFilter.ts b/specification/_global/search/_types/SourceFilter.ts
index 6b48c59f79..b2b2c05071 100644
--- a/specification/_global/search/_types/SourceFilter.ts
+++ b/specification/_global/search/_types/SourceFilter.ts
@@ -32,9 +32,9 @@ export class SourceFilter {
 
 /**
  * Defines how to fetch a source. Fetching can be disabled entirely, or the source can be filtered.
- * @codegen_names fetch, filter
+ * @codegen_names fetch, fields, filter
  */
-export type SourceConfig = boolean | SourceFilter
+export type SourceConfig = boolean | Fields | SourceFilter
 
 /**
  * Defines how to fetch a source. Fetching can be disabled entirely, or the source can be filtered.