---
setup:
  - requires:
      cluster_features: ["gte_v8.8.0"]
      reason: position metric introduced in 8.8.0
  - do:
      indices.create:
        index: locations
        body:
          settings:
            index:
              mode: time_series
              routing_path: [ city ]
              time_series:
                start_time: 2023-01-01T00:00:00Z
                end_time: 2024-01-01T00:00:00Z
          mappings:
            properties:
              "@timestamp":
                type: date
              city:
                type: keyword
                time_series_dimension: true
              category:
                type: keyword
                time_series_dimension: false
              route:
                type: long
                time_series_dimension: false
              name:
                type: keyword
                time_series_dimension: false
              location:
                type: geo_point
                time_series_metric: position

  - do:
      bulk:
        index: locations
        refresh: true
        body: |
          {"index":{}}
          {"@timestamp": "2023-01-02T09:00:00Z", "route": 0, "location": "POINT(4.889187 52.373184)", "city": "Amsterdam", "category": "Attraction", "name": "Royal Palace Amsterdam"}
          {"index":{}}
          {"@timestamp": "2023-01-02T10:00:00Z", "route": 1, "location": "POINT(4.885057 52.370159)", "city": "Amsterdam", "category": "Attraction", "name": "The Amsterdam Dungeon"}
          {"index":{}}
          {"@timestamp": "2023-01-02T13:00:00Z", "route": 2, "location": "POINT(4.901618 52.369219)", "city": "Amsterdam", "category": "Museum", "name": "Museum Het Rembrandthuis"}
          {"index":{}}
          {"@timestamp": "2023-01-02T16:00:00Z", "route": 3, "location": "POINT(4.912350 52.374081)", "city": "Amsterdam", "category": "Museum", "name": "NEMO Science Museum"}
          {"index":{}}
          {"@timestamp": "2023-01-03T12:00:00Z", "route": 4, "location": "POINT(4.914722 52.371667)", "city": "Amsterdam", "category": "Museum", "name": "Nederlands Scheepvaartmuseum"}
          {"index":{}}
          {"@timestamp": "2023-01-04T09:00:00Z", "route": 5, "location": "POINT(4.401384 51.220292)", "city": "Antwerp", "category": "Attraction", "name": "Cathedral of Our Lady"}
          {"index":{}}
          {"@timestamp": "2023-01-04T12:00:00Z", "route": 6, "location": "POINT(4.405819 51.221758)", "city": "Antwerp", "category": "Museum", "name": "Snijders&Rockoxhuis"}
          {"index":{}}
          {"@timestamp": "2023-01-04T15:00:00Z", "route": 7, "location": "POINT(4.405200 51.222900)", "city": "Antwerp", "category": "Museum", "name": "Letterenhuis"}
          {"index":{}}
          {"@timestamp": "2023-01-05T10:00:00Z", "route": 8, "location": "POINT(2.336389 48.861111)", "city": "Paris", "category": "Museum", "name": "Musée du Louvre"}
          {"index":{}}
          {"@timestamp": "2023-01-05T14:00:00Z", "route": 9, "location": "POINT(2.327000 48.860000)", "city": "Paris", "category": "Museum", "name": "Musée dOrsay"}
  - do:
      indices.refresh: { }

---
"geo_line on position field ASC":
  - skip:
      features: close_to

  - do:
      search:
        index: locations
        rest_total_hits_as_int: true
        body:
          aggregations:
            by_time_series:
              time_series: { }
              aggregations:
                museum_tour:
                  geo_line:
                    point:
                      field: location
                    sort:
                      field: "@timestamp"
                    sort_order: ASC
  - match: { hits.total: 10 }
  - length: { aggregations.by_time_series.buckets: 3 }
  - match:
      aggregations.by_time_series.buckets:
        "{city=Paris}":
          key: { city: Paris }
          doc_count: 2
          museum_tour:
            type: Feature
            geometry:
              coordinates: [ [ 2.336389, 48.861111 ], [ 2.327, 48.86 ] ]
              type: LineString
            properties: { complete: true }
        "{city=Antwerp}":
          key: { city: Antwerp }
          doc_count: 3
          museum_tour:
            type: Feature
            geometry:
              coordinates: [ [ 4.401384, 51.220292 ], [ 4.405819, 51.221758 ], [ 4.4052, 51.2229 ] ]
              type: LineString
            properties: { complete: true }
        "{city=Amsterdam}":
          key: { city: Amsterdam }
          doc_count: 5
          museum_tour:
            type: Feature
            geometry:
              coordinates: [ [ 4.889187, 52.373184 ], [ 4.885057, 52.370159 ], [ 4.901618, 52.369219 ], [ 4.91235, 52.374081 ], [ 4.914722, 52.371667 ] ]
              type: LineString
            properties: { complete: true }

