---
setup:
  - do:
      indices.create:
        index: locations
        body:
          settings:
            number_of_shards: 1
            number_of_replicas: 0
          mappings:
            runtime:
              location_from_doc_value:
                type: geo_point
                script:
                  source: |
                    emit(doc["location"].lat, doc["location"].lon);
              location_from_source:
                type: geo_point
                script:
                  source: |
                    emit(params._source.location.lat, params._source.location.lon);
            properties:
              timestamp:
                type: date
              location:
                type: geo_point
  - do:
      bulk:
        index: locations
        refresh: true
        body: |
          {"index":{}}
          {"timestamp": "1998-04-30T14:30:17-05:00", "location" : {"lat": 13.5, "lon" : 34.89}}
          {"index":{}}
          {"timestamp": "1998-04-30T14:30:53-05:00", "location" : {"lat": -7.9, "lon" : 120.78}}
          {"index":{}}
          {"timestamp": "1998-04-30T14:31:12-05:00", "location" : {"lat": 45.78, "lon" : -173.45}}
          {"index":{}}
          {"timestamp": "1998-04-30T14:31:19-05:00", "location" : {"lat": 32.45, "lon" : 45.6}}
          {"index":{}}
          {"timestamp": "1998-04-30T14:31:22-05:00", "location" : {"lat": -63.24, "lon" : 31.0}}
          {"index":{}}
          {"timestamp": "1998-04-30T14:31:27-05:00", "location" : {"lat": 0.0, "lon" : 0.0}}


---
"get mapping":
  - do:
      indices.get_mapping:
        index: locations
  - match: {locations.mappings.runtime.location_from_source.type: geo_point }
  - match:
      locations.mappings.runtime.location_from_source.script.source: |
        emit(params._source.location.lat, params._source.location.lon);
  - match: {locations.mappings.runtime.location_from_source.script.lang: painless }

---
"fetch fields from source":
  - do:
      search:
        index: locations
        body:
          sort: timestamp
          fields: [location, location_from_doc_value, location_from_source]
  - match: {hits.total.value: 6}
  - match: {hits.hits.0.fields.location.0.type: "Point" }
  - match: {hits.hits.0.fields.location.0.coordinates: [34.89, 13.5] }
  - match: {hits.hits.0.fields.location_from_doc_value: [{type: Point, coordinates: [34.889999935403466, 13.499999991618097]}] }
  - match: {hits.hits.0.fields.location_from_source: [{type: Point, coordinates: [34.89, 13.5]}] }

---
"exists query":
  - do:
      search:
        index: locations
        body:
          query:
            exists:
              field: location_from_source
  - match: {hits.total.value: 6}

---
"geo bounding box query":
  - do:
      search:
        index: locations
        body:
          query:
            geo_bounding_box:
              location_from_source:
                top_left:
                  lat: 10
                  lon: -10
                bottom_right:
                  lat: -10
                  lon: 10
  - match: {hits.total.value: 1}

---
"geo shape query":
  - do:
      search:
        index: locations
        body:
          query:
            geo_shape:
              location_from_source:
                shape:
                  type: "envelope"
                  coordinates: [ [ -10, 10 ], [ 10, -10 ] ]
  - match: {hits.total.value: 1}

---
"geo distance query":
  - do:
      search:
        index: locations
        body:
          query:
            geo_distance:
              distance: "2000km"
              location_from_source:
                  lat: 0
                  lon: 0
  - match: {hits.total.value: 1}

---
"bounds agg":
  - do:
      search:
        index: locations
        body:
          aggs:
            bounds:
              geo_bounds:
                field: "location"
                wrap_longitude: false
            bounds_from_doc_value:
              geo_bounds:
                 field: "location_from_doc_value"
                 wrap_longitude: false
            bounds_from_source:
              geo_bounds:
                 field: "location_from_source"
                 wrap_longitude: false
  - match: {hits.total.value: 6}
  - match: {aggregations.bounds.bounds.top_left.lat: 45.7799999602139 }
  - match: {aggregations.bounds.bounds.top_left.lon: -173.4500000718981 }
  - match: {aggregations.bounds.bounds.bottom_right.lat: -63.240000014193356 }
  - match: {aggregations.bounds.bounds.bottom_right.lon: 120.77999993227422 }
  - match: {aggregations.bounds_from_doc_value.bounds.top_left.lat: 45.7799999602139 }
  - match: {aggregations.bounds_from_doc_value.bounds.top_left.lon: -173.4500000718981 }
  - match: {aggregations.bounds_from_doc_value.bounds.bottom_right.lat: -63.240000014193356 }
  - match: {aggregations.bounds_from_doc_value.bounds.bottom_right.lon: 120.77999993227422 }
  - match: {aggregations.bounds_from_source.bounds.top_left.lat: 45.7799999602139 }
  - match: {aggregations.bounds_from_source.bounds.top_left.lon: -173.4500000718981 }
  - match: {aggregations.bounds_from_source.bounds.bottom_right.lat: -63.240000014193356 }
  - match: {aggregations.bounds_from_source.bounds.bottom_right.lon: 120.77999993227422 }

---
"geo_distance sort":
  - do:
      search:
        index: locations
        body:
          sort:
            _geo_distance:
              location_from_source:
                lat: 0.0
                lon: 0.0
  - match: {hits.total.value: 6}
  - match: {hits.hits.0._source.location.lat: 0.0 }
  - match: {hits.hits.0._source.location.lon: 0.0 }
  - match: {hits.hits.1._source.location.lat: 13.5 }
  - match: {hits.hits.1._source.location.lon: 34.89 }
  - match: {hits.hits.2._source.location.lat: 32.45 }
  - match: {hits.hits.2._source.location.lon: 45.6 }
  - match: {hits.hits.3._source.location.lat: -63.24 }
  - match: {hits.hits.3._source.location.lon: 31.0 }

---
"distance_feature query":
  - do:
      search:
        index: locations
        body:
          query:
            bool:
              should:
                distance_feature:
                  field: "location"
                  pivot: "1000km"
                  origin: [0.0, 0.0]

  - match: {hits.total.value: 6}
  - match: {hits.hits.0._source.location.lat: 0.0 }
  - match: {hits.hits.0._source.location.lon: 0.0 }
  - match: {hits.hits.1._source.location.lat: 13.5 }
  - match: {hits.hits.1._source.location.lon: 34.89 }
  - match: {hits.hits.2._source.location.lat: 32.45 }
  - match: {hits.hits.2._source.location.lon: 45.6 }
  - match: {hits.hits.3._source.location.lat: -63.24 }
  - match: {hits.hits.3._source.location.lon: 31.0 }

