ElasticSearch creates index Indice and index Index Template case sharing

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

The ClientInterface interface of the ElasticSearch client framework bboss provides a method for creating an index Indice and an index IndexTemplate. This article illustrates how to use it.

1 Preparations

Refer to the documentation to import the Elasticsearch client into the project: Integration of Elasticsearch Restful API Case Sharing

2 Define the dsl script for creating Indice

In the configuration file - esmapper/demo.xml define a dsl script named createDemoIndice:

    <!--
        创建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 Create an indice

Initialize the ClientInterface object according to the dsl script file defined above, and create an index table 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 Define the index Template dsl script

By defining an index template, the table creation template of the index table with the same structure but different index table names is defined, and the index table applicable to the index template is matched by the corresponding pattern name in 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 Create an index table template

	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 Case source code project download

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

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

7 Reference documents

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

8 Development Communication

elasticsearch technical exchange group: 166471282

Elasticsearch WeChat public account:

bboss WeChat public account: bbossgroups

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325342790&siteId=291194637