一、BBOSS-ElasticSearch
二、通過freeMarker建立索引庫、建立索引
2.1maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.bbossgroups.plugins</groupId>
<artifactId>bboss-elasticsearch-rest-jdbc</artifactId>
<version>6.5.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.bbossgroups.plugins</groupId>
<artifactId>bboss-elasticsearch-spring-boot-starter</artifactId>
<version>6.5.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.23.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
2.2application.yml
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<String, Object> responseMap = new HashMap<String, Object>();
//3、擷取索引庫字段類型
List<CmField> 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模闆【需要根據實際手動配置模闆】
<?xml version="1.0" encoding="UTF-8"?>
<properties>
<!--
Demo indice mapping structure
-->
<property name="${mappingName}">
<![CDATA[{
"settings": {
"number_of_shards": 6,
"index.refresh_interval": "5s"
},
"mappings": {
"properties": {
<#list esMappings as esMapping>
"${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>
}
}
}]]>
</property>
<property name="testHighlightSearch">
<![CDATA[{
"query": {
"bool": {
"must": [
## 全文檢索參考文檔 https://www.elastic.co/guide/en/elasticsearch/reference/6.2/full-text-queries.html
<#list esMappings as esMapping>
#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]
}]]>
</property>
</properties>
2.6建立ES索引
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參數
SpringBoot內建BBOSS-ElasticSearch實作ElasticSearch用戶端 4.2後端接口進行查詢
@RequestMapping(value="/selectEsIndex",method = RequestMethod.POST)
public List<Object> selectEsIndex(String indexName,String params) throws Exception {
Map map = (Map)JSONObject.parse(params);
ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/"+indexName+".xml");//初始化一個加載sql配置檔案的es用戶端接口
//執行查詢,demo為索引表,_search為檢索操作action
ESDatas<Object> esDatas = //ESDatas包含目前檢索的記錄集合,最多1000條記錄,由dsl中的size屬性指定
clientUtil.searchList(indexName+"/_search",//demo為索引表,_search為檢索操作action
"testHighlightSearch",//esmapper/demo.xml中定義的dsl語句
map,//變量參數
Object.class);//傳回的文檔封裝對象類型
System.out.println(esDatas.getDatas());
return esDatas.getDatas();
}
此次難點:1、傳值類型是否正确;2、freeMarker模闆查詢配置是否正确;