Search code examples
jsonelasticsearchnestedmaxmin

Elasticsearch: get the max and min value form a specific object of a maps of objects


I has this mapping for the índex on elastic, i was try to get the max value of a day for a specific sensor, but my query get the value of all the sensors.

  "sensor": {
    "type": "nested",
    "properties": {
      "type": {
        "type": "integer"
      },
      "name": {
        "type": "keyword"
      },
      "number": {
        "type": "integer"
      },
      "values": {
        "type": "nested",
        "properties": {
          "type": {
            "type": "text"
          },
          "value": {
            "type": "float"
          },
          "unit": {
            "type": "text"
          },
          "Lmin": {
            "type": "float"
          },
          "Lmax": {
            "type": "float"
          }
        }
      }
    }

An this is the map of objects,

enter image description here

I need only the max and the min value of the las day from the sensor number 13, i try it but ever i get the max of all sensors.

  {"query": {
    "nested": {
      "path": "sensor",
      "query": {
        "nested": {
          "path": "sensor.values",
          "query": {
            "bool": {
              "must": [
                {
                  "match": {
                    "sensor.values.type": "TEMPERATURE"
                  }
                }
              ]
            }
          }
        }
      }
    }
  },
  "aggs": {
    "agg_by_type": {
      "nested": {
        "path": "sensor.values"
      },
      "aggs": {
        "max_value": {
          "max": {
            "field": "sensor.values.value"
          }
        }
      }
    }
  }
}

I'm new in elasticsearch, can someone help whit this please?, thanks.


Solution

  • You need to also add the nested filter in the aggregation part to only aggregate the relevant nested documents, i.e. the ones related to TEMPERATURE, like this:

    {
      "query": {
        "nested": {
          "path": "sensor",
          "query": {
            "nested": {
              "path": "sensor.values",
              "query": {
                "bool": {
                  "must": [
                    {
                      "match": {
                        "sensor.values.type": "TEMPERATURE"
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      },
      "aggs": {
        "agg_by_type": {
          "nested": {
            "path": "sensor.values"
          },
          "aggs": {
            "temperature_only": {
              "filter": {
                "match": {
                  "sensor.values.type": "TEMPERATURE"
                }
              },
              "aggs": {
                "max_value": {
                  "max": {
                    "field": "sensor.values.value"
                  }
                }
              }
            }
          }
        }
      }
    }