---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- requires:
    cluster_features:
    - "mapper.source.mode_from_index_setting"
    reason: "Source mode configured through index setting"
- do:
    cluster.health:
      wait_for_status: "yellow"
    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"
---
Filter on single field:
- do:
    indices.create:
      index: "index_dls"
      body:
        settings:
          index:
            mapping.source.mode: "synthetic"
        mappings:
          properties:
            name:
              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: "index_dls"
      refresh: true
      body:
      - "{\"create\": { \"_id\": \"1a\" }}"
      - "{\"name\": \"A\", \"type\": \"foo\"}"
      - "{\"create\": { \"_id\": \"2a\" }}"
      - "{\"name\": \"B\", \"type\": \"bar\"}"
    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"
- match:
    errors: false
- do:
    security.create_api_key:
      body:
        name: "test-fls"
        expiration: "1d"
        role_descriptors:
          index_access:
            indices:
            - names:
              - "index_dls"
              privileges:
              - "read"
              query:
                match:
                  name: "A"
    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"
- match:
    name: "test-fls"
- is_true: "id"
- set:
    id: "api_key_id"
    encoded: "credentials"
- do:
    search:
      index: "index_dls"
      sort: "name"
    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"
- match:
    hits.total.value: 2
- match:
    hits.hits.0._id: "1a"
- match:
    hits.hits.0._source.name: "A"
- match:
    hits.hits.0._source.type: "foo"
- match:
    hits.hits.1._id: "2a"
- match:
    hits.hits.1._source.name: "B"
- match:
    hits.hits.1._source.type: "bar"
- do:
    get:
      index: "index_dls"
      id: "2a"
    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"
- match:
    _source.name: "B"
- match:
    _source.type: "bar"
- do:
    headers:
      Authorization: "ApiKey ${credentials}"
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    search:
      index: "index_dls"
    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"
- match:
    hits.total.value: 1
- match:
    hits.hits.0._id: "1a"
- match:
    hits.hits.0._source.name: "A"
- match:
    hits.hits.0._source.type: "foo"
- do:
    catch: "missing"
    headers:
      Authorization: "ApiKey ${credentials}"
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    get:
      index: "index_dls"
      id: "2a"
    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"
---
Filter on nested field:
- do:
    indices.create:
      index: "index_dls"
      body:
        settings:
          index:
            mapping.source.mode: "synthetic"
        mappings:
          properties:
            name:
              type: "keyword"
            object:
              type: "nested"
              properties:
                secret:
                  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: "index_dls"
      refresh: true
      body:
      - "{\"create\": { }}"
      - "{\"name\": \"A\", \"object\": [ { \"secret\": \"mission\" }, { \"secret\"\
        : \"nomatch\" } ] }"
      - "{\"create\": { }}"
      - "{\"name\": \"B\", \"object\": { \"secret\": \"mission\", \"public\": \"interest\"\
        \ } }"
      - "{\"create\": { }}"
      - "{\"name\": \"C\", \"object\": { \"foo\": \"bar\" } }"
    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"
- match:
    errors: false
- do:
    security.create_api_key:
      body:
        name: "test-fls"
        expiration: "1d"
        role_descriptors:
          index_access:
            indices:
            - names:
              - "index_dls"
              privileges:
              - "read"
              query:
                nested:
                  path: "object"
                  query:
                    term:
                      object.secret: "mission"
    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"
- match:
    name: "test-fls"
- is_true: "id"
- set:
    id: "api_key_id"
    encoded: "credentials"
- do:
    search:
      index: "index_dls"
      sort: "name"
    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"
- match:
    hits.total.value: 3
- match:
    hits.hits.0._source.name: "A"
- match:
    hits.hits.0._source.object.0.secret: "mission"
- match:
    hits.hits.0._source.object.1.secret: "nomatch"
- match:
    hits.hits.1._source.name: "B"
- match:
    hits.hits.1._source.object.secret: "mission"
- match:
    hits.hits.1._source.object.public: "interest"
- match:
    hits.hits.2._source.name: "C"
- match:
    hits.hits.2._source.object.foo: "bar"
- do:
    headers:
      Authorization: "ApiKey ${credentials}"
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    search:
      index: "index_dls"
      sort: "name"
    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"
- match:
    hits.total.value: 2
- match:
    hits.hits.0._source.name: "A"
- match:
    hits.hits.0._source.object.0.secret: "mission"
- match:
    hits.hits.0._source.object.1.secret: "nomatch"
- match:
    hits.hits.1._source.name: "B"
- match:
    hits.hits.1._source.object.secret: "mission"
- match:
    hits.hits.1._source.object.public: "interest"
---
Filter on object with stored source:
- do:
    indices.create:
      index: "index_dls"
      body:
        settings:
          index:
            mapping.source.mode: "synthetic"
        mappings:
          properties:
            name:
              type: "keyword"
            obj:
              type: "object"
              synthetic_source_keep: "arrays"
              properties:
                secret:
                  type: "keyword"
          runtime:
            secret:
              type: "keyword"
              script:
                source: "emit(params._source.obj.0.secret)"
    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: "index_dls"
      refresh: true
      body:
      - "{\"create\": { }}"
      - "{\"name\": \"A\", \"obj\": [ { \"secret\": \"mission\" }, { \"foo\": \"bar\"\
        \ } ] }"
      - "{\"create\": { }}"
      - "{\"name\": \"B\", \"obj\": [ { \"secret\": \"common\" }, {\"foo\": \"baz\"\
        } ] }"
    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"
