Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch Painless calculate score from nested elements

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?
like image 483
el n00b Avatar asked Oct 20 '17 16:10

el n00b


People also ask

How do I search in nested fields?

You can search nested fields using dot notation that includes the complete path, such as obj1.name . Multi-level nesting is automatically supported, and detected, resulting in an inner nested query to automatically match the relevant nesting level, rather than root, if it exists within another nested query.

What is nested type in Elasticsearch?

The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.

What is a nested field?

When a packed class contains an instance field that is a packed type, the data for that field is packed directly into the containing class. The field is known as a nested field .


1 Answers

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

like image 90
Val Avatar answered Oct 02 '22 07:10

Val