Find the lowest value from nested document for score calculation using script

I am using ElasticSearch 2.4. I have the following mapping with many more properties (simplified here intentionally)

{
  "name": {"type": "string"}
  "rents": {
    "type": "nested",
    "dynamic": "strict",
    "properties": {
      "start":                        { "type": "integer"},
      "end":                          { "type": "integer"},
      "min":                          { "type": "integer"}
    }                     
  }
}

I have to calculate the search score for each documents using the script. In that score calculation, one of the calculation
is to find the nested rents documents those match the given conditions and in those matched nested documents
get the LOWEST value of min and return that as the score of that script. So I have written the following
query but it is not working.

"query": {
  "bool": {
    "must": [{
      "nested": {
        "path": "rents",
        "query": {
          "function_score": {
            "functions": [{
              "aggs" : {
                "min_price" : {
                  "min" : {
                    "script" : "if(doc['rents.start'].value > 200 && doc['rents.end'].value < 400) {return doc['rents.min'].value;} else {return 0;}"
                  }
                }
              }
            }],
            "score_mode": "sum",
            "boost_mode": "replace"
          }
        }
      }
    }]
  }
}

Any idea what is going wrong here?

What exactly does not working mean here?

Also, with such use-cases it would be incredibly helpful to have a reproducible example, including some documents to test with.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.