The pits encountered by ElasticSearch7 JavaAPI Java High Level REST Client using script script

version

ElasticSearch:7.9
elasticsearch-rest-high-level-client:7.6

problem

I wrote a project recently. I used ES when I needed to search, but I encountered a few pits when using script. Record here. My script already exists in ES and is called by id.

process

1. Use Script's getParams().put() method to set parameters

Result: java.lang.UnsupportedOperationException is thrown, because the getParams() method of Script gets an UnmodifiableMap, which is an unmodifiable and read-only Map. Using the put method will throw an exception. Therefore, pass in the constructor Just enter the parameters.

2. A Variable ** undefined error occurred during use

Reason: This is related to ScriptType. There are two types of ScriptType enumeration: INLINE and STORED. Because my script exists in ES, I should use STORED.

if (this.type == ScriptType.INLINE) {
           ...
        } else {
            builder.field("id", this.idOrCode);
        }

The source code shows that when the ScriptType is INLINE, idOrCode is the code, that is, the script code, and when the ScriptType is STORED, it is used as the script id stored in ES, but I used INLINE in the constructor, so an error occurred. In addition, looking at the code, you can see that the default ScriptType is INLINE, and in the static code block, set DEFAULT_SCRIPT_TYPE to INLINE

 DEFAULT_SCRIPT_TYPE = ScriptType.INLINE;

3. Run again, throw java.lang.IllegalArgumentException: lang cannot be specified for stored scripts

Reason: In the lang of the constructor, I set "painless"

if (lang != null) {
                throw new IllegalArgumentException("lang cannot be specified for stored scripts");
            }

According to the source code, just set the lang parameter to null

Guess you like

Origin blog.csdn.net/Baibair/article/details/108361192