526a33485c48dcf4fbe7b765d5f07cd50668c1bf.svn-base 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. package org.jeecg.common.es;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.jeecg.common.util.RestUtil;
  7. import org.jeecg.common.util.oConvertUtils;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.http.HttpHeaders;
  10. import org.springframework.http.HttpMethod;
  11. import org.springframework.http.HttpStatus;
  12. import org.springframework.http.ResponseEntity;
  13. import org.springframework.stereotype.Component;
  14. import java.util.*;
  15. /**
  16. * 关于 ElasticSearch 的一些方法(创建索引、添加数据、查询等)
  17. *
  18. * @author sunjianlei
  19. */
  20. @Slf4j
  21. @Component
  22. public class JeecgElasticsearchTemplate {
  23. /** es服务地址 */
  24. private String baseUrl;
  25. private final String FORMAT_JSON = "format=json";
  26. /** Elasticsearch 的版本号 */
  27. private String version = null;
  28. // ElasticSearch 最大可返回条目数
  29. public static final int ES_MAX_SIZE = 10000;
  30. public JeecgElasticsearchTemplate(@Value("${jeecg.elasticsearch.cluster-nodes}") String baseUrl, @Value("${jeecg.elasticsearch.check-enabled}") boolean checkEnabled) {
  31. log.debug("JeecgElasticsearchTemplate BaseURL:" + baseUrl);
  32. if (StringUtils.isNotEmpty(baseUrl)) {
  33. this.baseUrl = baseUrl;
  34. // 验证配置的ES地址是否有效
  35. if (checkEnabled) {
  36. try {
  37. this.getElasticsearchVersion();
  38. log.info("ElasticSearch 服务连接成功");
  39. log.info("ElasticSearch version: " + this.version);
  40. } catch (Exception e) {
  41. this.version = "";
  42. log.warn("ElasticSearch 服务连接失败,原因:配置未通过。可能是BaseURL未配置或配置有误,也可能是Elasticsearch服务未启动。接下来将会拒绝执行任何方法!");
  43. }
  44. }
  45. }
  46. }
  47. /**
  48. * 获取 Elasticsearch 的版本号信息,失败返回null
  49. */
  50. private void getElasticsearchVersion() {
  51. if (this.version == null) {
  52. String url = this.getBaseUrl().toString();
  53. JSONObject result = RestUtil.get(url);
  54. if (result != null) {
  55. JSONObject v = result.getJSONObject("version");
  56. this.version = v.getString("number");
  57. }
  58. }
  59. }
  60. public StringBuilder getBaseUrl(String indexName, String typeName) {
  61. typeName = typeName.trim().toLowerCase();
  62. return this.getBaseUrl(indexName).append("/").append(typeName);
  63. }
  64. public StringBuilder getBaseUrl(String indexName) {
  65. indexName = indexName.trim().toLowerCase();
  66. return this.getBaseUrl().append("/").append(indexName);
  67. }
  68. public StringBuilder getBaseUrl() {
  69. return new StringBuilder("http://").append(this.baseUrl);
  70. }
  71. /**
  72. * cat 查询ElasticSearch系统数据,返回json
  73. */
  74. public <T> ResponseEntity<T> _cat(String urlAfter, Class<T> responseType) {
  75. String url = this.getBaseUrl().append("/_cat").append(urlAfter).append("?").append(FORMAT_JSON).toString();
  76. return RestUtil.request(url, HttpMethod.GET, null, null, null, responseType);
  77. }
  78. /**
  79. * 查询所有索引
  80. * <p>
  81. * 查询地址:GET http://{baseUrl}/_cat/indices
  82. */
  83. public JSONArray getIndices() {
  84. return getIndices(null);
  85. }
  86. /**
  87. * 查询单个索引
  88. * <p>
  89. * 查询地址:GET http://{baseUrl}/_cat/indices/{indexName}
  90. */
  91. public JSONArray getIndices(String indexName) {
  92. StringBuilder urlAfter = new StringBuilder("/indices");
  93. if (!StringUtils.isEmpty(indexName)) {
  94. urlAfter.append("/").append(indexName.trim().toLowerCase());
  95. }
  96. return _cat(urlAfter.toString(), JSONArray.class).getBody();
  97. }
  98. /**
  99. * 索引是否存在
  100. */
  101. public boolean indexExists(String indexName) {
  102. try {
  103. JSONArray array = getIndices(indexName);
  104. return array != null;
  105. } catch (org.springframework.web.client.HttpClientErrorException ex) {
  106. if (HttpStatus.NOT_FOUND == ex.getStatusCode()) {
  107. return false;
  108. } else {
  109. throw ex;
  110. }
  111. }
  112. }
  113. /**
  114. * 根据ID获取索引数据,未查询到返回null
  115. * <p>
  116. * 查询地址:GET http://{baseUrl}/{indexName}/{typeName}/{dataId}
  117. *
  118. * @param indexName 索引名称
  119. * @param typeName type,一个任意字符串,用于分类
  120. * @param dataId 数据id
  121. * @return
  122. */
  123. public JSONObject getDataById(String indexName, String typeName, String dataId) {
  124. String url = this.getBaseUrl(indexName, typeName).append("/").append(dataId).toString();
  125. log.info("url:" + url);
  126. JSONObject result = RestUtil.get(url);
  127. boolean found = result.getBoolean("found");
  128. if (found) {
  129. return result.getJSONObject("_source");
  130. } else {
  131. return null;
  132. }
  133. }
  134. /**
  135. * 创建索引
  136. * <p>
  137. * 查询地址:PUT http://{baseUrl}/{indexName}
  138. */
  139. public boolean createIndex(String indexName) {
  140. String url = this.getBaseUrl(indexName).toString();
  141. /* 返回结果 (仅供参考)
  142. "createIndex": {
  143. "shards_acknowledged": true,
  144. "acknowledged": true,
  145. "index": "hello_world"
  146. }
  147. */
  148. try {
  149. return RestUtil.put(url).getBoolean("acknowledged");
  150. } catch (org.springframework.web.client.HttpClientErrorException ex) {
  151. if (HttpStatus.BAD_REQUEST == ex.getStatusCode()) {
  152. log.warn("索引创建失败:" + indexName + " 已存在,无需再创建");
  153. } else {
  154. ex.printStackTrace();
  155. }
  156. }
  157. return false;
  158. }
  159. /**
  160. * 删除索引
  161. * <p>
  162. * 查询地址:DELETE http://{baseUrl}/{indexName}
  163. */
  164. public boolean removeIndex(String indexName) {
  165. String url = this.getBaseUrl(indexName).toString();
  166. try {
  167. return RestUtil.delete(url).getBoolean("acknowledged");
  168. } catch (org.springframework.web.client.HttpClientErrorException ex) {
  169. if (HttpStatus.NOT_FOUND == ex.getStatusCode()) {
  170. log.warn("索引删除失败:" + indexName + " 不存在,无需删除");
  171. } else {
  172. ex.printStackTrace();
  173. }
  174. }
  175. return false;
  176. }
  177. /**
  178. * 获取索引字段映射(可获取字段类型)
  179. * <p>
  180. *
  181. * @param indexName 索引名称
  182. * @param typeName 分类名称
  183. * @return
  184. */
  185. public JSONObject getIndexMapping(String indexName, String typeName) {
  186. String url = this.getBaseUrl(indexName, typeName).append("/_mapping?").append(FORMAT_JSON).toString();
  187. // 针对 es 7.x 版本做兼容
  188. this.getElasticsearchVersion();
  189. if (oConvertUtils.isNotEmpty(this.version) && this.version.startsWith("7")) {
  190. url += "&include_type_name=true";
  191. }
  192. log.info("getIndexMapping-url:" + url);
  193. /*
  194. * 参考返回JSON结构:
  195. *
  196. *{
  197. * // 索引名称
  198. * "[indexName]": {
  199. * "mappings": {
  200. * // 分类名称
  201. * "[typeName]": {
  202. * "properties": {
  203. * // 字段名
  204. * "input_number": {
  205. * // 字段类型
  206. * "type": "long"
  207. * },
  208. * "input_string": {
  209. * "type": "text",
  210. * "fields": {
  211. * "keyword": {
  212. * "type": "keyword",
  213. * "ignore_above": 256
  214. * }
  215. * }
  216. * }
  217. * }
  218. * }
  219. * }
  220. * }
  221. * }
  222. */
  223. try {
  224. return RestUtil.get(url);
  225. } catch (org.springframework.web.client.HttpClientErrorException e) {
  226. String message = e.getMessage();
  227. if (message != null && message.contains("404 Not Found")) {
  228. return null;
  229. }
  230. throw e;
  231. }
  232. }
  233. /**
  234. * 获取索引字段映射,返回Java实体类
  235. *
  236. * @param indexName
  237. * @param typeName
  238. * @return
  239. */
  240. public <T> Map<String, T> getIndexMappingFormat(String indexName, String typeName, Class<T> clazz) {
  241. JSONObject mapping = this.getIndexMapping(indexName, typeName);
  242. Map<String, T> map = new HashMap<>();
  243. if (mapping == null) {
  244. return map;
  245. }
  246. // 获取字段属性
  247. JSONObject properties = mapping.getJSONObject(indexName)
  248. .getJSONObject("mappings")
  249. .getJSONObject(typeName)
  250. .getJSONObject("properties");
  251. // 封装成 java类型
  252. for (String key : properties.keySet()) {
  253. T entity = properties.getJSONObject(key).toJavaObject(clazz);
  254. map.put(key, entity);
  255. }
  256. return map;
  257. }
  258. /**
  259. * 保存数据,详见:saveOrUpdate
  260. */
  261. public boolean save(String indexName, String typeName, String dataId, JSONObject data) {
  262. return this.saveOrUpdate(indexName, typeName, dataId, data);
  263. }
  264. /**
  265. * 更新数据,详见:saveOrUpdate
  266. */
  267. public boolean update(String indexName, String typeName, String dataId, JSONObject data) {
  268. return this.saveOrUpdate(indexName, typeName, dataId, data);
  269. }
  270. /**
  271. * 保存或修改索引数据
  272. * <p>
  273. * 查询地址:PUT http://{baseUrl}/{indexName}/{typeName}/{dataId}
  274. *
  275. * @param indexName 索引名称
  276. * @param typeName type,一个任意字符串,用于分类
  277. * @param dataId 数据id
  278. * @param data 要存储的数据
  279. * @return
  280. */
  281. public boolean saveOrUpdate(String indexName, String typeName, String dataId, JSONObject data) {
  282. String url = this.getBaseUrl(indexName, typeName).append("/").append(dataId).append("?refresh=wait_for").toString();
  283. /* 返回结果(仅供参考)
  284. "createIndexA2": {
  285. "result": "created",
  286. "_shards": {
  287. "total": 2,
  288. "successful": 1,
  289. "failed": 0
  290. },
  291. "_seq_no": 0,
  292. "_index": "test_index_1",
  293. "_type": "test_type_1",
  294. "_id": "a2",
  295. "_version": 1,
  296. "_primary_term": 1
  297. }
  298. */
  299. try {
  300. // 去掉 data 中为空的值
  301. Set<String> keys = data.keySet();
  302. List<String> emptyKeys = new ArrayList<>(keys.size());
  303. for (String key : keys) {
  304. String value = data.getString(key);
  305. //1、剔除空值
  306. if (oConvertUtils.isEmpty(value) || "[]".equals(value)) {
  307. emptyKeys.add(key);
  308. }
  309. //2、剔除上传控件值(会导致ES同步失败,报异常failed to parse field [ge_pic] of type [text] )
  310. if (oConvertUtils.isNotEmpty(value) && value.indexOf("[{")!=-1) {
  311. emptyKeys.add(key);
  312. log.info("-------剔除上传控件字段------------key: "+ key);
  313. }
  314. }
  315. for (String key : emptyKeys) {
  316. data.remove(key);
  317. }
  318. } catch (Exception e) {
  319. e.printStackTrace();
  320. }
  321. try {
  322. String result = RestUtil.put(url, data).getString("result");
  323. return "created".equals(result) || "updated".equals(result);
  324. } catch (Exception e) {
  325. log.error(e.getMessage() + "\n-- url: " + url + "\n-- data: " + data.toJSONString());
  326. //TODO 打印接口返回异常json
  327. return false;
  328. }
  329. }
  330. /**
  331. * 批量保存数据
  332. *
  333. * @param indexName 索引名称
  334. * @param typeName type,一个任意字符串,用于分类
  335. * @param dataList 要存储的数据数组,每行数据必须包含id
  336. * @return
  337. */
  338. public boolean saveBatch(String indexName, String typeName, JSONArray dataList) {
  339. String url = this.getBaseUrl().append("/_bulk").append("?refresh=wait_for").toString();
  340. StringBuilder bodySB = new StringBuilder();
  341. for (int i = 0; i < dataList.size(); i++) {
  342. JSONObject data = dataList.getJSONObject(i);
  343. String id = data.getString("id");
  344. // 该行的操作
  345. // {"create": {"_id":"${id}", "_index": "${indexName}", "_type": "${typeName}"}}
  346. JSONObject action = new JSONObject();
  347. JSONObject actionInfo = new JSONObject();
  348. actionInfo.put("_id", id);
  349. actionInfo.put("_index", indexName);
  350. actionInfo.put("_type", typeName);
  351. action.put("create", actionInfo);
  352. bodySB.append(action.toJSONString()).append("\n");
  353. // 该行的数据
  354. data.remove("id");
  355. bodySB.append(data.toJSONString()).append("\n");
  356. }
  357. System.out.println("+-+-+-: bodySB.toString(): " + bodySB.toString());
  358. HttpHeaders headers = RestUtil.getHeaderApplicationJson();
  359. RestUtil.request(url, HttpMethod.PUT, headers, null, bodySB, JSONObject.class);
  360. return true;
  361. }
  362. /**
  363. * 删除索引数据
  364. * <p>
  365. * 请求地址:DELETE http://{baseUrl}/{indexName}/{typeName}/{dataId}
  366. */
  367. public boolean delete(String indexName, String typeName, String dataId) {
  368. String url = this.getBaseUrl(indexName, typeName).append("/").append(dataId).toString();
  369. /* 返回结果(仅供参考)
  370. {
  371. "_index": "es_demo",
  372. "_type": "docs",
  373. "_id": "001",
  374. "_version": 3,
  375. "result": "deleted",
  376. "_shards": {
  377. "total": 1,
  378. "successful": 1,
  379. "failed": 0
  380. },
  381. "_seq_no": 28,
  382. "_primary_term": 18
  383. }
  384. */
  385. try {
  386. return "deleted".equals(RestUtil.delete(url).getString("result"));
  387. } catch (org.springframework.web.client.HttpClientErrorException ex) {
  388. if (HttpStatus.NOT_FOUND == ex.getStatusCode()) {
  389. return false;
  390. } else {
  391. throw ex;
  392. }
  393. }
  394. }
  395. /* = = = 以下关于查询和查询条件的方法 = = =*/
  396. /**
  397. * 查询数据
  398. * <p>
  399. * 请求地址:POST http://{baseUrl}/{indexName}/{typeName}/_search
  400. */
  401. public JSONObject search(String indexName, String typeName, JSONObject queryObject) {
  402. String url = this.getBaseUrl(indexName, typeName).append("/_search").toString();
  403. log.info("url:" + url + " ,search: " + queryObject.toJSONString());
  404. JSONObject res = RestUtil.post(url, queryObject);
  405. log.info("url:" + url + " ,return res: \n" + res.toJSONString());
  406. return res;
  407. }
  408. /**
  409. * @param _source (源滤波器)指定返回的字段,传null返回所有字段
  410. * @param query
  411. * @param from 从第几条数据开始
  412. * @param size 返回条目数
  413. * @return { "query": query }
  414. */
  415. public JSONObject buildQuery(List<String> _source, JSONObject query, int from, int size) {
  416. JSONObject json = new JSONObject();
  417. if (_source != null) {
  418. json.put("_source", _source);
  419. }
  420. json.put("query", query);
  421. json.put("from", from);
  422. json.put("size", size);
  423. return json;
  424. }
  425. /**
  426. * @return { "bool" : { "must": must, "must_not": mustNot, "should": should } }
  427. */
  428. public JSONObject buildBoolQuery(JSONArray must, JSONArray mustNot, JSONArray should) {
  429. JSONObject bool = new JSONObject();
  430. if (must != null) {
  431. bool.put("must", must);
  432. }
  433. if (mustNot != null) {
  434. bool.put("must_not", mustNot);
  435. }
  436. if (should != null) {
  437. bool.put("should", should);
  438. }
  439. JSONObject json = new JSONObject();
  440. json.put("bool", bool);
  441. return json;
  442. }
  443. /**
  444. * @param field 要查询的字段
  445. * @param args 查询参数,参考: *哈哈* OR *哒* NOT *呵* OR *啊*
  446. * @return
  447. */
  448. public JSONObject buildQueryString(String field, String... args) {
  449. if (field == null) {
  450. return null;
  451. }
  452. StringBuilder sb = new StringBuilder(field).append(":(");
  453. if (args != null) {
  454. for (String arg : args) {
  455. sb.append(arg).append(" ");
  456. }
  457. }
  458. sb.append(")");
  459. return this.buildQueryString(sb.toString());
  460. }
  461. /**
  462. * @return { "query_string": { "query": query } }
  463. */
  464. public JSONObject buildQueryString(String query) {
  465. JSONObject queryString = new JSONObject();
  466. queryString.put("query", query);
  467. JSONObject json = new JSONObject();
  468. json.put("query_string", queryString);
  469. return json;
  470. }
  471. /**
  472. * @param field 查询字段
  473. * @param min 最小值
  474. * @param max 最大值
  475. * @param containMin 范围内是否包含最小值
  476. * @param containMax 范围内是否包含最大值
  477. * @return { "range" : { field : { 『 "gt『e』?containMin" : min 』?min!=null , 『 "lt『e』?containMax" : max 』}} }
  478. */
  479. public JSONObject buildRangeQuery(String field, Object min, Object max, boolean containMin, boolean containMax) {
  480. JSONObject inner = new JSONObject();
  481. if (min != null) {
  482. if (containMin) {
  483. inner.put("gte", min);
  484. } else {
  485. inner.put("gt", min);
  486. }
  487. }
  488. if (max != null) {
  489. if (containMax) {
  490. inner.put("lte", max);
  491. } else {
  492. inner.put("lt", max);
  493. }
  494. }
  495. JSONObject range = new JSONObject();
  496. range.put(field, inner);
  497. JSONObject json = new JSONObject();
  498. json.put("range", range);
  499. return json;
  500. }
  501. }