elasticsearch_script_02
文章目錄
- 1. painless簡介
- 2. script使用
- 1. Ingest processor
- 1.使用的變量
- 2. 可以產生的影響Side Effects
- 3. 返回Return
- 4.使用樣例
- 2. Update
- 1.使用的變量
- 2. 可以產生的影響Side Effects
- 3. 返回Return
- 4.使用樣例
- 3. Update by query
- 1.使用的變量
- 2. 可以產生的影響Side Effects
- 3. 返回Return
- 4.使用樣例
- 4. Reindex
- 1.使用的變量
- 2. 可以產生的影響Side Effects
- 3. 返回Return
- 4.使用樣例
- 5. Sort
- 1.使用的變量
- 2. 可以產生的影響Side Effects
- 3. 返回Return
- 4.使用樣例
- 6. Score
- 1.使用的變量
- 2. 可以產生的影響Side Effects
- 3. 返回Return
- 4.使用樣例
- 7. Field
- 1.使用的變量
- 2. 可以產生的影響Side Effects
- 3. 返回Return
- 4.使用樣例
- 8. Filter
- 1.使用的變量
- 2. 可以產生的影響Side Effects
- 3. 返回Return
- 4.使用樣例
- 9. search template
- 1. 使用存儲的mustache進行查詢
- 2. 直接使用mustache查詢
1. painless簡介
painless 在不同的上下文中能夠使用的文檔的字段等都是不同的,需要特殊處理
在上下文中評估painless腳本。每個上下文都有可用作局部變量的值,控制可用類的白名單以及這些類(API)中的方法和字段,以及是否以及返回哪種類型的值。
通常在下表的上下文之一內執行painless腳本。請注意,這不一定是完整的列表,因為自定義插件和專用代碼可能會定義使用painless腳本的新方法。
painless 總共可以運行在以下的各種上下文當中,每個上下文的介紹分為兩部分,一部分是painless script的介紹(更全面),一部分是es文檔的介紹(舉例更生動)
感覺相對更常用的Ingest processor Update Update by query Reindex Sort Score Field Filter感覺不是很常用的 Metric aggregation initialization Metric aggregation map Metric aggregation combine Metric aggregation reduce Bucket script aggregation Bucket selector aggregationSimilarity Weight Minimum should match Watcher condition Watcher transform2. script使用
1. Ingest processor
1.使用的變量
params (Map, read-only): User-defined parameters passed in as part of the query.
ctx[’_index’] (String): The name of the index.
ctx[’_type’] (String): The type of document within an index.
ctx (Map): Contains extracted JSON in a Map and List structure for the fields that are part of the document.
2. 可以產生的影響Side Effects
ingest 對應的是doc還沒有進行存儲的時候,所以這個時候沒有_source字段,直接使用ctx.field就可以獲取到對應的字段
3. 返回Return
void
No expected return value.
4.使用樣例
修改index name
PUT _ingest/pipeline/my_index {"description": "use index:my_index and type:_doc","processors": [{"script": {"source": """ctx._index = 'my_index';ctx._type = '_doc';"""}}] }PUT any_index/_doc/1?pipeline=my_index {"message": "text" }返回{"_index": "my_index","_type": "_doc","_id": "1","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"_seq_no": 89,"_primary_term": 1, }可以看到index的name變了修改文檔字段,增加減少
{"script": {"lang": "painless","source": "ctx.field_a_plus_b_times_c = (ctx.field_a + ctx.field_b) * params.param_c","params": {"param_c": 10}} }2. Update
1.使用的變量
params (Map, read-only): User-defined parameters passed in as part of the query.
ctx[‘op’] (String): The name of the operation.
ctx[’_routing’] (String, read-only): The value used to select a shard for document storage.
ctx[’_index’] (String, read-only): The name of the index.
ctx[’_type’] (String, read-only): The type of document within an index.
ctx[’_id’] (int, read-only): The unique document id.
ctx[’_version’] (int, read-only): The current version of the document.
ctx[’_now’] (long, read-only): The current timestamp in milliseconds.
ctx[’_source’] (Map): Contains extracted JSON in a Map and List structure for the fields existing in a stored document.
2. 可以產生的影響Side Effects
ctx[‘op’]: Use the default of index to update a document. Set to none to specify no operation or delete to delete the current document from the index.
ctx[’_source’]: Modify the values in the Map/List structure to add, modify, or delete the fields of a document.
3. 返回Return
void
4.使用樣例
POST /seats/_update/3 {"script": {"source": "ctx._source.sold = true; ctx._source.cost = params.sold_cost","lang": "painless","params": {"sold_cost": 26}} } PUT test/_doc/1 {"counter" : 1,"tags" : ["red"] }修改一個field的值 POST test/_update/1 {"script" : {"source": "ctx._source.counter += params.count","lang": "painless","params" : {"count" : 4}} }增加一個數組field POST test/_update/1 {"script" : {"source": "ctx._source.tags.add(params.tag)","lang": "painless","params" : {"tag" : "blue"}} }移除一個數組元素,感覺這個就是在寫代碼,😂
POST test/_update/1 {"script" : {"source": "if (ctx._source.tags.contains(params.tag)) { ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag)) }","lang": "painless","params" : {"tag" : "blue"}} } 增加一個field POST test/_update/1 {"script" : "ctx._source.new_field = 'value_of_new_field'" }刪除一個field POST test/_update/1 {"script" : "ctx._source.remove('new_field')" }刪除一個文檔 POST test/_update/1 {"script" : {"source": "if (ctx._source.tags.contains(params.tag)) { ctx.op = 'delete' } else { ctx.op = 'none' }","lang": "painless","params" : {"tag" : "green"}} }使用存儲后的script
GET hamlet/_doc/1 數據為 "_source" : {"line_number" : "2","speaker" : "FRANCISCO","text_entry" : "Nay, answer me:stand, and unfold yourself."}直接使用script處理 POST hamlet/_update/1 {"script":{"lang":"painless","source": "ctx._source.name=params.nn","params": {"nn":"hahah"}} }使用store script處理 PUT _scripts/update_add_f {"script":{"lang":"painless","source": "ctx._source.name02=params.nn02"} }POST hamlet/_update/1 {"script":{ ## 這個地方和下面講到的search template中使用的方式不一樣,這個看起來更規范"id":"update_add_f","params": {"nn02":"you are good boy"}} }GET hamlet/_doc/1"_source" : {"line_number" : "2","speaker" : "FRANCISCO","text_entry" : "Nay, answer me:stand, and unfold yourself.","name" : "hahah","name02" : "you are good boy"}3. Update by query
1.使用的變量
params (Map, read-only): User-defined parameters passed in as part of the query.
ctx[‘op’] (String): The name of the operation.
ctx[’_routing’] (String, read-only): The value used to select a shard for document storage.
ctx[’_index’] (String, read-only): The name of the index.
ctx[’_type’] (String, read-only): The type of document within an index.
ctx[’_id’] (int, read-only): The unique document id.
ctx[’_version’] (int, read-only): The current version of the document.
ctx[’_source’] (Map): Contains extracted JSON in a Map and List structure for the fields existing in a stored document.
2. 可以產生的影響Side Effects
ctx[‘op’]: Use the default of index to update a document. Set to none to specify no operation or delete to delete the current document from the index.
ctx[’_source’]: Modify the values in the Map/List structure to add, modify, or delete the fields of a document.
3. 返回Return
void
4.使用樣例
POST /seats/_update_by_query {"query": {"bool": {"filter": [{"range": {"row": {"lte": 3}}},{"match": {"sold": false}}]}},"script": {"source": "ctx._source.cost -= params.discount","lang": "painless","params": {"discount": 2}} } POST twitter/_update_by_query {"script": {"source": "ctx._source.likes++","lang": "painless"},"query": {"term": {"user": "kimchy"}} }4. Reindex
1.使用的變量
params (Map, read-only): User-defined parameters passed in as part of the query.
ctx[‘op’] (String): The name of the operation.
ctx[’_routing’] (String): The value used to select a shard for document storage.
ctx[’_index’] (String): The name of the index.
ctx[’_type’] (String): The type of document within an index.
ctx[’_id’] (int, read-only): The unique document id.
ctx[’_version’] (int): The current version of the document.
ctx[’_source’] (Map): Contains extracted JSON in a Map and List structure for the fields existing in a stored document.
2. 可以產生的影響Side Effects
ctx[‘op’]: Use the default of index to update a document. Set to none to specify no operation or delete to delete the current document from the index.
ctx[’_routing’]: Modify this to change the routing value for the current document.
ctx[’_index’]: Modify this to change the destination index for the current document.
ctx[’_type’]: Modify this to change the type for the current document.
ctx[’_id’]: Modify this to change the id for the current document.
ctx[’_version’] (int): Modify this to modify the version for the current document.
ctx[’_source’]: Modify the values in the Map/List structure to add, modify, or delete the fields of a document.
3. 返回Return
void
4.使用樣例
修改meta field
POST _reindex {"source": {"index": "twitter"},"dest": {"index": "new_twitter","version_type": "external"},"script": {"source": "if (ctx._source.foo == 'bar') {ctx._version++; ctx._source.remove('foo')}","lang": "painless"} }修改字段名,注意,這個是rename,在update_by_query中也是可以使用的
POST test/_doc/1?refresh {"text": "words words","flag": "foo" }POST _reindex {"source": {"index": "test"},"dest": {"index": "test2"},"script": {"source": "ctx._source.tag = ctx._source.remove(\"flag\")"} }5. Sort
1.使用的變量
params (Map, read-only): User-defined parameters passed in as part of the query.
doc (Map, read-only): Contains the fields of the current document. For single-valued fields, the value can be accessed via doc[‘fieldname’].value. For multi-valued fields, this returns the first value; other values can be accessed via doc[‘fieldname’].get(index)
_score (double read-only): The similarity score of the current document.
2. 可以產生的影響Side Effects
3. 返回Return
double
The score for the specified document.
這個并不會影響doc的_score字段,只是返回一個double用來進行sort排序
4.使用樣例
GET /_search {"query" : {"term" : { "sold" : "true" }},"sort" : {"_script" : {"type" : "number","script" : {"lang": "painless","source": "doc['theatre'].value.length() * params.factor","params" : {"factor" : 1.1}},"order" : "asc"}} }6. Score
1.使用的變量
params (Map, read-only): User-defined parameters passed in as part of the query.
doc (Map, read-only): Contains the fields of the current document. For single-valued fields, the value can be accessed via doc[‘fieldname’].value. For multi-valued fields, this returns the first value; other values can be accessed via doc[‘fieldname’].get(index)
_score (double read-only): The similarity score of the current document.
2. 可以產生的影響Side Effects
3. 返回Return
double
The score for the current document.
4.使用樣例
不使用script的情況下GET /_search {"query": {"function_score": {"query": { "match_all": {} },"boost": "5","random_score": {}, "boost_mode":"multiply"}} }使用script_scoreGET /seats/_search {"query": {"function_score": {"query": {"match": { "sold": "false" }},"script_score" : {"script" : {"source": "1.0 / doc['row'].value"}}}} } GET /_search {"query": {"function_score": {"query": {"match": { "message": "elasticsearch" }},"script_score" : {"script" : {"source": "Math.log(2 + doc['likes'].value)"}}}} }GET /_search {"query": {"function_score": {"query": {"match": { "message": "elasticsearch" }},"script_score" : {"script" : {"params": {"a": 5,"b": 1.2},"source": "params.a / Math.pow(params.b, doc['likes'].value)"}}}} }7. Field
1.使用的變量
params (Map, read-only): User-defined parameters passed in as part of the query.
doc (Map, read-only): Contains the fields of the specified document where each field is a List of values.
params[’_source’] (Map, read-only): Contains extracted JSON in a Map and List structure for the fields existing in a stored document.
2. 可以產生的影響Side Effects
3. 返回Return
Object
The customized value for each document.
4.使用樣例
GET seats/_search {"query" : {"match_all": {}},"script_fields" : {"day-of-week" : {"script" : {"source": "doc['datetime'].value.getDayOfWeek()"}},"number-of-actors" : {"script" : {"source": "params['_source']['actors'].length"}}} } 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}}}} }GET /_search{"query" : {"match_all": {}},"script_fields" : {"test1" : {"script" : "params['_source']['message']"}}}8. Filter
1.使用的變量
params (Map, read-only)
User-defined parameters passed in as part of the query.
doc (Map, read-only)
Contains the fields of the current document where each field is a List of values.
2. 可以產生的影響Side Effects
3. 返回Return
boolean
Return true if the current document should be returned as a result of the query, and false otherwise.
4.使用樣例
GET evening/_search {"query": {"bool" : {"filter" : {"script" : {"script" : {"source" : "doc['sold'].value == false && doc['cost'].value < params.cost","params" : {"cost" : 18}}}}}} } GET /_search {"query": {"bool" : {"filter" : {"script" : {"script" : {"source": "doc['num1'].value > 1","lang": "painless"}}}}} }GET /_search {"query": {"bool" : {"filter" : {"script" : {"script" : {"source" : "doc['num1'].value > params.param1","lang" : "painless","params" : {"param1" : 5}}}}}} }9. search template
search template使用的mustache,不是painless,需要注意
hamlet的數據為
1. 使用存儲的mustache進行查詢
PUT _scripts/query_halmet {"script": {"lang": "mustache","source": """{"query": {"match": {"line_number.keyword": "{{line}}" ## 取值方式和painless是有很大的區別的}}} """} }GET hamlet/_search/template {"id": "query_halmet", ## 注意這里直接使用的id,不是使用的script標簽"params": {"line": "1"} }可以看到在使用store template時候直接將最外層的script key去掉了,直接使用了內部的id,params參數。
2. 直接使用mustache查詢
注意uri是template結尾 GET hamlet/_search/template {"source": """ ## 注意這里使用的直接是source,沒有script標簽{"query": {"match": {"line_number.keyword": "{{line}}"}}} ""","params": {"line": "1"} }可以看到在使用mustache template時候也是直接將最外層的script key去掉了,直接使用了內部的source和params參數。
總結
以上是生活随笔為你收集整理的elasticsearch_script_02的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: elasticsearch_script
- 下一篇: spark-on-yarn安装cento