0

How to use multiple scripts in ElasticSearch should

Here is the sample data

{
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "hits": [
            {
                "_source": {
                    "type": 2,
                    "size": 11,
                    "name": "haha",
                    "length": 18
                }
            },
            {
                "_source": {
                    "type": 2,
                    "size": 13,
                    "name": "haha",
                    "length": 17
                }
            },
            {
                "_source": {
                    "type": 2,
                    "size": 13,
                    "name": "hehe",
                    "length": 17
                }
            }
        ]
    }
}

It looks like this.

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "type": 2
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "script": {
                                    "script": {
                                        "inline": "doc['size'].value == 11",
                                        "lang": "painless"
                                    }
                                }
                            },
                            {
                                "script": {
                                    "script": {
                                        "inline": "doc['type'].value + doc['size'].value == 15",
                                        "lang": "painless"
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

I am getting the following error. I don't understand why, I only use one of the scripts and they all work fine, do you know what is the reason?

{
    "error": {
        "root_cause": [
            {
                "type": "script_exception",
                "reason": "runtime error",
                "script_stack": [
                    "org.elasticsearch.index.fielddata.ScriptDocValues.throwIfEmpty(ScriptDocValues.java:73)",
                    "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:118)",
                    "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:113)",
                    "doc['size'].value == 11",
                    "           ^---- HERE"
                ],
                "script": "doc['size'].value == 11",
                "lang": "painless",
                "position": {
                    "offset": 11,
                    "start": 0,
                    "end": 23
                }
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "my-test",
                "node": "O9NpQOHXSNqELJwzBf5r0w",
                "reason": {
                    "type": "script_exception",
                    "reason": "runtime error",
                    "script_stack": [
                        "org.elasticsearch.index.fielddata.ScriptDocValues.throwIfEmpty(ScriptDocValues.java:73)",
                        "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.get(ScriptDocValues.java:118)",
                        "org.elasticsearch.index.fielddata.ScriptDocValues$Longs.getValue(ScriptDocValues.java:113)",
                        "doc['size'].value == 11",
                        "           ^---- HERE"
                    ],
                    "script": "doc['size'].value == 11",
                    "lang": "painless",
                    "position": {
                        "offset": 11,
                        "start": 0,
                        "end": 23
                    },
                    "caused_by": {
                        "type": "illegal_state_exception",
                        "reason": "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"
                    }
                }
            }
        ]
    },
    "status": 400
}

I want to use multiple scripts inside should, but I don't know how to use it, can you tell me?

3
  • please add sample document Commented Oct 19, 2022 at 3:55
  • Above query looks fine. What is the error you are getting? Commented Oct 19, 2022 at 4:03
  • That's the typical error for a document that doesn't have a size field with a valid value. Commented Oct 19, 2022 at 5:33

1 Answer 1

2

That's the typical error for a document that doesn't have a size field with a valid value.

A document doesn't have a value for a field! Use doc[].size()==0 to check if a document is missing a field!

If you can't guarantee that all documents have this field, you need to test for the existence of the field before using it:

 "inline": "doc['size'].size() > 0 ? doc['size'].value == 11 : false",

Besides, this simple script can be replaced with a term query and is much more performant:

{
    "term": {
         "size": 11
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I want to use multiple scripts in such a way that this effect is achieved Example: (a && b) || (a && c) || (a && d)
Does my answer above fix the error you get?
Thank you for your answer, your answer can solve the above problem, but I'd rather know how to achieve this by using multiple scripts
My answer solves the script error in your original question, so this should be considered answered. As for your other need ((a && b) || (a && c) || (a && d)) you should ask a new question for it.

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.