ElasticSearch: aggregation on _score field w/ Groovy disabled

前端 未结 2 762
感情败类
感情败类 2021-01-18 03:34

Every example I\'ve seen (e.g., ElasticSearch: aggregation on _score field?) for doing aggregations on or related to the _score field seems to require the usage of scripting

2条回答
  •  灰色年华
    2021-01-18 04:06

    One possible approach is to use the other scripting options available. mvel seems not to be possible to be used unless dynamic scripting is enabled. And, unless a more fine-grained control of scripting enable/disable reaches 1.6 version, I don't think is possible to enable dynamic scripting for mvel and not for groovy.

    We are left with native and mustache (used for templates) that are enabled by default. I don't think custom scripting can be done with mustache, if it's possible I didn't find a way and we are left with native (Java) scripting.

    Here's my take to this:

    • create an implementation of NativeScriptFactory:
    package com.foo.script;
    
    import java.util.Map;
    
    import org.elasticsearch.script.ExecutableScript;
    import org.elasticsearch.script.NativeScriptFactory;
    
    public class MyScriptNativeScriptFactory implements NativeScriptFactory {
    
        @Override
        public ExecutableScript newScript(Map arg0) {
            return new MyScript();
        }
    
    }
    
    • an implementation of AbstractFloatSearchScript for example:
    package com.foo.script;
    
    import java.io.IOException;
    
    import org.elasticsearch.script.AbstractFloatSearchScript;
    
    public class MyScript extends AbstractFloatSearchScript {
    
        @Override
        public float runAsFloat() {
            try {
                return score();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return 0;
        }
    
    }
    
    • alternatively, build a simple Maven project to tie all together. pom.xml:
    
        1.5.2
        1.8
        1.8
    
    
    
        
            org.elasticsearch
            elasticsearch
            ${elasticsearch.version}
            compile
        
    
    
    
        src
        
            
                maven-compiler-plugin
                3.1
                
                    1.8
                    1.8
                
            
        
    
    
    • build it and get the resulting jar file.
    • place the jar inside [ES_folder]/lib
    • edit elasticsearch.yml and add script.native.my_script.type: com.foo.script.MyScriptNativeScriptFactory

    • restart ES nodes.

    • use it in aggregations:
    {
      "aggs": {
        "max_score": {
          "max": {
            "script": "my_script",
            "lang": "native"
          }
        }
      }
    }
    

    My sample above just returns the _score as a script but, of course, it can be used in more advanced scenarios.

    EDIT: if you are not allowed to touch the instances, then I don't think you have any options.

提交回复
热议问题