ElasticSearch DSL Script使用案例分享

[i]the best elasticsearch highlevel java rest api-----[url=https://www.oschina.net/p/bboss-elastic]bboss[/url][/i]

ElasticSearch DSL Script使用案例分享,涉及到的功能点:

[list]
[*]脚本片段使用
[*]多行文本使用
[*]添加属性字段
[/list]
[size=medium][b]1前言[/b][/size]
先看看elasticsearch官方的script dsl块文本的写法:通过一对""" """来包含块文本

{
"query": {
"function_score": {
"script_score": {
"script": {
"lang": "painless",
"source": """
int total = 0;
for (int i = 0; i < doc['goals'].length; ++i) {
total += doc['goals'][i];
}
return total;
"""
}
}
}
}
}

对应的bboss script dsl块文本的写法:通过一对@""" """来包含块文本

{
"query": {
"function_score": {
"script_score": {
"script": {
"lang": "painless",
"source": @"""
int total = 0;
for (int i = 0; i < doc['goals'].length; ++i) {
total += doc['goals'][i];
}
return total;
"""
}
}
}
}
}

bboss中管理的dsl块文本和elasticsearch官方的dsl中的块文本唯一的区别就是在开头的"""前面加了个@符号

[size=medium][b]2.定义dsl配置[/b][/size]
在demo.xml文件中增加以下配置


"params": {
"last": #[last],
"nick": #[nick]
}
]]>


"script": {
"lang": "painless",
"source": @""" ##块文本开始
ctx._source.last = params.last;
ctx._source.nick = params.nick
""",##块文本结束
@{scriptPianduan}
}
}]]>

[size=medium][b]2.执行脚本处理[/b][/size]
定义类ScriptImpl,增加方法updateDocumentByScriptPath来执行脚本

package org.bboss.elasticsearchtest.script;
/*
* Copyright 2008 biaoping.yin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import org.bboss.elasticsearchtest.crud.DocumentCRUD;
import org.frameworkset.elasticsearch.ElasticSearchHelper;
import org.frameworkset.elasticsearch.client.ClientInterface;

import java.util.HashMap;
import java.util.Map;

public class ScriptImpl {
private String mappath = "esmapper/demo.xml";

public void updateDocumentByScriptPath(){
//初始化数据,会创建type为demo的indice demo,并添加docid为2的文档
DocumentCRUD documentCRUD = new DocumentCRUD();
documentCRUD.testCreateIndice();
documentCRUD.testBulkAddDocument();
//创建加载配置文件的客户端工具,用来检索文档,单实例多线程安全
ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil(mappath);
Map params = new HashMap();
//为id为2的文档增加last和nick两个属性
params.put("last","gaudreau");
params.put("nick","hockey");
//通过script脚本为文档id为2的文档增加last和nick两个属性,为了演示效果强制refresh,实际环境慎用
clientUtil.updateByPath("demo/demo/2/_update?refresh","scriptDsl",params);
//获取更新后的文档,会看到新加的2个字段属性
String doc = clientUtil.getDocument("demo","demo","2");
System.out.println(doc);

}
}


3.一个比较复杂Script的案例

{
"query": {
"bool": {
"must": [
{
"term": {
"_id": #[id]
}
}
]
}
},
"script": {
"lang": "painless",
"source": @"""
ctx._source.is_expired_dynamic_price=params.is_expired_dynamic_price;
ctx._source.dynamic_price_template_id=params.dynamic_price_template_id;
ctx._source.dynamic_price_template.id=params.dynamic_price_template_id;
ctx._source.dynamic_price_template.code=params.dynamic_price_template_code;
ctx._source.dynamic_price_template.name=params.dynamic_price_template.name;
ctx._source.dynamic_price_template.count_products=params.dynamic_price_template.count_products;
ctx._source.dynamic_price_template.is_deleted=params.dynamic_price_template.is_deleted;
ctx._source.dynamic_price_template.user_id_update=params.dynamic_price_template.user_id_update;
ctx._source.dynamic_price_template.rules=params.ctx._source.dynamic_price_template.rules;
""",
"params": {
"is_expired_dynamic_price": #[isExpiredDynamicPrice],
"dynamic_price_template_id": #[dynamicPriceTemplateId],
"dynamic_price_template_code":#[dynamicPriceTemplate->code],
"dynamic_price_template.name":#[dynamicPriceTemplate->name],
"dynamic_price_template.count_products":#[dynamicPriceTemplate->countProducts],
"dynamic_price_template.is_deleted":#[dynamicPriceTemplate->isDeleted],
"dynamic_price_template.user_id_update":#[dynamicPriceTemplate->userIdUpdate],
"dynamic_price_template.rules":
[
#foreach($rule in $dynamicPriceTemplate.rules)
#if($velocityCount > 0),#end
{
"id": #[dynamicPriceTemplate->rules[$velocityCount].id],
"sort_num": #[dynamicPriceTemplate->rules[$velocityCount]->sortNum],
"act_expired_time_tick": #[dynamicPriceTemplate->rules[$velocityCount]->actExpiredTimeTick],
"act_expired_time_value": #[dynamicPriceTemplate->rules[$velocityCount]->actExpiredTimeValue],
"act_expired_time_unit": #[dynamicPriceTemplate->rules[$velocityCount]->actExpiredTimeUnit],
"price_new_type": #[dynamicPriceTemplate->rules[$velocityCount]->priceNewType],
"price_new_coefficient": #[dynamicPriceTemplate->rules[$velocityCount]->priceNewCoefficient],
"price_new_custom": #[dynamicPriceTemplate->rules[$velocityCount]->priceNewCustom],
"auto_set_commend": #[dynamicPriceTemplate->rules[$velocityCount].autoSetCommend],
"auto_set_tag": #[dynamicPriceTemplate->rules[$velocityCount]->autoSetTag]
}
#end
]
}
}
}
]]>


[size=medium][b]4 参考资料[/b][/size]
[url]https://my.oschina.net/bboss/blog/1556866#h3_37[/url]

[url]https://www.elastic.co/guide/en/elasticsearch/painless/6.3/painless-examples.html[/url]

[b]elasticsearch技术交流群:[/b]166471282

[b]elasticsearch微信公众号:[/b]
[img]https://static.oschina.net/uploads/space/2017/0617/094201_QhWs_94045.jpg[/img]

你可能感兴趣的:(bboss,elastic,ElasticSearch,Script,DSL,bboss)