---
setup:

  - do:
      indices.create:
        index: test
        body:
          settings:
            number_of_shards: 3
            number_of_routing_shards: 3
          mappings:
            test:
              "properties":
                "val1":
                  "type": "double"
                "val2":
                  "type": "double"
                "val3":
                  "type": "double"

  - do:
      indices.create:
        index: unmapped
        body:
          settings:
            number_of_shards: 3

  - do:
      index:
        index:  test
        type:   test
        id:     1
        body:   { "val1": 1.9, "val2": 3.1, "val3": 2.3, "vals" : [1.9, 16.143] }
  - do:
      index:
        index:  test
        type:   test
        id:     2
        body:   { "val1": -5.2, "val2": -3.4, "val3": 2.3, "vals" : [155, 16.23]}
  - do:
      index:
        index:  test
        type:   test
        id:     3
        body:   { "val1": -5.2, "val3": 2.3, "vals" : [-455, -32.32]}
  - do:
      index:
        index:  test
        type:   test
        id:     4
        body:   { "val1": 18.3, "val2": 104.4, "val3": 2.3, "vals" : [0.14, 92.1]}
  - do:
      index:
        index:  test
        type:   test
        id:     5
        body:   { "val1": -53.2, "val2": -322.4, "val3": 2.3, "vals" : [16, 16]}
  - do:
      index:
        index:  test
        type:   test
        id:     6
        body:   { "val1": -578.9, "val2": 69.9, "val3": 2.3}
  - do:
      index:
        index:  test
        type:   test
        id:     7
        body:   { "val1": 16.2, "val2": 17.2, "val3": 2.3, "vals" : [1234.3, -3433]}
  - do:
      index:
        index:  test
        type:   test
        id:     8
        body:   { "val1": -4222.63, "val2": 316.44, "val3": 2.3, "vals" : [177.2, -93.333]}
  - do:
      index:
        index:  test
        type:   test
        id:     9
        body:   { "val1": -59999.55, "val2": -3163.4, "val3": 2.3, "vals" : [-29.9, 163.0]}
  - do:
      index:
        index:  test
        type:   test
        id:     10
        body:   { "val1": 782.7, "val2": 789.7, "val3": 2.3, "vals" : [-0.2, 1343.3]}
  - do:
      index:
        index:  test
        type:   test
        id:     11
        body:   { "val1": -1.2, "val2": 6.3, "val3": 2.3, "vals" : [15.3, 16.9]}
  - do:
      index:
        index:  test
        type:   test
        id:     12
        body:   { "val1": 0, "val2": 1.11, "val3": 2.3, "vals" : [-644.4, -644.4]}
  - do:
      index:
        index:  test
        type:   test
        id:     13
        body:   { "val1": 0.1, "val2": 0.92, "val3": 2.3, "vals" : [73.2, 0.12]}
  - do:
      index:
        index:  test
        type:   test
        id:     14
        body:   { "val1": 0.12, "val2": -82.4, "val3": 2.3, "vals" : [-0.001, 1295.3]}
  - do:
      index:
        index:  test
        type:   test
        id:     15
        body:   { "val1": 98.2, "val2": 32.4, "val3": 2.3, "vals" : [15.5, 16.5]}

  - do:
      indices.refresh:
        index: [test, unmapped]

  - do:
      cluster.health:
        wait_for_status: yellow

---
"Unmapped":

  - do:
      search:
        index: unmapped
        type:  test
        body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "vals"]} } } }

  - match: {hits.total: 0}
  - match: {aggregations.mfs.doc_count: 0}

---
"Multi value field Max":

  - do:
      search:
        index: test
        type:  test
        body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "vals"], "mode" : "max"} } } }

  - match: {hits.total: 15}
  - match: {aggregations.mfs.doc_count: 14}
  - match: {aggregations.mfs.fields.0.count: 14}
  - match: {aggregations.mfs.fields.0.correlation.val1: 0.06838646533369998}

---
"Multi value field Min":

  - do:
      search:
        index: test
        type:  test
        body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "vals"], "mode" : "min"} } } }

  - match: {hits.total: 15}
  - match: {aggregations.mfs.doc_count: 14}
  - match: {aggregations.mfs.fields.0.count: 14}
  - match: {aggregations.mfs.fields.0.correlation.val1: -0.09777682707831963}

---
"Partially unmapped":

  - do:
      search:
        index: [test, unmapped]
        type:  test
        body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "vals"]} } } }

  - match: {hits.total: 15}
  - match: {aggregations.mfs.doc_count: 13}
  - match: {aggregations.mfs.fields.0.count: 13}
  - match: {aggregations.mfs.fields.0.correlation.val1: -0.044997535185684244}

---
"Partially unmapped with missing defaults":

  - do:
      search:
        index: [test, unmapped]
        type:  test
        body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val2", "vals"], "missing" : {"val2" : 10, "vals" : 5 } } } } }

  - match: {hits.total: 15}
  - match: {aggregations.mfs.doc_count: 15}
  - match: {aggregations.mfs.fields.0.count: 15}
  - match: {aggregations.mfs.fields.0.correlation.val2: 0.04028024709708195}

---
"With script":

  - do:
      catch: /parsing_exception/
      search:
        index: test
        type:  test
        body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["vals", "val3"], "script" : { "my_script" : {"source" : "1 + doc['val1'].value", "lang" : "js"} } } } } }

---
"With script params":

  - do:
      catch: /parsing_exception/
      search:
        index: test
        type:  test
        body: {"aggs": { "mfs" : { "matrix_stats": {"fields": ["val1", "val3", "vals"], "script" : { "my_script" : {"source" : "my_var + doc['val1'].value", "params" : { "my_var" : 1 }, "lang" : "js" } } } } } }