---
"geo_line on position field DESC":
  - skip:
      features: close_to

  - do:
      search:
        index: locations
        rest_total_hits_as_int: true
        body:
          aggregations:
            by_time_series:
              time_series: { }
              aggregations:
                museum_tour:
                  geo_line:
                    point:
                      field: location
                    sort:
                      field: "@timestamp"
                    sort_order: DESC
  - match: { hits.total: 10 }
  - length: { aggregations.by_time_series.buckets: 3 }
  - match:
      aggregations.by_time_series.buckets:
        "{city=Paris}":
          key: { city: Paris }
          doc_count: 2
          museum_tour:
            type: Feature
            geometry:
              coordinates: [ [ 2.327, 48.86 ], [ 2.336389, 48.861111 ] ]
              type: LineString
            properties: { complete: true }
        "{city=Antwerp}":
          key: { city: Antwerp }
          doc_count: 3
          museum_tour:
            type: Feature
            geometry:
              coordinates: [ [ 4.4052, 51.2229 ], [ 4.405819, 51.221758 ], [ 4.401384, 51.220292 ] ]
              type: LineString
            properties: { complete: true }
        "{city=Amsterdam}":
          key: { city: Amsterdam }
          doc_count: 5
          museum_tour:
            type: Feature
            geometry:
              coordinates: [ [ 4.914722, 52.371667 ],
                             [ 4.91235, 52.374081 ],
                             [ 4.901618, 52.369219 ],
                             [ 4.885057, 52.370159 ],
                             [ 4.889187, 52.373184 ] ]
              type: LineString
            properties: { complete: true }

---
"geo_line on position field time time-series with invalid sort specified":
  - skip:
      features: close_to

  - do:
      catch: /invalid field \[sort\]='route' configured for time-series aggregations/
      search:
        index: locations
        rest_total_hits_as_int: true
        body:
          aggregations:
            by_time_series:
              time_series: { }
              aggregations:
                museum_tour:
                  geo_line:
                    point:
                      field: location
                    sort:
                      field: route

---
"geo_line on position field no sort field or sort order specified":
  - skip:
      features: close_to

  - do:
      search:
        index: locations
        rest_total_hits_as_int: true
        body:
          aggregations:
            by_time_series:
              time_series: { }
              aggregations:
                museum_tour:
                  geo_line:
                    point:
                      field: location
  - match: { hits.total: 10 }
  - length: { aggregations.by_time_series.buckets: 3 }
  - match:
      aggregations.by_time_series.buckets:
        "{city=Paris}":
          key: { city: Paris }
          doc_count: 2
          museum_tour:
            type: Feature
            geometry:
              coordinates: [ [ 2.336389, 48.861111 ], [ 2.327, 48.86 ] ]
              type: LineString
            properties: { complete: true }
        "{city=Antwerp}":
          key: { city: Antwerp }
          doc_count: 3
          museum_tour:
            type: Feature
            geometry:
              coordinates: [ [ 4.401384, 51.220292 ], [ 4.405819, 51.221758 ], [ 4.4052, 51.2229 ] ]
              type: LineString
            properties: { complete: true }
        "{city=Amsterdam}":
          key: { city: Amsterdam }
          doc_count: 5
          museum_tour:
            type: Feature
            geometry:
              coordinates: [ [ 4.889187, 52.373184 ], [ 4.885057, 52.370159 ], [ 4.901618, 52.369219 ], [ 4.91235, 52.374081 ], [ 4.914722, 52.371667 ] ]
              type: LineString
            properties: { complete: true }

---
"geo_line on position field same grouping as time-series without time-series":
  - skip:
      features: close_to

  - do:
      search:
        index: locations
        rest_total_hits_as_int: true
        body:
          aggregations:
            not_time_series:
              terms:
                field: city
              aggregations:
                museum_tour:
                  geo_line:
                    point:
                      field: location
                    sort:
                      field: "@timestamp"
  - match: { hits.total: 10 }
  - length: { aggregations.not_time_series.buckets: 3 }
  - match:
      aggregations.not_time_series.buckets.0:
        key: Amsterdam
        doc_count: 5
        museum_tour:
          type: Feature
          geometry:
            coordinates: [ [ 4.889187, 52.373184 ], [ 4.885057, 52.370159 ], [ 4.901618, 52.369219 ], [ 4.91235, 52.374081 ], [ 4.914722, 52.371667 ] ]
            type: LineString
          properties: { complete: true }
  - match:
      aggregations.not_time_series.buckets.1:
        key: Antwerp
        doc_count: 3
        museum_tour:
          type: Feature
          geometry:
            coordinates: [ [ 4.401384, 51.220292 ], [ 4.405819, 51.221758 ], [ 4.4052, 51.2229 ] ]
            type: LineString
          properties: { complete: true }
  - match:
      aggregations.not_time_series.buckets.2:
        key: Paris
        doc_count: 2
        museum_tour:
          type: Feature
          geometry:
            coordinates: [ [ 2.336389, 48.861111 ], [ 2.327, 48.86 ] ]
            type: LineString
          properties: { complete: true }

