Access logger from Elasticsearch script

后端 未结 1 1861
天涯浪人
天涯浪人 2020-12-18 01:45

I use the scripts aggressively for scoring and aggregation. One thing i cant figure out is how to emit logs from the script. I tried console.log , but then it didnt work out

相关标签:
1条回答
  • 2020-12-18 02:24

    This can be done by accessing global Elasticsearch logger instance. Its groovy example is given below You should be able to do something similar for javascript and other scripting languages too.

    import  org.elasticsearch.common.logging.*; 
    ESLogger logger=ESLoggerFactory.getLogger('myscript'); 
    logger.info('This is a log message'); 
    

    So when you do a terms aggregation , you can do something like below -

      "aggregations": {
          "debug":{
              "terms":{
                  "script":"import  org.elasticsearch.common.logging.*; ESLogger logger=ESLoggerFactory.getLogger('myscript'); logger.info('This is a log message'); return doc['myField'].value;"
              }
          }
    }
    

    Some good folks from Elasticsearch has given a good documentation on it against a issue.

    LINK - https://github.com/elasticsearch/elasticsearch/issues/9068

    I have also given some examples here.

    0 讨论(0)
提交回复
热议问题