2.2application.ymlorg.springframework.boot spring-boot-starter-data-elasticsearch org.slf4j slf4j-log4j12 com.bbossgroups.plugins bboss-elasticsearch-rest-jdbc 6.5.6 org.slf4j slf4j-log4j12 com.bbossgroups.plugins bboss-elasticsearch-spring-boot-starter 6.5.6 org.slf4j slf4j-log4j12 org.xerial sqlite-jdbc 3.23.1 compile org.springframework.boot spring-boot-starter-freemarker org.springframework.boot spring-boot-starter-test
spring:
elasticsearch:
bboss:
elasticPassword: 123456
elasticUser: elastic
elasticsearch:
dateFormat: yyyy.MM.dd
discoverHost: false
rest:
hostNames: 127.0.0.1:9200
scrollBlockedWaitTimeout: 0
scrollThreadCount: 200
scrollThreadQueue: 200
showTemplate: false
sliceScrollBlockedWaitTimeout: 0
sliceScrollThreadCount: 100
sliceScrollThreadQueue: 100
timeZone: Asia/Shanghai
http:
connectionRequestTimeout: 5000
customHttpRequestRetryHandler: org.frameworkset.spi.remote.http.ConnectionResetHttpRequestRetryHandler
defaultMaxPerRoute: 200
hostnameVerifier:
keepAlive: 3600000
keyPassword:
keystore:
maxHeaderCount: 200
maxLineLength: -1
maxTotal: 400
retryTime: 1
retryInterval: 1000
soKeepAlive: false
soReuseAddress: false
staleConnectionCheckEnabled: false
timeToLive: 3600000
timeoutConnection: 5000
timeoutSocket: 5000
validateAfterInactivity: 2000
dslfile:
refreshInterval: -1
2.3接口方法
@RequestMapping(value="/createEsIndexMapping",method = RequestMethod.POST)
public String createEsIndexMapping(@RequestBody CmIndexes cmIndexes) throws Exception {
String str = "";
//1、第一步使用freeMarker生成xml文件
String cmIndex = esUtils.createXml(cmIndexes);
//2、第二步创建ES索引
String mapping = createIndiceMapping(cmIndexes);
//判断是否存在
if("1".equals(mapping)){
str = "索引库已经存在!!!";
return str;
}
return cmIndex;
}
2.4freeMarker生成xml文件
public String createXml(CmIndexes cmIndexes) throws IOException {
Writer w = null;
String str = "";
try {
//1、获取xmlTemplate文件夹的当前路径
URL url = Thread.currentThread().getContextClassLoader().getResource("templates");
String path = url.getPath();
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");//解决写入到xml文件出现乱码问题
configuration.setDirectoryForTemplateLoading(new File(path));
//2、获取到freeMarker模板
Template template = configuration.getTemplate("es.ftl","utf-8");
Map responseMap = new HashMap();
//3、获取索引库字段类型
List fieldList = cmIndexes.getFieldList();//获取到映射配置文件
responseMap.put("esMappings",fieldList);//设置
responseMap.put("mappingName",cmIndexes.getIndexesMappingName());//设置映射名称
responseMap.put("indexName",cmIndexes.getIndexesName());//设置索引名称
str = route+cmIndexes.getIndexesName()+".xml";
File file = new File(str);
FileOutputStream f = new FileOutputStream(file);
w = new OutputStreamWriter(f,"utf-8");
template.process(responseMap, w);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "文件创建失败";
}finally{
w.close();
}
return "创建成功";
}
2.5freeMarker模板【需要根据实际手动配置模板】
2.6创建ES索引"${esMapping.fieldName}":{ "type":"${esMapping.indexesFieldType}"<#if esMapping.isDate == 1>, "format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"#if> }<#if esMapping_has_next>,#if> #list> } } }]]> #if($${esMapping.fieldName} && !$${esMapping.fieldName}.equals("")) <#if esMapping.isDate == 1> { ## 时间范围检索,返回对应时间范围内的记录,接受long型的值 "range": { "${esMapping.fieldName}":{ "gte": #[startTime],##统计开始时间 "lt": #[endTime], ##统计截止时间 } } }, #if> <#if esMapping.isDate == 2> { "match" : { "${esMapping.fieldName}":{ "query":#[${esMapping.fieldName}], "operator": "and" } } }, #if> #end #list> { "match_all": { } } ] } }, ## 分页起点 "from":#[from], ## 最多返回size条记录 "size":#[size] }]]>
public String createIndiceMapping(CmIndexes cmIndexes){
String str = "";
//1、创建es客户端对象
ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("./esmapper/"+cmIndexes.getIndexesName()+".xml");
//2、查询该索引是否存在
boolean exist = clientUtil.existIndice(cmIndexes.getIndexesName());
//3、如果存在,则删除索引表
if (exist){
str = "1";
return str;
}
//4、不存在,则创建索引
str = clientUtil.createIndiceMapping(cmIndexes.getIndexesName(), cmIndexes.getIndexesMappingName());
return str;
}
三、删除索引以及freeMarker文件
@RequestMapping(value="/deleteEsIndex",method = RequestMethod.POST)
public String deleteEsIndex(@RequestParam String indexName){
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
try {
//1、删除索引
//删除索引表
String indice = clientUtil.dropIndice(indexName);
//删除上一步创建的ftl文件 route是在pom.xml里面设置的路径
String fileUrl = route+indexName+".xml";
File file = new File(fileUrl);
boolean delete = file.delete();
if(delete == false){
return "删除失败";
}
}catch (Exception e){
e.printStackTrace();
}
return "删除成功";
}
3.1单个删除
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();
clientUtil.deleteDocument("索引名称","文档类型","id");
3.2新增更新
clientUtil.addDocument("索引名称",“内容”);
clientUtil.addDocumentWithId("索引名称","文档类型","内容","内容id");
四、ES查询
4.1首先前端传JSON参数
4.2后端接口进行查询
@RequestMapping(value="/selectEsIndex",method = RequestMethod.POST)
public List
此次难点:1、传值类型是否正确;2、freeMarker模板查询配置是否正确;
ElasticSearch入门学习笔记(一)概念篇
ElasticSearch入门学习笔记(二)软件安装篇
ElasticSearch入门学习笔记(三)SpringBoot整合篇
阿里云Docker安装ESES_Head安装部署logstash导mysql数据入ElasticSearch



