---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- do:
    index:
      index: "ip_locations"
      id: "192.168.1.1"
      body:
        country: "Canada"
        city: "Montreal"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    index:
      index: "ip_locations"
      id: "192.168.1.3"
      body:
        country: "Canada"
        city: "Toronto"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    index:
      index: "logs"
      body:
        ip: "192.168.1.1"
        msg: "The first message"
        ord: 1
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    index:
      index: "logs"
      body:
        ip: "192.168.1.2"
        msg: "The second message"
        ord: 2
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    indices.refresh:
      index: "ip_locations,logs"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
---
Retrieve lookup fields:
- requires:
    cluster_features:
    - "gte_v8.2.0"
    reason: "Lookup fields are introduced in 8.2"
- do:
    search:
      index: "logs"
      body:
        runtime_mappings:
          location:
            type: "lookup"
            target_index: "ip_locations"
            input_field: "ip"
            target_field: "_id"
            fetch_fields:
            - "city"
            - "country"
        sort: "ord"
        fields:
        - "msg"
        - "location"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- match:
    hits.hits.0.fields.msg:
    - "The first message"
- match:
    hits.hits.0.fields.location:
    - country:
      - "Canada"
      city:
      - "Montreal"
- match:
    hits.hits.1.fields.msg:
    - "The second message"
- match:
    hits.hits.1.fields.location: null
---
Fails to query or aggregate on lookup fields:
- requires:
    cluster_features:
    - "gte_v8.2.0"
    reason: "Lookup fields are introduced in 8.2"
- do:
    catch: "/Field \\[location\\] of type \\[lookup\\] does not support match queries/"
    search:
      index: "logs"
      body:
        runtime_mappings:
          location:
            type: "lookup"
            target_index: "ip_locations"
            input_field: "ip"
            target_field: "_id"
            fetch_fields:
            - "city"
        query:
          match:
            location: "montreal"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
- do:
    catch: "/Fielddata is not supported on field \\[location\\] of type \\[lookup\\\
      ]/"
    search:
      index: "logs"
      body:
        runtime_mappings:
          location:
            type: "lookup"
            target_index: "ip_locations"
            input_field: "ip"
            target_field: "_id"
            fetch_fields:
            - "city"
        aggs:
          locations:
            terms:
              field: "location"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - "Use of the \\[max_size\\] rollover condition has been deprecated in favour\
      \ of the \\[max_primary_shard_size\\] condition and will be removed in a later\
      \ version"
    - "Parameter \\[confidence_interval\\] in \\[index_options\\] for dense_vector\
      \ field \\[[^\\]]+\\] is deprecated and will be removed in a future version"
