使用script,可以在ES中计算自定义的表达式。script language默认是Painless
script没法使用es的index structure或者相关的优化,所以通常脚本查询会比较慢。

ES会在每一个满足query条件或者聚合结果的文档上计算script表达式,因此一个script有可能被执行上百万 甚至上亿次,所以脚本一定要快

获取_score

//script获取_score

  1. PUT my-index-000001/_doc/1?refresh
  2. {
  3. "text": "quick brown fox",
  4. "popularity": 1
  5. }
  6. PUT my-index-000001/_doc/2?refresh
  7. {
  8. "text": "quick fox",
  9. "popularity": 5
  10. }
  11. ##在function_score, script-based sorting 和aggregations中可以获取文档的评分。直接通过_score获取
  12. GET my-index-000001/_search
  13. {
  14. "query": {
  15. "function_score": {
  16. "query": {
  17. "match": {
  18. "text": "quick brown fox"
  19. }
  20. },
  21. "script_score": {
  22. "script": {
  23. "lang": "expression",
  24. "source": "_score * doc['popularity']"
  25. }
  26. }
  27. }
  28. }
  29. }

获取field values

///script获取字段的值的方式有: doc-values, _source, stored fields

doc_values获取

注意:上面三种方法都能获取到field的值,但是每种方式都有自己适用的场景。
///使用doc[‘field_name’]语法获取
///doc_value用来获取一个field的所有值非常快。适用于对单个字段的查找和聚合

PUT my-index-000001/_doc/1?refresh
{
  "cost_price": 100
}

GET my-index-000001/_search
{
  "script_fields": {
    "sales_price": {
      "script": {
        "lang":   "expression",
        "source": "doc['cost_price'] * markup",
        "params": {
          "markup": 0.2
        }
      }
    }
  }
}

_source获取

///params._source.field_name语法可以获取字段值,但是尽量还是使用doc_values。因为_source字段就是用来给返回一个文档的多个字段做优化的,而doc_values专门是针对取一个指定字段的value而做的优化。
通常_source用来返回一个由多个字段组合而来的新字段,因此如果一个script计算中需要同时使用文档的两个或以上的字段使用_source获取field value比较方便。
但是如果使用doc_value时,需要去分别查找两个field的doc_value表去查询

GET my-index-000001/_search
{
  "script_fields": {
    "full_name": {
      "script": {
        "lang": "painless",
        "source": "params._source.first_name + ' ' + params._source.last_name"
      }
    }
  }
}

stored field获取

当字段开启了store:true那么可以通过_fields['field_name'].value or _fields['field_name']取字段值

_source字段实际上一个特殊的stored field。所以通过_source字段和stored取field 值实际上性能差不多。
通过stored去取field value唯一有意义的场景是:当文档内容特别长的时候用stored去取开销会小一点。但是store需要打开,实际上可以忽略这种取field value的方法。

how to use script

update script

A script used in the update, update-by-query, or reindex API will have access to the ctxvariable which exposes:

ctx._source Access to the document _source field.
ctx.op The operation that should be applied to the document: index or delete.
ctx._index etc Access to document metadata fields, some of which may be read-only.

search and aggregation scripts

search

GET /_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "test1": {
      "script": {
        "lang": "painless",
        "source": "doc['price'].value * 2"
      }
    },
    "test2": {
      "script": {
        "lang": "painless",
        "source": "doc['price'].value * params.factor",
        "params": {
          "factor": 2.0
        }
      }
    }
  }
}

sort

GET /_search
{
  "query": {
    "term": { "user": "kimchy" }
  },
  "sort": {
    "_script": {
      "type": "number",
      "script": {
        "lang": "painless",
        "source": "doc['field_name'].value * params.factor",
        "params": {
          "factor": 1.1
        }
      },
      "order": "asc"
    }
  }
}


GET /my_test_scores/_search
{
  "query": {
    "term": {
      "grad_year": "2099"
    }
  },
  "sort": [
    {
      "_script": {
        "type": "number",
        "script": {
          "source": "doc['math_score'].value + doc['verbal_score'].value"
        },
        "order": "desc"
      }
    }
  ]
}

aggregation

某些聚合支持script语法

GET /my-index-000001/_search
{
  "aggs": {
    "my-agg-name": {
      "histogram": {
        "field": "my-field",
        "interval": 1000,
        "script": "_value / 1000",  指定需要执行的计算,这里需要把_value/1000
        "value_type": "long"
      }
    }
  }
}

script and security

These languages are less flexible, but typically have higher performance for certain tasks.

Language Sandboxed Required plugin Purpose
expression yes built-in fast custom ranking and sorting
mustache yes built-in templates
java n/a you write it! expert API

Languages that are sandboxed are designed with security in mind. However, non- sandboxed languages can be a security issue, please read Scripting and securityfor more details.