setup:
  - requires:
      cluster_features: [ "mapper.patterned_text" ]
      reason: "pattern_text mappings are used in this test"

  - do:
      indices.create:
        index:  test
        body:
          settings:
            number_of_shards: 1
            index.mapping.use_doc_values_skipper: true
            index.mode: logsdb
            sort.field: [ "foo.template_id" ]
            sort.order: [ desc ]
          mappings:
            properties:
              foo:
                type: pattern_text

  - do:
      bulk:
        index: test
        refresh: true
        body:
          - { "index": { "_id": "1" } }
          - { "@timestamp": "2025-07-17T00:00:00Z" }
          - { "index": { "_id": "2" } }
          - { "@timestamp": "2025-07-17T00:00:01Z", "foo": "Found 5 errors for service [cheddar1]" }
          - { "index": { "_id": "3" } }
          - { "@timestamp": "2025-07-17T00:00:02Z", "foo": "[2020-08-18T00:58:56] Found 123 errors for service [cheddar1]" }
          - { "index": { "_id": "4" } }
          - { "@timestamp": "2025-07-17T00:00:03Z", "foo": "Found some errors for cheddar data service" }
          - { "index": { "_id": "5" } }
          - { "@timestamp": "2025-07-17T00:00:04Z", "foo": "Found 123 errors for service [gorgonzola-24]" }

---
Field caps:

  - do:
      field_caps:
        index: test
        fields: [ foo.template_id ]
  - match: { fields: {"foo.template_id": {"keyword": {"type": "keyword", "metadata_field": false, "searchable": true, "aggregatable": true}}}}

---
Get template_id field:

  - do:
      search:
        index: test
        body:
          docvalue_fields: [ "foo.template_id" ]
          query:
            ids:
              values: ["2"]

  - match: { hits.total.value: 1 }
  - match: { hits.hits.0.fields: {"foo.template_id": ["mOVsnxlxdac"]} }

---
Sort by template_id descending from index config setting:
  - do:
      search:
        index: test
        body:
          docvalue_fields: [ "foo.template_id" ]
          query: { match_all: {} }

  - match: { hits.total.value: 5 }
  - match: { hits.hits.0.fields: {"foo.template_id": ["mOVsnxlxdac"]} }
  - match: { hits.hits.0._id: "2" }
  - match: { hits.hits.1.fields: {"foo.template_id": ["mOVsnxlxdac"]} }
  - match: { hits.hits.1._id: "5" }
  - match: { hits.hits.2.fields: {"foo.template_id": ["k-2qtjujOCw"]} }
  - match: { hits.hits.2._id: "4" }
  - match: { hits.hits.3.fields: {"foo.template_id": ["1l_PtCLQ5xY"]} }
  - match: { hits.hits.3._id: "3" }
  - match: { hits.hits.4.fields: null }
  - match: { hits.hits.4._id: "1" }

---
Sort by template_id ascending from request:

  - do:
      search:
        index: test
        body:
          docvalue_fields: [ "foo.template_id" ]
          sort: "foo.template_id"
          query: { match_all: {} }

  - match: { hits.total.value: 5 }
  - match: { hits.hits.0.fields: {"foo.template_id": ["1l_PtCLQ5xY"]} }
  - match: { hits.hits.0._id: "3" }
  - match: { hits.hits.1.fields: {"foo.template_id": ["k-2qtjujOCw"]} }
  - match: { hits.hits.1._id: "4" }
  - match: { hits.hits.2.fields: {"foo.template_id": ["mOVsnxlxdac"]} }
  - match: { hits.hits.2._id: "2" }
  - match: { hits.hits.3.fields: {"foo.template_id": ["mOVsnxlxdac"]} }
  - match: { hits.hits.3._id: "5" }
  - match: { hits.hits.4.fields: null }
  - match: { hits.hits.4._id: "1" }

---
Exist query:

  - do:
      search:
        index: test
        body:
          query:
            exists:
              field: foo.template_id

  - match: { hits.total.value: 4 }
  - match: { hits.hits.0._score: 1.0 }

---
Match query:

  - do:
      search:
        index: test
        body:
          query:
            match:
              foo.template_id: "mOVsnxlxdac"

  - match: { hits.total.value: 2 }
  - match: { hits.hits.0._score: 1.0 }

---
Range query:

  - do:
      search:
        index: test
        body:
          query:
            range:
              foo.template_id:
                lt: "l"
  # one doc has a null value for template_id and two start with m > l
  - match: { hits.total.value: 2 }
  - match: { hits.hits.0._id: "4" }
  - match: { hits.hits.1._id: "3" }

---
Term aggregation:
  - do:
      search:
        index: test
        body:
          size: 0
          aggs:
            template_id_agg:
              terms:
                field: "foo.template_id"
                "order": { "_key": "asc" }

  - match: { hits.total.value: 5 }
  - length: { aggregations.template_id_agg.buckets: 3 }
  - match: { aggregations.template_id_agg.buckets.0.key: "1l_PtCLQ5xY" }
  - match: { aggregations.template_id_agg.buckets.0.doc_count: 1 }
  - match: { aggregations.template_id_agg.buckets.1.key: "k-2qtjujOCw" }
  - match: { aggregations.template_id_agg.buckets.1.doc_count: 1 }
  - match: { aggregations.template_id_agg.buckets.2.key: "mOVsnxlxdac" }
  - match: { aggregations.template_id_agg.buckets.2.doc_count: 2 }


