7

Note: I had originally posted this question a little differently and it wasn't worth updating as after reading I learned a bit more.

Requirement

Search for documents and calculate a custom score based on nested elements within the document.

Structure

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

Sample Query

{
  "query": {
    "function_score": {
      "query": {
        "term": { "title": "The Magical World of Spittle" }
      },
      "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
          }
        }
      }
    }
  }
}

Isolated Painless

int score = 0;
for(int i = 0; i < doc['topics'].values.length; i++) {
  score += doc['topics'][i].weight;
}
return score;

The Error

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

The Questions

  • What's wrong?
  • What to do?
Phonolog
  • 6,321
  • 3
  • 36
  • 64
el n00b
  • 1,957
  • 7
  • 37
  • 64

1 Answers1

12

Nested documents are stored in different documents in the index, so you cannot access them via doc values from the parent document. You need to use the source document and navigate to the topics.weight property, like this:

Isolated Painless:

int score = 0; 
for(int i = 0; i < params._source['topics'].size(); i++) { 
    score += params._source['topics'][i].weight; 
}
return score;

Full query:

{
  "query": {
    "function_score": {
      "query": {
        "term": { "title": "Book 1" }
      },
      "script_score": {
        "script": {
          "lang": "painless",
          "inline": "int score = 0; for(int i = 0; i < params._source['topics'].size(); i++) { score += params._source['topics'][i].weight; } return score;",
          "params": {
            "default_return_value": 100
          }
        }
      }
    }
  }
}

PS: Also note that the type int doesn't exist, it's is integer

Val
  • 207,596
  • 13
  • 358
  • 360
  • That worked like a charm. The `params._source` was exactly what I needed. – el n00b Oct 23 '17 at 14:47
  • For the nature of what is going on for the actual solution, the performance hit is more than welcomed to be passed on to Elastic. The tests I ran against a giant load of documents wasn't too bad and this isn't for a client-facing issue. This is all back-end processing for data I'm pulling in. – el n00b Oct 23 '17 at 17:25