Getting Started with Elasticsearch Painless

Painless

Painless is a scripting language developed and maintained by Elastic and optimized for Elasticsearch.

type of data

  • def

    Dynamic data type, the default value is null.

  • The rest of the data types are basically the same as java. There are basic data types, object types, Map, List also have. The basic api is also the corresponding api of java

relationship with Java. (Java8)

Extends Java’s syntax to provide Groovy-style scripting language features that make scripts easier to write.

Example

  • iterate over the array

    for(def item :  doc['cmd'].values) {
    
    }
    

    The data is taken out as an object, such as doc['cmd']. To get the corresponding value, you need to call the corresponding attribute. If it is an array, it is values, if it is a numerical value, and if it is a date, it is date.

  • date type operation

    return doc['begin_at'].date.hourOfDay

    The type of this date corresponds to what is in java. I don't know. It's a bit like a variant of Calendar, and all the properties that Calendar can get are

debugging

Regarding debugging, I have always been very concerned and helpless.

If you add script fields to Kibana, use Dev Tools to verify the script first to see if there is any syntax error, and then paste the scipt part in after success. Simple conversion example

GET /test/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "test_script": {
      "script": {
        "lang": "painless",
        "inline": "return doc['begin_at'].date.hourOfDay"
      }
    }
  }, 
  "size": 1
}

The official description of debugging is Debug.explain

PUT /hockey/player/1?refresh
{"first":"johnny","last":"gaudreau","goals":[9,27,1],"assists":[17,46,0],"gp":[26,82,1]}

POST /hockey/player/1/_explain
{
  "query": {
    "script": {
      "script": "Debug.explain(doc.goals)"
    }
  }
}

by responding

{
   "error": {
      "type": "script_exception",
      "to_string": "[1, 9, 27]",
      "painless_class": "org.elasticsearch.index.fielddata.ScriptDocValues.Longs",
      "java_class": "org.elasticsearch.index.fielddata.ScriptDocValues$Longs",
      ...
   },
   "status": 500
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324550906&siteId=291194637