elasticsearch_script_01
文章目錄
- 1. script 簡述
- 1. script的類型和作用
- 2. script使用的方式
- 2. script的使用場景
- 1. update scripts
- 2. search 和agg 中使用script
- 3. search中獲取doc的打分數據
- 3. script靈活使用的一些理解
- 4. script的使用方式
- 5. 樣例
- 1. 在search中使用 script_score功能
- 2. 獲取field的value,通過doc values
- 3. 獲取field value 通過_source字段
- 4. 獲取field value 通過store field
1. script 簡述
elasticsearch的script使用比較靈活而且廣泛,有必要先了解和學習一下
1. script的類型和作用
script 可以讓你自定義的返回一些field,也可以讓你自定義score的計算。
在es中可以使用的script的類型有三種,就像是三種編程語言一樣
painless 是效率比較好,也比較靈活的一種。后面介紹script的功能的時候也主要以這個為主進行介紹。
2. script使用的方式
"script": {"lang": "...", "source" | "id": "...", "params": { ... } }source 部分是script的代碼或者是 使用id,調用存儲的script的id。
2. script的使用場景
在不同的使用場景下script能夠使用的doc的field是不相同的。
1. update scripts
在update, update-by-query, reindex API可以通過ctx前綴獲取到
2. search 和agg 中使用script
script fields 會在每個搜索命中的doc執行一次,其他在search和agg中使用的腳本將針對可能與search或agg匹配的每個文檔執行一次,所有的普通字段值可以通過下面方式獲取
同時還可以在一些特定的查詢中獲取比如score等字段
3. search中獲取doc的打分數據
獲取和修改文檔的_score字段只能夠在 function_score query, script-based sorting, 或者 aggregations 當中
具體的使用方式有不太相同,在function_score中使用 script_score屬性,在script-based sorting 中使用_script 屬性
script語法,點號“.”表示獲取字段,中括號表示獲取數組“[]”,對應的樣例就是
params._source.first_name
params._fields[‘first_name’].value
3. script靈活使用的一些理解
script可以簡單的理解像java一樣
常見的數據類型有
script的邏輯表達式只有兩種
語法
int x; List y; add(ele) get(1) y[1]=5 y.length() int x, y = 5, z; def d; int i = 10; float[] f; f[0] f.length 數組類型 Map[][] m; get(key) m['a']='b' m.a='a_value' String r = "some text"這個語法有點類似python的語法,比較簡練
4. script的使用方式
5. 樣例
1. 在search中使用 script_score功能
在 function_score 查詢, script-based 排序(sorting), 或者 aggregations 有能力獲取到 _score 字段
需要使用script_score 查詢。
比如下面 是在function_score 中改變_score,其他類型的將來遇到再看
返回
{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 3,"relation" : "eq"},"max_score" : 4.0225573,"hits" : [{"_index" : "my_index","_type" : "_doc","_id" : "3","_score" : 4.0225573,"_source" : {"text" : "quick fox","popularity" : 50}},{"_index" : "my_index","_type" : "_doc","_id" : "1","_score" : 1.2483562,"_source" : {"text" : "quick brown fox","popularity" : 1}},{"_index" : "my_index","_type" : "_doc","_id" : "2","_score" : 0.4022557,"_source" : {"text" : "quick fox","popularity" : 5}}]} }script base sorting
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"}} }agg操作等后面再總結。
2. 獲取field的value,通過doc values
使用 doc[‘field_name’]
但是因為string.text field 沒有doc-values 所以如果想用這種方式獲取的話需要打開 fielddata 設置,這是一個很昂貴的操作。所以盡量不要使用script獲取text field的內容。
3. 獲取field value 通過_source字段
通過_source.field_name 可以獲取到source中對應的字段,但是相對來說比doc-values的要慢很多。
PUT my_index {"mappings": {"properties": {"first_name": {"type": "text"},"last_name": {"type": "text"}}} }PUT my_index/_doc/1?refresh {"first_name": "Barry","last_name": "White" }GET my_index/_search {"script_fields": {"full_name": {"script": {"lang": "painless","source": "params._source.first_name + ' ' + params._source.last_name"}}}返回主體 "hits" : [{"_index" : "my_index","_type" : "_doc","_id" : "1","_score" : 1.0,"fields" : {"full_name" : ["Barry White"]}}]上面的script.source中還必須要帶params。
4. 獲取field value 通過store field
store field是指那些在mapping中定義的mapping param 為 store:true的field
可以使用 _fields[‘field_name’].value or _fields[‘field_name’] 語法
source field也是 store field的一種,所以_source field的性能和store field的性能接近,使用store filed的唯一場景是 _source特別大,我們并不需要獲取全部的_source的時候 使用store field會更好。
總結
以上是生活随笔為你收集整理的elasticsearch_script_01的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spark写入elasticsearch
- 下一篇: elasticsearch_script