0

I have this mapping for my index:

    "my_index": {
        "mappings": {
            "properties": {
                "medias": {
                    "properties": {
                        "media_id": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "media_type": {
                            "type": "long"
                        },
                        "variant": {
                            "properties": {
                                "height": {
                                    "type": "long"
                                },
                                "url": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "width": {
                                    "type": "long"
                                }
                            }
                        },
                        "view_count": {
                            "type": "long"
                        }
                    }
                }
              
}

And I want to write a script for custom scoring like this:

    // "_source": "content",
    "query": {
        "function_score": {
            "functions": [
                {
                    "script_score": {
                        "script": {
                            "source": " (doc.medias.size() == 0 || doc.medias.view_count.size() == 0 ? 0 : doc.medias.view_count.value) "
                        }
                    
                    }
                }
            ],
            "query": {
                "match": {
                    "content": something"
                }
            },
            "score_mode": "multiply"
        }
    }
}

But I get this error:

"caused_by": {
                        "type": "illegal_argument_exception",
                        "reason": "No field found for [medias] in mapping with types []"
                    }

I guess I'm not accessing the view_count property correctly. How can I do that? And one more thing: the medias field is an array and if the size of the array exceeded 1 I need to get the maximum of the view_counts. How can I do this?

1 Answer 1

1

Great start! You're almost there, you just need to access the doc values like this and you'll get the desired results :

    {
      "script_score": {
        "script": {
          "source": " (doc['medias.view_count'].size() == 0 ? 0 : doc['medias.view_count'].value) "
        }
      }
    }

Since doc['medias.view_count'] is actually a Java List, if you need to get the maximum of the view count you can do it like this:

    {
      "script_score": {
        "script": {
          "source": " (doc['medias.view_count'].size() == 0 ? 0 : doc['medias.view_count'].stream().max(Long::compare).get()) "
        }
      }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much! just one more thing: the medias field is an array and if the size of the array exceeded 1 I need to get the maximum of the view_counts. How can I do this?
And something else: When the medias has more than one element, your script view_count returns which one of them?
doc['medias.view_count'] is actually a Java List, so you can get any positional element in that list. For instance, doc['medias.view_count'][2] would return the third view_count

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.