[elasticsearch笔记] Scripting

本文深入探讨了Elasticsearch中使用的脚本语言,包括painless、expression和mustache,通过具体示例展示了如何利用这些脚本进行文档操作、搜索优化及字段计算,为用户提供了一种高效的数据处理和检索手段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

notes

demo

painless

PUT script_text_v1
{
 "mappings": {
   "properties": {
     "title": {
       "type": "text"
     },
     "first_name": {
       "type": "text",
       "store": true
     },
     "last_name": {
       "type": "text",
       "store": true
     }
   }
 }
}

PUT script_text_v1/_doc/1?refresh
{
 "title": "Mr",
 "first_name": "Barry",
 "last_name": "White"
}

GET script_text_v1/_search
{
 "script_fields": {
   "source": {
     "script": {
       "lang": "painless",
       "source": "params._source.title + ' ' + params._source.first_name + ' ' + params._source.last_name" 
     }
   },
   "stored_fields": {
     "script": {
       "lang": "painless",
       "source": "params._fields['first_name'].value + ' ' + params._fields['last_name'].value"
     }
   }
 }
}

expression

GET kibana_sample_data_logs/_search
{
 "script_fields": {
   "double_bytes": {
     "script": {
       "lang": "expression",
       "source": "doc['bytes'] * multiplier",
       "params": {
         "multiplier":2
       }
     }
   }
 }
}

PUT script_text/_doc/1?refresh
{
 "text": "quick brown fox",
 "popularity": 1
}

PUT script_text/_doc/2?refresh
{
 "text": "quick fox",
 "popularity": 5
}

GET script_text/_search
{
 "query": {
   "function_score": {
     "query": {
       "match": {
         "text": "quick brown fox"
       }
     },
     "script_score": {
       "script": {
         "lang": "expression",
         "source": "_score * doc['popularity']"
       }
     }
   }
 }
}

GET script_text/_search
{
 "script_fields": {
   "popularity": {
     "script": {
       "lang": "expression",
       "source": "doc['popularity']*markup",
       "params": {
         "markup":0.2
       }
     }
   }
 }
}

PUT script_text_v1
{
 "mappings": {
   "properties": {
     "title": { 
       "type": "text"
     },
     "first_name": {
       "type": "text",
       "store": true
     },
     "last_name": {
       "type": "text",
       "store": true
     }
   }
 }
}

mustache

点击查看详情

DIY plugin in java

write plugin by java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值