Painless iterate over nested data in an array to calculate custom score

I'd like to iterate over a set of child elements on a search in order to calculate a value using Painless and then return that information with the rest of the search result. I decided to hijack the score calculation.

Basic index:

{
  "mappings": {
    "book": {
      "properties": {
        "title": { "type": "string", "index": "not_analyzed" },
        "topics": {
          "type": "nested",
          "properties": {
            "weight": { "type": "int" },
            "title":  { "type": "string" }
          }
        }
      }
    }
  }
}

Assuming a sample document like this in /books/book/:

{
  "title": "Title 1",
  "topics": [
    { "weight": 123, "title": "foo" },
    { "weight": 4, "title": "bar" },
    { "weight": 72, "title": "baz" }
  ]
}

I formatted my query with a script like this:

{
  "query": {
  	"function_score": {
  	  "query": {
  	    "term": { "title": "QT-0003" }
  	  },
  	  "script_score": {
  	  	"script": {
  	  		"lang": "painless",
  	  		"inline": "int score = 0; for(int i = 0; i < doc['topics'].values.length; i++) { score += doc['topics'][i].weight; } return score;",
  	  		"params": {
  	  		  "default_return_value": 100
  	  		}
  	  	}
  	  }
  	}
  }
}

My problem is this:

No field found for [topics] in mapping with types [book]

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

Hi @el-n00b,

I realize that this response is a bit late, but in case you or anyone else is still wondering:

To use nested fields, you must enter the nested context. To do so, you simply need to wrap whatever query / filter you want in a nested query:

{
  "query": {
    "nested": {
      "path": "title",
      "query": { ... }
    }
  }
}

Unless you're in the right path of a nested context, then you cannot access the given field.

Hope that helps,
Chris

1 Like

This topic was automatically closed after 24 days. New replies are no longer allowed.