- match:
    errors: false
- do:
    security.create_api_key:
      body:
        name: "test-fls"
        expiration: "1d"
        role_descriptors:
          index_access:
            indices:
            - names:
              - "index_dls"
              privileges:
              - "read"
              query:
                term:
                  secret: "mission"
    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"
- match:
    name: "test-fls"
- is_true: "id"
- set:
    id: "api_key_id"
    encoded: "credentials"
- do:
    search:
      index: "index_dls"
      sort: "name"
    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"
- match:
    hits.total.value: 2
- match:
    hits.hits.0._source.name: "A"
- match:
    hits.hits.0._source.obj.0.secret: "mission"
- match:
    hits.hits.0._source.obj.1.foo: "bar"
- match:
    hits.hits.1._source.name: "B"
- match:
    hits.hits.1._source.obj.0.secret: "common"
- match:
    hits.hits.1._source.obj.1.foo: "baz"
- do:
    headers:
      Authorization: "ApiKey ${credentials}"
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    search:
      index: "index_dls"
      sort: "name"
    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"
- match:
    hits.total.value: 1
- match:
    hits.hits.0._source.name: "A"
- match:
    hits.hits.0._source.obj.0.secret: "mission"
- match:
    hits.hits.0._source.obj.1.foo: "bar"
---
Filter on field within a disabled object:
- do:
    indices.create:
      index: "index_dls"
      body:
        settings:
          index:
            mapping.source.mode: "synthetic"
        mappings:
          properties:
            name:
              type: "keyword"
            object:
              type: "object"
              enabled: false
          runtime:
            secret:
              type: "keyword"
              script:
                source: "emit(params._source.object.secret)"
    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: "index_dls"
      refresh: true
      body:
      - "{\"create\": { }}"
      - "{\"name\": \"A\", \"object\": { \"secret\":\"mission\", \"public\":\"interest\"\
        \ } }"
      - "{\"create\": { }}"
      - "{\"name\": \"B\", \"object\": { \"secret\":\"common\", \"foo\":\"bar\" }\
        \ }"
    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"
- match:
    errors: false
- do:
    security.create_api_key:
      body:
        name: "test-fls"
        expiration: "1d"
        role_descriptors:
          index_access:
            indices:
            - names:
              - "index_dls"
              privileges:
              - "read"
              - "monitor"
              query:
                term:
                  secret: "mission"
    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"
- match:
    name: "test-fls"
- is_true: "id"
- set:
    id: "api_key_id"
    encoded: "credentials"
- do:
    search:
      index: "index_dls"
      sort: "name"
    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"
- match:
    hits.total.value: 2
- match:
    hits.hits.0._source.name: "A"
- match:
    hits.hits.0._source.object.secret: "mission"
- match:
    hits.hits.0._source.object.public: "interest"
- match:
    hits.hits.1._source.name: "B"
- match:
    hits.hits.1._source.object.secret: "common"
- match:
    hits.hits.1._source.object.foo: "bar"
- do:
    headers:
      Authorization: "ApiKey ${credentials}"
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    search:
      index: "index_dls"
      sort: "name"
    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"
- match:
    hits.total.value: 1
- match:
    hits.hits.0._source.name: "A"
- match:
    hits.hits.0._source.object.secret: "mission"
- match:
    hits.hits.0._source.object.public: "interest"
---
Filter on field with ignored_malformed:
- do:
    indices.create:
      index: "index_dls"
      body:
        settings:
          index:
            mapping.source.mode: "synthetic"
        mappings:
          properties:
            name:
              type: "keyword"
            secret:
              type: "integer"
              ignore_malformed: true
          runtime:
            rt_secret:
              type: "keyword"
              script:
                source: "emit(params._source.secret)"
    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: "index_dls"
      refresh: true
      body:
      - "{\"create\": { }}"
      - "{\"name\": \"A\", \"secret\": \"mission\"}"
      - "{\"create\": { }}"
      - "{\"name\": \"B\", \"secret\": \"top\" }"
    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"
- match:
    errors: false
- do:
    security.create_api_key:
      body:
        name: "test-fls"
        expiration: "1d"
        role_descriptors:
          index_access:
            indices:
            - names:
              - "index_dls"
              privileges:
              - "read"
              query:
                term:
                  rt_secret: "mission"
    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"
- match:
    name: "test-fls"
- is_true: "id"
- set:
    id: "api_key_id"
    encoded: "credentials"
- do:
    search:
      index: "index_dls"
      sort: "name"
    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"
- match:
    hits.total.value: 2
- match:
    hits.hits.0._source.name: "A"
- match:
    hits.hits.0._source.secret: "mission"
- match:
    hits.hits.1._source.name: "B"
- match:
    hits.hits.1._source.secret: "top"
- do:
    headers:
      Authorization: "ApiKey ${credentials}"
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    search:
      index: "index_dls"
      sort: "name"
    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"
- match:
    hits.total.value: 1
- match:
    hits.hits.0._source.name: "A"
- match:
    hits.hits.0._source.secret: "mission"
