---
setup:
- skip:
    awaits_fix: "https://github.com/elastic/elasticsearch/issues/130740"
    features:
    - "headers"
    - "allowed_warnings_regex"
- requires:
    cluster_features:
    - "gte_v8.11.0"
    reason: "ESQL is available in 8.11+"
    test_runner_features: "allowed_warnings_regex"
- do:
    indices.create:
      index: "cities"
      body:
        settings:
          number_of_shards: 5
        mappings:
          properties:
            city_code:
              type: "keyword"
            city:
              type: "text"
            country:
              type: "keyword"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    bulk:
      index: "cities"
      refresh: true
      body:
      - index: {}
      - city_code: "nyc"
        city: "New York"
        country: "USA"
      - index: {}
      - city_code: "rom"
        city: "Rome"
        country: "Italy"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    enrich.put_policy:
      name: "city_codes)policy"
      body:
        match:
          indices:
          - "cities"
          match_field: "city_code"
          enrich_fields:
          - "city"
          - "country"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    enrich.execute_policy:
      name: "city_codes)policy"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    indices.create:
      index: "test"
      body:
        mappings:
          properties:
            name:
              type: "keyword"
            city_id:
              type: "keyword"
            city_name:
              type: "text"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    bulk:
      index: "test"
      refresh: true
      body:
      - index: {}
      - name: "Alice"
        city_id: "nyc"
        city_name: "New York"
      - index: {}
      - name: "Bob"
        city_id: "nyc"
        city_name: "New York"
      - index: {}
      - name: "Mario"
        city_id: "rom"
        city_name: "Rome"
      - index: {}
      - name: "Denise"
        city_id: "sgn"
        city_name: "Tan Son Nhat"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
teardown:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- do:
    enrich.delete_policy:
      name: "city_codes)policy"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Enrich on keyword:
- requires:
    test_runner_features:
    - "capabilities"
    - "allowed_warnings_regex"
    capabilities:
    - method: "POST"
      path: "/_query"
      parameters: []
      capabilities:
      - "no_brackets_in_unquoted_index_names"
    reason: "Change in the grammar"
- do:
    allowed_warnings_regex:
    - "No limit defined, adding default limit of \\[.*\\]"
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
    esql.query:
      body:
        query: "from test | enrich \"city_codes)policy\" on city_id | keep name, city,\
          \ country | sort name"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
- match:
    columns.0.name: "name"
- match:
    columns.0.type: "keyword"
- match:
    columns.1.name: "city"
- match:
    columns.1.type: "text"
- match:
    columns.2.name: "country"
- match:
    columns.2.type: "keyword"
- length:
    values: 4
- match:
    values.0:
    - "Alice"
    - "New York"
    - "USA"
- match:
    values.1:
    - "Bob"
    - "New York"
    - "USA"
- match:
    values.2:
    - "Denise"
    - null
    - null
- match:
    values.3:
    - "Mario"
    - "Rome"
    - "Italy"
