0

I'm using kibana 4.4.1 and in elasticsearch I store the status of PC, only when PC status is changed (open, closed, warings, etc)

My data into Elasticsearch looks like:

{ "status_id":1 , "pc":"lpt001" , "date":"2016-10-25T17:49:00Z" }
{ "status_id":3 , "pc":"lpt001" , "date":"2016-10-25T15:48:00Z" }
{ "status_id":4 , "pc":"lpt002" , "date":"2016-10-25T15:46:00Z" }
{ "status_id":1 , "pc":"lpt002" , "date":"2016-10-25T12:48:00Z" }

And I what to get the newest record in order to have at any time how many PC's are opened, closed or have some issues. My query is like:

GET cb-2016.10.26/_search
{
  "query": {
    "match_all": { }
  },
  "sort": [
    {
      "date": {
        "order": "desc"
      }
    }
  ], 
  "aggs": {
    "max_date":{
      "max": {
        "field": "date"
      }
    }
  }
}

And the result is:

"aggregations": {
    "max_date": {
      "value": 1477417680000,
      "value_as_string": "2016-10-25T17:48:00.000Z"
    }
  }

But What I want is to have that max_date for each "pc": "lpt001", "lpt002".

There is any way to split max_date by "pc" field? I read something about bucket aggregations but I did not reach the result.

Thank you

Ovidiu Rudi
  • 191
  • 1
  • 15

2 Answers2

2

Yes, you can do it like this using a terms aggregation for the pc field and then move the max_date to a sub-aggregation of the terms one:

POST cb-2016.10.26/_search
{
  "query": {
    "match_all": { }
  },
  "sort": [
    {
      "date": {
        "order": "desc"
      }
    }
  ], 
  "aggs": {
    "pcs": {
      "terms": {
        "field": "pc"
      },
      "aggs": {
        "max_date":{
          "max": {
            "field": "date"
          }
        }
      }
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
0

the final query looks like:

{
  "query": {
    "match_all": { }
  },
  "aggs" : {
        "pcstatus" : {
            "terms" : {
                "field" : "pc"
            },
            "aggs": {
                "top_date_hit": {
                    "top_hits": {
                        "sort": [
                            {
                                "date": {
                                    "order": "desc"
                                }
                            }
                        ],
                        "size" : 1
                    }
                }
            }
        }
    }
}
Ovidiu Rudi
  • 191
  • 1
  • 15