Rajagopal Vajja
Rajagopal Vajja

Reputation: 127

Find min values within a document using elastic search

We tried aggregation it is returning the min of all the documents.

Is there any way we can find the min of start date from each document, like this using elasticsearch?.

The output should be "1989-03-01T00:00:00Z" :

{
"experience": [{
        "isCurrent": false,
        "endDate": "2000-06-01T00:00:00Z",
        "jobTitle": "a",
        "company": "a",
        "title": "a",
        "startDate": "1996-05-01T00:00:00Z"
    }, {
        "isCurrent": false,
        "endDate": "2012-01-01T00:00:00Z",
        "jobTitle": "b",
        "company": "B",
        "title": "b",
        "startDate": "2008-01-01T00:00:00Z"
    }, {
        "isCurrent": false,
        "endDate": "2007-10-01T00:00:00Z",
        "company_org": "",
        "jobTitle": "c",
        "company": "C",
        "companyUrl": "",
        "title": "c",
        "startDate": "2004-09-01T00:00:00Z"
    }, {
        "isCurrent": false,
        "endDate": "1993-03-01T00:00:00Z",
        "jobTitle": "d",
        "company": "D",
        "title": "d",
        "startDate": "1991-05-01T00:00:00Z"
    }, {
        "isCurrent": false,
        "endDate": "1991-05-01T00:00:00Z",
        "company_org": "",
        "jobTitle": "e",
        "company": "E",
        "companyUrl": "",
        "title": "e",
        "startDate": "1989-03-01T00:00:00Z"
    }]
}

Upvotes: 0

Views: 109

Answers (1)

Val
Val

Reputation: 217294

If your experience field has the nested data type, then you can retrieve what you expect with the following query that leverages nested inner_hits:

{
  "query": {
    "nested": {
      "path": "experience",
      "query": {
        "query_string": { "query": "*" }
      },
      "inner_hits": {
        "size": 1,
        "sort": { "startDate": "asc" }
      } 
    }
  }
}

This is going to get you the document with the experience having the oldest startDate

Upvotes: 1

Related Questions