Sorting by an expression based on nested field values in Elasticsearch

Viewed 33

I have an index with a mapping like

{
  "mappings": {
  "properties": {
    "content": {
      "type": "text"
    },
    "entities": {
      "type": "nested",
      "properties": {
        "name": {
          "type": "text"
        },
        "score": {
          "type": "double"
        }
      }
    }
  }
}

}

So, the index consists of documents having text (content) and a list of nested entities, each having a name and a score telling, say, how often an entity is mentioned in a document. When I build a query, I could be interested in multiple entities, let's say, (A and B or C), meaning that I search for documents having either both A and B or just C (and I build my query accordingly). What I try to achieve in addition is to apply the same rules for sorting. By that I mean (let's imagine I sort my documents descending) that a score for ranking in the above-mentioned case should be calculated as max( min(a.score, b.score), c.score). So, briefly, any and is converted to min, while any or becomes max. Let's say that building that expression is not an issue and is out of scope of my question. My question is how to map that expression with mins and maxes to the sort part of a query? Is it possible to achieve that without scripting? If not, how do I write the script?

Thanks in advance

0 Answers
Related