---
"geo_line on position field same grouping as time-series with time-series":
  - skip:
      features: close_to

  - do:
      search:
        index: locations
        rest_total_hits_as_int: true
        body:
          aggregations:
            with_time_series:
              time_series: { }
              aggregations:
                museums:
                  terms:
                    field: city
                  aggregations:
                    museum_tour:
                      geo_line:
                        point:
                          field: location
                        sort:
                          field: "@timestamp"
  - match: { hits.total: 10 }
  - length: { aggregations.with_time_series.buckets: 3 }
  - match:
      aggregations.with_time_series.buckets:
        "{city=Amsterdam}":
          key: { city: Amsterdam }
          doc_count: 5
          museums:
            doc_count_error_upper_bound: 0
            sum_other_doc_count: 0
            buckets:
              - key: Amsterdam
                doc_count: 5
                museum_tour:
                  type: Feature
                  geometry:
                    coordinates: [ [ 4.889187, 52.373184 ], [ 4.885057, 52.370159 ], [ 4.901618, 52.369219 ], [ 4.91235, 52.374081 ], [ 4.914722, 52.371667 ] ]
                    type: LineString
                  properties:
                    complete: true
        "{city=Paris}":
          key: { city: Paris }
          doc_count: 2
          museums:
            doc_count_error_upper_bound: 0
            sum_other_doc_count: 0
            buckets:
              - key: Paris
                doc_count: 2
                museum_tour:
                  type: Feature
                  geometry:
                    coordinates: [ [ 2.336389, 48.861111 ], [ 2.327, 48.86 ] ]
                    type: LineString
                  properties:
                    complete: true
        "{city=Antwerp}":
          key: { city: Antwerp }
          doc_count: 3
          museums:
            doc_count_error_upper_bound: 0
            sum_other_doc_count: 0
            buckets:
              - key: Antwerp
                doc_count: 3
                museum_tour:
                  type: Feature
                  geometry:
                    coordinates: [ [ 4.401384, 51.220292 ], [ 4.405819, 51.221758 ], [ 4.4052, 51.2229 ] ]
                    type: LineString
                  properties:
                    complete: true

---
"geo_line on position field with terms aggregation on non-dimension field":
  - skip:
      features: close_to

  - do:
      search:
        index: locations
        rest_total_hits_as_int: true
        body:
          aggregations:
            not_time_series:
              terms:
                field: category
              aggregations:
                museum_tour:
                  geo_line:
                    point:
                      field: location
                    sort:
                      field: "@timestamp"
  - match: { hits.total: 10 }
  - length: { aggregations.not_time_series.buckets: 2 }
  - match:
      aggregations.not_time_series.buckets.0:
        key: Museum
        doc_count: 7
        museum_tour:
          type: Feature
          geometry:
            coordinates: [ [ 4.901618, 52.369219 ], [ 4.91235, 52.374081 ], [ 4.914722, 52.371667 ], [ 4.405819, 51.221758 ], [ 4.4052, 51.2229 ], [ 2.336389, 48.861111 ], [ 2.327, 48.86 ] ]
            type: LineString
          properties: { complete: true }
  - match:
      aggregations.not_time_series.buckets.1:
        key: Attraction
        doc_count: 3
        museum_tour:
          type: Feature
          geometry:
            coordinates: [ [ 4.889187, 52.373184 ], [ 4.885057, 52.370159 ], [ 4.401384, 51.220292 ] ]
            type: LineString
          properties: { complete: true }

