title: ElasticSearch sorting nested with condition
body: <p>With ElasticSearch I want to insert a condition to sort nested fields.</p>
  
  <p>I have this mapping</p>
  
  <pre><code>{
    "dario": {
      "mappings": {
        "agents": {
          "properties": {
            "applications": {
              "type": "nested",
              "properties": {
                "companies": {
                  "type": "nested",
                  "properties": {
                    "active": {
                      "type": "integer"
                    },
                    "application_date": {
                      "type": "date",
                      "format": "strict_date_optional_time||epoch_millis"
                    },
                    "application_date_month": {
                      "type": "date",
                      "format": "yyyy-MM"
                    },
                    "application_id": {
                      "type": "long"
                    },
                    "assigned_date": {
                      "type": "date",
                      "format": "strict_date_optional_time||epoch_millis"
                    },
                    "buy_date": {
                      "type": "date",
                      "format": "strict_date_optional_time||epoch_millis"
                    },
                    "date": {
                      "type": "date",
                      "format": "strict_date_optional_time||epoch_millis"
                    },
                    "date_month": {
                      "type": "date",
                      "format": "yyyy-MM"
                    },
                    "favorite": {
                      "type": "integer"
                    },
                    "id": {
                      "type": "long"
                    },
                    "notes": {
                      "type": "nested",
                      "properties": {
                        "date": {
                          "type": "date",
                          "format": "strict_date_optional_time||epoch_millis"
                        },
                        "id": {
                          "type": "integer"
                        },
                        "note": {
                          "type": "string",
                          "analyzer": "standard"
                        }
                      }
                    },
                    "score": {
                      "type": "long"
                    },
                    "state": {
                      "type": "long"
                    },
                    "view": {
                      "type": "integer"
                    },
                    "visible": {
                      "type": "integer"
                    },
                    "visible_date": {
                      "type": "date",
                      "format": "strict_date_optional_time||epoch_millis"
                    }
                  }
                },
                "count": {
                  "type": "integer"
                },
                "sectors": {
                  "type": "long"
                }
              }
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>I want to sort by the field applications.companies.buy_date, but, if this is null, then i want to consider applications.companies.date 
  I tried with groovy script:</p>
  
  <pre><code>{
    "size": 10,
    "from": 0,
    "sort": [
      {
        "_script": {
          "script": "doc['applications.companies.buy_date'] != null ? doc['applications.companies.buy_date'].date.getMillisOfDay() : doc['applications.companies.date'].date.getMillisOfDay()",
          "type": "number",
          "nested_filter": {
            "match": {
              "applications.companies.id": 711
            }
          },
          "order": "desc"
        }
      }
    ],
    "query": {
      "bool": {
        "filter": [
          {
            "bool": {
              "must": [
                {
                  "bool": {
                    "must": [
                      {
                        "nested": {
                          "path": "applications.companies",
                          "query": {
                            "bool": {
                              "must": [
                                {
                                  "match": {
                                    "applications.companies.active": 1
                                  }
                                },
                                {
                                  "match": {
                                    "applications.companies.id": 711
                                  }
                                },
                                {
                                  "bool": {
                                    "should": [
                                      {
                                        "exists": {
                                          "field": "applications.companies.buy_date"
                                        }
                                      },
                                      {
                                        "match": {
                                          "applications.companies.favorite": 1
                                        }
                                      }
                                    ]
                                  }
                                }
                              ]
                            }
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
  </code></pre>
  
  <p>but nothing change. Any ideas?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I resolved issue with this solution</p>
  
  <pre><code>{
    "size": 10,
    "from": 0,
    "_source": [
      "id"
    ],
    "sort": [
      {
        "_script": {
          "script": {
            "script": " if (doc['applications.companies.id'].value == 711) { return (doc['applications.companies.buy_date'].value &gt; 0) ? doc['applications.companies.buy_date'].value : doc['applications.companies.date'].value; } else { return null; } ",
            "lang": "groovy"
          },
          "type": "number",
          "order": "desc",
          "nested_path": "applications.companies",
          "nested_filter": {
            "match": {
              "applications.companies.id": 711
            }
          }
        }
      }
    ],
    "query": {
      "bool": {
        "filter": [
          {
            "bool": {
              "must": [
                {
                  "bool": {
                    "must": [
                      {
                        "nested": {
                          "path": "applications.companies",
                          "query": {
                            "bool": {
                              "must": [
                                {
                                  "match": {
                                    "applications.companies.active": 1
                                  }
                                },
                                {
                                  "match": {
                                    "applications.companies.id": 711
                                  }
                                },
                                {
                                  "bool": {
                                    "should": [
                                      {
                                        "exists": {
                                          "field": "applications.companies.buy_date"
                                        }
                                      },
                                      {
                                        "match": {
                                          "applications.companies.favorite": 1
                                        }
                                      }
                                    ]
                                  }
                                }
                              ]
                            }
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
  </code></pre>