---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- requires:
    capabilities:
    - method: "POST"
      path: "/_query"
      parameters:
      - "method"
      - "path"
      - "parameters"
      - "capabilities"
      capabilities:
      - "enrich_strict_range_types"
    reason: "Strict range type checking was added"
    test_runner_features:
    - "capabilities"
    - "allowed_warnings_regex"
    - "warnings_regex"
- do:
    indices.create:
      index: "decades"
      body:
        settings:
          index.number_of_shards: 1
          index.routing.rebalance.enable: "none"
        mappings:
          properties:
            decade:
              type: "integer"
            description:
              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: "decades"
      refresh: true
      body:
      - index: {}
      - decade: 1900
        description: "Gay Nineties"
      - index: {}
      - decade: 1910
        description: "Teens"
      - index: {}
      - decade: 1920
        description: "Roaring Twenties"
      - index: {}
      - decade: 1930
        description: "Dirty Thirties"
      - index: {}
      - decade: 1940
        description: "War Years"
      - index: {}
      - decade: 1950
        description: "Fabulous Fifties"
      - index: {}
      - decade: 1960
        description: "Swinging Sixties"
      - index: {}
      - decade: 1970
        description: "Me Decade"
      - index: {}
      - decade: 1980
        description: "Decade of Greed"
      - index: {}
      - decade: 1990
        description: "Nineties"
      - index: {}
      - decade: 2000
        description: "Aughts"
      - index: {}
      - decade: 2010
        description: "Digital Age"
      - index: {}
      - decade: 2020
        description: "Roaring Twenties 2.0"
    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:
    cluster.health:
      wait_for_no_initializing_shards: true
      wait_for_events: "languid"
    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: "decades-policy"
      body:
        match:
          indices:
          - "decades"
          match_field: "decade"
          enrich_fields:
          - "description"
    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: "decades-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: "songs"
      body:
        mappings:
          properties:
            title:
              type: "keyword"
            year:
              type: "integer"
            singer:
              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: "songs"
      refresh: true
      body:
      - index: {}
      - singer: "Louis Armstrong"
        title: "What a Wonderful World"
        year: 1967
      - index: {}
      - singer: "The Beatles"
        title: "Hey Jude"
        year: 1968
      - index: {}
      - singer: "Elvis Presley"
        title: "Jailhouse Rock"
        year: 1957
      - index: {}
      - singer: "Billie Holiday"
        title: "Strange Fruit"
        year: 1939
      - index: {}
      - singer: "Frank Sinatra"
        title: "Fly Me to the Moon"
        year: 1964
      - index: {}
      - singer: "Bob Dylan"
        title: "Blowin' in the Wind"
        year: 1963
      - index: {}
      - singer: "Queen"
        title: "Bohemian Rhapsody"
        year: 1975
      - index: {}
      - singer: "ABBA"
        title: "Dancing Queen"
        year: 1976
      - index: {}
      - singer: "Michael Jackson"
        title: "Thriller"
        year: 1982
      - index: {}
      - singer: "Nirvana"
        title: "Smells Like Teen Spirit"
        year: 1991
      - index: {}
      - singer: "Whitney Houston"
        title: "I Will Always Love You"
        year: 1992
      - index: {}
      - singer: "Aretha Franklin"
        title: "Respect"
        year: 1967
      - index: {}
      - singer: "Chuck Berry"
        title: "Johnny B. Goode"
        year: 1958
      - index: {}
      - singer: "Madonna"
        title: "Like a Prayer"
        year: 1989
      - index: {}
      - singer: "The Rolling Stones"
        title: "(I Can't Get No) Satisfaction"
        year: 1965
      - index: {}
      - singer: "Beyoncé"
        title: "Single Ladies (Put a Ring on It)"
        year: 2008
      - index: {}
      - singer: "Adele"
        title: "Rolling in the Deep"
        year: 2010
      - index: {}
      - singer: "Lady Gaga"
        title: "Bad Romance"
        year: 2009
      - index: {}
      - singer: "Billie Eilish"
        title: "Bad Guy"
        year: 2019
      - index: {}
      - singer: "Taylor Swift"
        title: "Anti-Hero"
        year: 2022
    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: "decades-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"
---
decades:
- 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 songs | EVAL decade = (10*FLOOR(year/10))::integer | ENRICH decades-policy\
          \ ON decade | STATS count=COUNT(*) BY description | SORT count DESC, description\
          \ ASC"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
- match:
    columns.0.name: "count"
- match:
    columns.0.type: "long"
- match:
    columns.1.name: "description"
- match:
    columns.1.type: "keyword"
- length:
    values: 9
- match:
    values.0:
    - 6
    - "Swinging Sixties"
- match:
    values.1:
    - 2
    - "Aughts"
- match:
    values.2:
    - 2
    - "Decade of Greed"
- match:
    values.3:
    - 2
    - "Digital Age"
- match:
    values.4:
    - 2
    - "Fabulous Fifties"
- match:
    values.5:
    - 2
    - "Me Decade"
- match:
    values.6:
    - 2
    - "Nineties"
- match:
    values.7:
    - 1
    - "Dirty Thirties"
- match:
    values.8:
    - 1
    - "Roaring Twenties 2.0"
---
decades as typecast keywords:
- 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 songs | EVAL decade = (10*FLOOR(year/10))::keyword | ENRICH decades-policy\
          \ ON decade | STATS count=COUNT(*) BY description | SORT count DESC, description\
          \ ASC"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
- match:
    columns.0.name: "count"
- match:
    columns.0.type: "long"
- match:
    columns.1.name: "description"
- match:
    columns.1.type: "keyword"
- length:
    values: 9
- match:
    values.0:
    - 6
    - "Swinging Sixties"
- match:
    values.1:
    - 2
    - "Aughts"
- match:
    values.2:
    - 2
    - "Decade of Greed"
- match:
    values.3:
    - 2
    - "Digital Age"
- match:
    values.4:
    - 2
    - "Fabulous Fifties"
- match:
    values.5:
    - 2
    - "Me Decade"
- match:
    values.6:
    - 2
    - "Nineties"
- match:
    values.7:
    - 1
    - "Dirty Thirties"
- match:
    values.8:
    - 1
    - "Roaring Twenties 2.0"
---
Invalid decade as keyword:
- 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 songs | ENRICH decades-policy ON singer | STATS count=COUNT(*)\
          \ BY description | SORT count DESC, description ASC"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
- match:
    columns.0.name: "count"
- match:
    columns.0.type: "long"
- match:
    columns.1.name: "description"
- match:
    columns.1.type: "keyword"
- length:
    values: 1
- match:
    values.0:
    - 20
    - null
---
Invalid decade as double:
- 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 songs | EVAL decade = 10.0*FLOOR(year/10) | ENRICH decades-policy\
          \ ON decade | STATS count=COUNT(*) BY description | SORT count DESC, description\
          \ ASC"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
- match:
    columns.0.name: "count"
- match:
    columns.0.type: "long"
- match:
    columns.1.name: "description"
- match:
    columns.1.type: "keyword"
- length:
    values: 1
- match:
    values.0:
    - 20
    - null