---
"geo_line on position field with terms aggregation on non-dimension field and time-series on dimension field":
  - skip:
      features: close_to

  - do:
      search:
        index: locations
        rest_total_hits_as_int: true
        body:
          aggregations:
            with_time_series:
              time_series: { }
              aggregations:
                museums:
                  terms:
                    field: category
                  aggregations:
                    museum_tour:
                      geo_line:
                        point:
                          field: location
                        sort:
                          field: "@timestamp"
  - match: { hits.total: 10 }
  - length: { aggregations.with_time_series.buckets: 3 }
  - match:
      aggregations.with_time_series.buckets:
        "{city=Amsterdam}":
          key: { city: Amsterdam }
          doc_count: 5
          museums:
            doc_count_error_upper_bound: 0
            sum_other_doc_count: 0
            buckets:
              - key: Museum
                doc_count: 3
                museum_tour:
                  type: Feature
                  geometry:
                    coordinates: [ [ 4.901618, 52.369219 ], [ 4.91235, 52.374081 ], [ 4.914722, 52.371667 ] ]
                    type: LineString
                  properties:
                    complete: true
              - key: Attraction
                doc_count: 2
                museum_tour:
                  type: Feature
                  geometry:
                    coordinates: [ [ 4.889187, 52.373184 ], [ 4.885057, 52.370159 ] ]
                    type: LineString
                  properties:
                    complete: true
        "{city=Paris}":
          key: { city: Paris }
          doc_count: 2
          museums:
            doc_count_error_upper_bound: 0
            sum_other_doc_count: 0
            buckets:
              - key: Museum
                doc_count: 2
                museum_tour:
                  type: Feature
                  geometry:
                    coordinates: [ [ 2.336389, 48.861111 ], [ 2.327, 48.86 ] ]
                    type: LineString
                  properties:
                    complete: true
        "{city=Antwerp}":
          key: { city: Antwerp }
          doc_count: 3
          museums:
            doc_count_error_upper_bound: 0
            sum_other_doc_count: 0
            buckets:
              - key: Museum
                doc_count: 2
                museum_tour:
                  type: Feature
                  geometry:
                    coordinates: [ [ 4.405819, 51.221758 ], [ 4.4052, 51.2229 ] ]
                    type: LineString
                  properties:
                    complete: true
              - key: Attraction
                doc_count: 1
                museum_tour:
                  type: Feature
                  geometry:
                    coordinates: [ 4.401384, 51.220292 ]
                    type: Point
                  properties:
                    complete: true

---
"geo_line on position field multi-terms aggregation on combined dimension and non-dimension fields":
  - skip:
      features: close_to

  - do:
      search:
        index: locations
        rest_total_hits_as_int: true
        body:
          aggregations:
            not_time_series:
              multi_terms:
                terms: [ { field: city }, { field: category } ]
              aggregations:
                museum_tour:
                  geo_line:
                    point:
                      field: location
                    sort:
                      field: "@timestamp"
  - match: { hits.total: 10 }
  - length: { aggregations.not_time_series.buckets: 5 }
  - match:
      aggregations.not_time_series.buckets.0:
        key: [Amsterdam, Museum]
        key_as_string: "Amsterdam|Museum"
        doc_count: 3
        museum_tour:
          type: Feature
          geometry:
            coordinates: [ [ 4.901618, 52.369219 ], [ 4.91235, 52.374081 ], [ 4.914722, 52.371667 ] ]
            type: LineString
          properties: { complete: true }
  - match:
      aggregations.not_time_series.buckets.1:
        key: [Amsterdam, Attraction]
        key_as_string: "Amsterdam|Attraction"
        doc_count: 2
        museum_tour:
          type: Feature
          geometry:
            coordinates: [ [ 4.889187, 52.373184 ], [ 4.885057, 52.370159 ] ]
            type: LineString
          properties: { complete: true }
  - match:
      aggregations.not_time_series.buckets.2:
        key: [Antwerp, Museum]
        key_as_string: "Antwerp|Museum"
        doc_count: 2
        museum_tour:
          type: Feature
          geometry:
            coordinates: [ [ 4.405819, 51.221758 ], [ 4.4052, 51.2229 ] ]
            type: LineString
          properties: { complete: true }
  - match:
      aggregations.not_time_series.buckets.3:
        key: [Paris, Museum]
        key_as_string: "Paris|Museum"
        doc_count: 2
        museum_tour:
          type: Feature
          geometry:
            coordinates: [ [ 2.336389, 48.861111 ], [ 2.327, 48.86 ] ]
            type: LineString
          properties: { complete: true }
  - match:
      aggregations.not_time_series.buckets.4:
        key: [Antwerp, Attraction]
        key_as_string: "Antwerp|Attraction"
        doc_count: 1
        museum_tour:
          type: Feature
          geometry:
            coordinates: [ 4.401384, 51.220292 ]
            type: Point
          properties: { complete: true }
