setup:

  - requires:
      cluster_features: ["gte_v7.14.0"]
      reason: "match_only_text was added in 7.14"

  - do:
      indices.create:
        index:  test
        body:
          mappings:
            properties:
              foo:
                type: match_only_text

  - do:
      index:
        index: test
        id:    "1"
        body:  {}

  - do:
      index:
        index: test
        id:    "2"
        body:  { "foo": "Apache Lucene powers Elasticsearch" }

  - do:
      index:
        index: test
        id:    "3"
        body:  { "foo": "Elasticsearch is based on Apache Lucene" }

  - do:
      index:
        index: test
        id:    "4"
        body:  { "foo": "The Apache Software Foundation manages many projects including Lucene" }

  - do:
      indices.refresh: {}

---
"Field caps":

  - do:
      field_caps:
        index: test
        fields: [ foo ]

  - match: { fields.foo.text.searchable: true }
  - match: { fields.foo.text.aggregatable: false }

---
"Exist query":

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

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

---
"Match query":

  - do:
      search:
        index: test
        body:
          query:
            match:
              foo: powers

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

---
"Match Phrase query":

  - do:
      search:
        index: test
        body:
          query:
            match_phrase:
              foo: "lucene powers"

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

---
"Match Phrase Prefix query":

  - do:
      search:
        index: test
        body:
          query:
            match_phrase_prefix:
              foo: "lucene pow"

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

---
"Query String query with phrase":

  - do:
      search:
        index: test
        body:
          query:
            query_string:
              query: '"lucene powers"'
              default_field: "foo"

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



---
"Regexp query":

  - do:
      search:
        index: test
        body:
          query:
            regexp:
              foo: "lu.*ne"

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

---
"Wildcard query":

  - do:
      search:
        index: test
        body:
          query:
            wildcard:
              foo: "lu*ne"

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

---
"Prefix query":

  - do:
      search:
        index: test
        body:
          query:
            prefix:
              foo: "luc"

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

---
"Fuzzy query":

  - do:
      search:
        index: test
        body:
          query:
            fuzzy:
              foo: "lucane"

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

---
"Span query":

  - do:
      catch: bad_request
      search:
        index: test
        body:
          query:
            span_term:
              foo: lucene

---
"Term intervals query":

  - do:
      search:
        index: test
        body:
          query:
            intervals:
              foo:
                match:
                  query: "apache lucene"
                  max_gaps: 1

  - match: { "hits.total.value": 2 }

---
"Prefix intervals query":

  - do:
      search:
        index: test
        body:
          query:
            intervals:
              foo:
                prefix:
                  prefix: "luc"

  - match: { "hits.total.value": 3 }

---
"Wildcard intervals query":

  - do:
      search:
        index: test
        body:
          query:
            intervals:
              foo:
                wildcard:
                  pattern: "*ase*"

  - match: { "hits.total.value": 1 }

---
"Fuzzy intervals query":

  - do:
      search:
        index: test
        body:
          query:
            intervals:
              foo:
                fuzzy:
                  term: "lucane"

  - match: { "hits.total.value": 3 }

---
"Wildcard highlighting":

  - requires:
      cluster_features: ["gte_v8.3.0"]
      reason: "Wildcard highlighting on match_only_text was fixed in 8.3"

  - do:
      search:
        index: test
        body:
          query:
            match:
              foo: "many"
          highlight:
            fields:
              "*": {}

  - match: { hits.total.value: 1 }
  - match: { hits.hits.0._source.foo: "The Apache Software Foundation manages many projects including Lucene" }
  - match: { hits.hits.0.highlight.foo.0: "The Apache Software Foundation manages <em>many</em> projects including Lucene" }

---
synthetic_source:
  - requires:
      cluster_features: [ "mapper.source.mode_from_index_setting" ]
      reason: "Source mode configured through index setting"

  - do:
      indices.create:
        index: synthetic_source_test
        body:
          settings:
            index:
              mapping.source.mode: synthetic
          mappings:
            properties:
              foo:
                type: match_only_text

  - do:
      index:
        index: synthetic_source_test
        id:    "1"
        refresh: true
        body:
          foo: "Apache Lucene powers Elasticsearch"

  - do:
      search:
        index: synthetic_source_test
  - match: { "hits.total.value": 1 }
  - match:
      hits.hits.0._source:
        foo: "Apache Lucene powers Elasticsearch"

---
tsdb:
  - requires:
      cluster_features: ["gte_v8.10.0"]
      reason: bug fixed in 8.10.0

  - do:
      indices.create:
        index: tsdb_test
        body:
          settings:
            index:
              mode: time_series
              routing_path: [ dimension ]
              time_series:
                start_time: 2000-01-01T00:00:00Z
                end_time: 2099-12-31T23:59:59Z
          mappings:
            properties:
              dimension:
                type: keyword
                time_series_dimension: true
              foo:
                type: match_only_text

  - do:
      index:
        index: tsdb_test
        refresh: true
        body:
          "@timestamp": "2000-01-01T00:00:00Z"
          dimension: "a"
          foo: "Apache Lucene powers Elasticsearch"

  - do:
      search:
        index: tsdb_test
  - match: { "hits.total.value": 1 }
  - match:
      hits.hits.0._source:
        "@timestamp" : "2000-01-01T00:00:00.000Z"
        "dimension" : "a"
        foo: "Apache Lucene powers Elasticsearch"

---
synthetic_source with copy_to:
  - requires:
      cluster_features: [ "mapper.source.mode_from_index_setting" ]
      reason: "Source mode configured through index setting"

  - do:
      indices.create:
        index: synthetic_source_test
        body:
          settings:
            index:
              mapping.source.mode: synthetic
          mappings:
            properties:
              foo:
                type: match_only_text
                copy_to: copy
              copy:
                type: keyword

  - do:
      index:
        index: synthetic_source_test
        id:    "1"
        refresh: true
        body:
          foo: "Apache Lucene powers Elasticsearch"

  - do:
      search:
        index: synthetic_source_test
        body:
          fields: ["copy"]

  - match: { "hits.total.value": 1 }
  - match:
      hits.hits.0._source.foo: "Apache Lucene powers Elasticsearch"
  - match:
      hits.hits.0.fields.copy.0: "Apache Lucene powers Elasticsearch"


