---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- do:
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    indices.create:
      index: "to_categorize"
      body:
        mappings:
          properties:
            kind:
              type: "keyword"
            text:
              type: "text"
            value:
              type: "long"
    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:
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    bulk:
      index: "to_categorize"
      refresh: true
      body: "{\"index\": {}}\n{\"product\": \"server\",\"text\": \"Node 2 stopping\"\
        , \"value\": 1}\n{\"index\": {}}\n{\"product\": \"server\", \"text\": \"Node\
        \ 2 starting\", \"value\": 1}\n{\"index\": {}}\n{\"product\": \"server\",\
        \ \"text\": \"Node 4 stopping\", \"value\": 1}\n{\"index\": {}}\n{\"product\"\
        : \"server\", \"text\": \"Node 5 stopping\", \"value\": 1}\n{\"index\": {}}\n\
        {\"product\": \"user\", \"text\": \"User Foo logging on\", \"value\": 1}\n\
        {\"index\": {}}\n{\"product\": \"user\", \"text\": \"User Foo logging on\"\
        , \"value\": 1}\n{\"index\": {}}\n{\"product\": \"user\", \"text\": \"User\
        \ Foo logging off\", \"value\": 1}\n"
    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"
---
Test categorization agg simple:
- do:
    search:
      index: "to_categorize"
      body: "{\n  \"size\": 0,\n  \"aggs\": {\n    \"categories\": {\n      \"categorize_text\"\
        : {\n        \"field\": \"text\"\n      }\n    }\n  }\n}\n"
    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"
- length:
    aggregations.categories.buckets: 4
- match:
    aggregations.categories.buckets.0.doc_count: 3
- match:
    aggregations.categories.buckets.0.key: "Node stopping"
- do:
    search:
      index: "to_categorize"
      body: "{\n  \"size\": 0,\n  \"aggs\": {\n    \"categories\": {\n      \"categorize_text\"\
        : {\n        \"field\": \"text\",\n        \"size\": 10,\n        \"similarity_threshold\"\
        : 11\n      }\n    }\n  }\n}\n"
    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"
- length:
    aggregations.categories.buckets: 2
- match:
    aggregations.categories.buckets.0.doc_count: 4
- match:
    aggregations.categories.buckets.0.key: "Node"
- match:
    aggregations.categories.buckets.0.regex: ".*?Node.*?"
- match:
    aggregations.categories.buckets.0.max_matching_length: 16
- match:
    aggregations.categories.buckets.1.doc_count: 3
- match:
    aggregations.categories.buckets.1.key: "User Foo logging"
- match:
    aggregations.categories.buckets.1.regex: ".*?User.+?Foo.+?logging.*?"
- match:
    aggregations.categories.buckets.1.max_matching_length: 22
---
Test categorization aggregation against unsupported field:
- do:
    catch: "/categorize_text agg \\[categories\\] only works on text and keyword fields/"
    search:
      index: "to_categorize"
      body: "{\n  \"size\": 0,\n  \"aggs\": {\n    \"categories\": {\n      \"categorize_text\"\
        : {\n        \"field\": \"value\"\n      }\n    }\n  }\n}\n"
    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"
---
Test categorization aggregation with poor settings:
- do:
    catch: "/\\[similarity_threshold\\] must be in the range \\[1, 100\\]/"
    search:
      index: "to_categorize"
      body: "{\n  \"size\": 0,\n  \"aggs\": {\n    \"categories\": {\n      \"categorize_text\"\
        : {\n        \"field\": \"text\",\n        \"similarity_threshold\": 0\n \
        \     }\n    }\n  }\n}\n"
    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:
    catch: "/\\[categorization_filters\\] cannot be used with \\[categorization_analyzer\\\
      ]/"
    search:
      index: "to_categorize"
      body: "{\n  \"size\": 0,\n  \"aggs\": {\n    \"categories\": {\n      \"categorize_text\"\
        : {\n        \"field\": \"text\",\n        \"categorization_filters\": [\"\
        foo\"],\n        \"categorization_analyzer\": \"english\"\n      }\n    }\n\
        \  }\n}\n"
    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:
    catch: "/\\[categorization_filters\\] cannot be used with \\[categorization_analyzer\\\
      ]/"
    search:
      index: "to_categorize"
      body: "{\n  \"size\": 0,\n  \"aggs\": {\n    \"categories\": {\n      \"categorize_text\"\
        : {\n        \"field\": \"text\",\n        \"categorization_analyzer\": \"\
        english\",\n        \"categorization_filters\": [\"foo\"]\n      }\n    }\n\
        \  }\n}\n"
    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"
