ElasticSearch创建索引Indice和索引Index Template案例分享

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

ElasticSearch客户端框架bboss的ClientInterface 接口提供了创建索引Indice和索引IndexTemplate的方法,本文举例说明其使用方法。

1 准备工作

参考文档在项目中导入Elasticsearch客户端:集成Elasticsearch Restful API案例分享

2 定义创建Indice的dsl脚本

在配置文件-esmapper/demo.xml中定义一个名称为createDemoIndice的dsl脚本:

    <!--
        创建demo需要的索引表结构
    -->
    <property name="createDemoIndice">
        <![CDATA[{
            "settings": {
                "number_of_shards": 6,
                "index.refresh_interval": "5s"
            },
            "mappings": {
                "demo": {
                    "properties": {
                        "demoId":{
                            "type":"long"
                        },
                        "contentbody": {
                            "type": "text" ##定义text类型的全文检索字段

                        },
                        "agentStarttime": {
                            "type": "date"
                             ## ,"format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"
                        },
                        "applicationName": {
                            "type": "text",##定义text类型的全文检索字段
                            "fields": { ##定义精确查找的内部keyword字段
                                "keyword": {
                                    "type": "keyword"
                                }
                            }
                        },
                        "name": {
                            "type": "keyword"
                        }
                    }
                }
            }
        }]]>
    </property>

3 创建indice

根据上面定义的dsl脚本文件初始化ClientInterface对象,并创建索引表demo:

	public void testCreateIndice(){
	//创建加载配置文件的客户端工具,单实例多线程安全
		ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/demo.xml");
		try {
			//判读索引表demo是否存在,存在返回true,不存在返回false
			boolean exist = clientUtil.existIndice("demo");

			//如果索引表demo已经存在先删除mapping
			if(exist)
				clientUtil.dropIndice("demo");
			//创建索引表demo
			clientUtil.createIndiceMapping("demo",//索引表名称
					"createDemoIndice");//索引表mapping dsl脚本名称,在esmapper/demo.xml中定义createDemoIndice
		} catch (ElasticSearchException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

4 定义索引Template dsl脚本

通过定义索引模板,定义表结构相同,但是索引表名称不同的索引表的建表模板,通过index_patterns中对应的模式名称来匹配索引模板适用的索引表:

   <property name="demoTemplate">
        <![CDATA[{
            "index_patterns": "demo-*", ## 5.x版本中请使用语法:"template": "demo-*"
            "settings": {
                "number_of_shards": 30, ##定义分片数
                "number_of_replicas" : 2, ##定义副本数
                "index.refresh_interval": "5s" ## 定义索引写入刷新时间间隔
            },
            "mappings": {
                "demo": {
                    "properties": {
                        "contentbody": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "agentStarttime": {
                            "type": "date",
                            "format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"
                        },
                        "applicationName": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                }
            }
        }]]>
    </property>

5 创建索引表模板

	public void testCreateTempate() throws ParseException{

		ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/demo.xml");
		//创建模板
		String response = clientUtil.createTempate("demotemplate_1",//模板名称
				"demoTemplate");//模板对应的脚本名称,在esmapper/demo.xml中配置
		System.out.println("createTempate-------------------------");
		System.out.println(response);//创建结果
		//获取模板
		/**
		 * 指定模板
		 * /_template/demoTemplate_1
		 * /_template/demoTemplate*
		 * 所有模板 /_template
		 *
		 */
		String template = clientUtil.executeHttp("/_template/demotemplate_1",ClientUtil.HTTP_GET);
		System.out.println("HTTP_GET-------------------------");
		System.out.println(template);

	}

6 案例源码工程下载

https://github.com/bbossgroups/eshelloword-booter

https://gitee.com/bbossgroups/eshelloword-booter

7 参考文档

https://my.oschina.net/bboss/blog/1556866

8 开发交流

elasticsearch技术交流群:166471282

elasticsearch微信公众号:

bboss微信公众号:bbossgroups

猜你喜欢

转载自my.oschina.net/bboss/blog/1807473