ElasticSearch DSL Script使用案例分享

the best elasticsearch highlevel java rest api-----bboss      

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

  • 脚本片段使用
  • 多行文本使用
  • 添加属性字段

1前言
先看看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中的块文本唯一的区别就是在开头的"""前面加了个@符号

2.定义dsl配置
在demo.xml文件中增加以下配置
    <property name="scriptPianduan">
        <![CDATA[
            "params": {
              "last": #[last],
              "nick": #[nick]
            }
        ]]>
    </property>
    <property name="scriptDsl">
        <![CDATA[{
          "script": {
            "lang": "painless",
            "source": @"""  ##块文本开始
              ctx._source.last = params.last;
              ctx._source.nick = params.nick
            """,##块文本结束
            @{scriptPianduan}
          }
        }]]>
    </property>

2.执行脚本处理
定义类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<String,Object> params = new HashMap<String,Object>();
		//为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 参考资料
https://my.oschina.net/bboss/blog/1556866#h3_37

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

elasticsearch技术交流群:166471282

elasticsearch微信公众号:

猜你喜欢

转载自yin-bp.iteye.com/blog/2425839