SpringBoot 2.x 集成ElasticSearch6.x

朱雀 2023-10-10 10:37 214阅读 0赞

任务要求:SpringBoot集成ElasticSearch6.x ,并且封装相关功能.

核心pom.xml 文件:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.1.1.RELEASE</version>
  5. </parent>
  6. <properties>
  7. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  8. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  9. <java.version>1.8</java.version>
  10. <mybatis-spring-boot-starter.version>1.3.2</mybatis-spring-boot-starter.version>
  11. <mysql-connector-java.version>8.0.11</mysql-connector-java.version>
  12. <com.alibaba.druid.version>1.1.9</com.alibaba.druid.version>
  13. <commons-lang.version>2.6</commons-lang.version>
  14. <commons-codec.version>1.10</commons-codec.version>
  15. <commons-lang3.version>3.8.1</commons-lang3.version>
  16. <commons-net.version>3.6</commons-net.version>
  17. <commons-io.version>2.6</commons-io.version>
  18. <commons-collections.version>3.2.1</commons-collections.version>
  19. <common-fileupload.version>1.3.1</common-fileupload.version>
  20. <fastjson.version>1.2.48</fastjson.version>
  21. <jasperreports.version>6.10.0</jasperreports.version>
  22. </properties>
  23. <dependencies>
  24. <!-- SpringWeb模块 -->
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. <!-- 移除springboot 自带日志框架log-back -->
  29. <exclusions>
  30. <exclusion>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-logging</artifactId>
  33. </exclusion>
  34. </exclusions>
  35. </dependency>
  36. <!--springboot 集成测试框架 -->
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-test</artifactId>
  40. <scope>test</scope>
  41. </dependency>
  42. <!--lombok插件 -->
  43. <dependency>
  44. <groupId>org.projectlombok</groupId>
  45. <artifactId>lombok</artifactId>
  46. <version>${lombok.version}</version>
  47. <scope>provided</scope>
  48. </dependency>
  49. <!-- mysql 连接 -->
  50. <dependency>
  51. <groupId>org.mybatis.spring.boot</groupId>
  52. <artifactId>mybatis-spring-boot-starter</artifactId>
  53. <version>${mybatis-spring-boot-starter.version}</version>
  54. </dependency>
  55. <dependency>
  56. <groupId>mysql</groupId>
  57. <artifactId>mysql-connector-java</artifactId>
  58. <version>${mysql-connector-java.version}</version>
  59. <scope>runtime</scope>
  60. </dependency>
  61. <dependency>
  62. <groupId>com.alibaba</groupId>
  63. <artifactId>druid-spring-boot-starter</artifactId>
  64. <version>${com.alibaba.druid.version}</version>
  65. </dependency>
  66. <!-- 分页控件 -->
  67. <dependency>
  68. <groupId>com.github.pagehelper</groupId>
  69. <artifactId>pagehelper</artifactId>
  70. <version>4.1.6</version>
  71. </dependency>
  72. <!--common-lang 常用工具包 -->
  73. <dependency>
  74. <groupId>commons-lang</groupId>
  75. <artifactId>commons-lang</artifactId>
  76. <version>${commons-lang.version}</version>
  77. </dependency>
  78. <!--commons-lang3 工具包 -->
  79. <dependency>
  80. <groupId>org.apache.commons</groupId>
  81. <artifactId>commons-lang3</artifactId>
  82. <version>${commons-lang3.version}</version>
  83. </dependency>
  84. <!--commons-codec 加密工具包 -->
  85. <dependency>
  86. <groupId>commons-codec</groupId>
  87. <artifactId>commons-codec</artifactId>
  88. <version>${commons-codec.version}</version>
  89. </dependency>
  90. <!--commons-net 网络工具包 -->
  91. <dependency>
  92. <groupId>commons-net</groupId>
  93. <artifactId>commons-net</artifactId>
  94. <version>${commons-net.version}</version>
  95. </dependency>
  96. <!--common-io 工具包 -->
  97. <dependency>
  98. <groupId>commons-io</groupId>
  99. <artifactId>commons-io</artifactId>
  100. <version>${commons-io.version}</version>
  101. </dependency>
  102. <!--common-collection 工具包 -->
  103. <dependency>
  104. <groupId>commons-collections</groupId>
  105. <artifactId>commons-collections</artifactId>
  106. <version>${commons-collections.version}</version>
  107. </dependency>
  108. <!--common-fileupload 工具包 -->
  109. <dependency>
  110. <groupId>commons-fileupload</groupId>
  111. <artifactId>commons-fileupload</artifactId>
  112. <version>${common-fileupload.version}</version>
  113. </dependency>
  114. <!-- Swagger2 -->
  115. <dependency>
  116. <groupId>io.springfox</groupId>
  117. <artifactId>springfox-swagger2</artifactId>
  118. <version>2.7.0</version>
  119. </dependency>
  120. <dependency>
  121. <groupId>io.springfox</groupId>
  122. <artifactId>springfox-swagger-ui</artifactId>
  123. <version>2.7.0</version>
  124. </dependency>
  125. <!-- fastjson -->
  126. <dependency>
  127. <groupId>com.alibaba</groupId>
  128. <artifactId>fastjson</artifactId>
  129. <version>${fastjson.version}</version>
  130. </dependency>
  131. <!-- 集成elasticsearch 框架 -->
  132. <dependency>
  133. <groupId>org.springframework.boot</groupId>
  134. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  135. </dependency>

配置类:

  1. package com.zzg.search.config;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  5. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import com.alibaba.druid.support.http.StatViewServlet;
  9. import com.alibaba.druid.support.http.WebStatFilter;
  10. /**
  11. * druid 监控配置
  12. * @author zzg
  13. *
  14. */
  15. @Configuration
  16. public class DruidConfig {
  17. @Bean
  18. public ServletRegistrationBean druidServletRegistrationBean() {
  19. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
  20. servletRegistrationBean.setServlet(new StatViewServlet());
  21. servletRegistrationBean.addUrlMappings("/druid/*");
  22. servletRegistrationBean.addInitParameter("allow", "");
  23. servletRegistrationBean.addInitParameter("deny", "");
  24. servletRegistrationBean.addInitParameter("loginUsername", "admin");
  25. servletRegistrationBean.addInitParameter("loginPassword", "admin");
  26. return servletRegistrationBean;
  27. }
  28. /**
  29. * 注册DruidFilter拦截
  30. *
  31. * @return
  32. */
  33. @Bean
  34. public FilterRegistrationBean duridFilterRegistrationBean() {
  35. FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
  36. filterRegistrationBean.setFilter(new WebStatFilter());
  37. Map<String, String> initParams = new HashMap<String, String>();
  38. //设置忽略请求
  39. initParams.put("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
  40. filterRegistrationBean.setInitParameters(initParams);
  41. filterRegistrationBean.addUrlPatterns("/*");
  42. return filterRegistrationBean;
  43. }
  44. }
  45. package com.zzg.search.config;
  46. import java.util.Properties;
  47. import org.springframework.context.annotation.Bean;
  48. import org.springframework.context.annotation.Configuration;
  49. import com.github.pagehelper.PageHelper;
  50. /**
  51. * mybatis 配置对象
  52. * @author zzg
  53. *
  54. */
  55. @Configuration
  56. public class MyBatisConfig {
  57. /**
  58. * 分页对象实列化
  59. * @return
  60. */
  61. @Bean
  62. public PageHelper pageHelper() {
  63. PageHelper pageHelper = new PageHelper();
  64. Properties p = new Properties();
  65. p.setProperty("offsetAsPageNum", "true");
  66. p.setProperty("rowBoundsWithCount", "true");
  67. p.setProperty("reasonable", "true");
  68. p.setProperty("dialect", "mysql");
  69. pageHelper.setProperties(p);
  70. return pageHelper;
  71. }
  72. }
  73. package com.zzg.search.config;
  74. import java.util.ArrayList;
  75. import java.util.List;
  76. import org.springframework.context.annotation.Bean;
  77. import org.springframework.context.annotation.Configuration;
  78. import io.swagger.annotations.ApiOperation;
  79. import springfox.documentation.builders.ApiInfoBuilder;
  80. import springfox.documentation.builders.ParameterBuilder;
  81. import springfox.documentation.builders.PathSelectors;
  82. import springfox.documentation.builders.RequestHandlerSelectors;
  83. import springfox.documentation.schema.ModelRef;
  84. import springfox.documentation.service.ApiInfo;
  85. import springfox.documentation.service.Contact;
  86. import springfox.documentation.service.Parameter;
  87. import springfox.documentation.spi.DocumentationType;
  88. import springfox.documentation.spring.web.plugins.Docket;
  89. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  90. @Configuration
  91. @EnableSwagger2
  92. public class SwaggerConfig {
  93. @Bean
  94. public Docket buildDocket() {
  95. ParameterBuilder tokenPar = new ParameterBuilder();
  96. List<Parameter> pars = new ArrayList<Parameter>();
  97. tokenPar.name("X-CSRF-TOKEN").description("令牌").modelRef(new ModelRef("string")).parameterType("header")
  98. .required(false).build();
  99. pars.add(tokenPar.build());
  100. return new Docket(DocumentationType.SWAGGER_2).select()
  101. .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any())
  102. .build().globalOperationParameters(pars).apiInfo(buildApiInf());
  103. }
  104. private ApiInfo buildApiInf() {
  105. return new ApiInfoBuilder().title("****").termsOfServiceUrl("http://www.baidu.cn/")
  106. .description("API接口")
  107. .contact(new Contact("baidu", "http://www.baidu.cn/", "zhouzhiwengang@163.com"))
  108. .version("2.0").build();
  109. }
  110. }

elasticsearch6.x 检索组件封装:

  1. package com.zzg.search.component;
  2. import java.net.InetAddress;
  3. import java.net.UnknownHostException;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Map;
  8. import org.apache.commons.collections.map.HashedMap;
  9. import org.apache.commons.lang.StringUtils;
  10. import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
  11. import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
  12. import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
  13. import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
  14. import org.elasticsearch.action.bulk.BulkRequestBuilder;
  15. import org.elasticsearch.action.bulk.BulkResponse;
  16. import org.elasticsearch.action.delete.DeleteResponse;
  17. import org.elasticsearch.action.index.IndexResponse;
  18. import org.elasticsearch.action.search.SearchRequestBuilder;
  19. import org.elasticsearch.action.search.SearchResponse;
  20. import org.elasticsearch.action.update.UpdateRequest;
  21. import org.elasticsearch.client.transport.TransportClient;
  22. import org.elasticsearch.common.settings.Settings;
  23. import org.elasticsearch.common.transport.TransportAddress;
  24. import org.elasticsearch.index.query.QueryBuilders;
  25. import org.elasticsearch.search.SearchHit;
  26. import org.elasticsearch.search.SearchHits;
  27. import org.elasticsearch.search.aggregations.AggregationBuilder;
  28. import org.elasticsearch.search.aggregations.AggregationBuilders;
  29. import org.elasticsearch.search.aggregations.bucket.terms.Terms;
  30. import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
  31. import org.elasticsearch.search.sort.SortOrder;
  32. import org.elasticsearch.transport.client.PreBuiltTransportClient;
  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;
  35. import com.zzg.search.component.builder.impl.ESQueryProduce;
  36. import com.zzg.search.component.exception.ESFailedException;
  37. import com.zzg.search.component.exception.ESIndexException;
  38. /**
  39. * ES 工具类
  40. * @author zzg
  41. *
  42. */
  43. public class ESService {
  44. //日志记录
  45. private Logger logger = LoggerFactory.getLogger(ESService.class);
  46. private final static int MAX = 10000;
  47. TransportClient client;
  48. public ESService(String clusterName, String ip, int port){
  49. try {
  50. Settings settings = Settings.builder()
  51. .put("cluster.name", clusterName).build();
  52. this.client = new PreBuiltTransportClient(settings)
  53. .addTransportAddress(new TransportAddress(InetAddress.getByName(ip), port));
  54. } catch (UnknownHostException e) {
  55. logger.error(e.getMessage());
  56. throw new ESFailedException("es init failed!", e);
  57. }
  58. }
  59. /**
  60. * 功能描述:新建索引
  61. * @param indexName 索引名
  62. */
  63. public void createIndex(String indexName) {
  64. client.admin().indices().create(new CreateIndexRequest(indexName))
  65. .actionGet();
  66. }
  67. /**
  68. * 功能描述:新建索引
  69. * @param index 索引名
  70. * @param type 类型
  71. */
  72. public void createIndex(String index, String type) {
  73. client.prepareIndex(index, type).setSource().get();
  74. }
  75. /**
  76. * 功能描述:删除索引
  77. * @param index 索引名
  78. */
  79. public void deleteIndex(String index) {
  80. if (indexExist(index)) {
  81. DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(index)
  82. .execute().actionGet();
  83. if (!dResponse.isAcknowledged()) {
  84. throw new ESIndexException("failed to delete index.");
  85. }
  86. } else {
  87. throw new ESIndexException("index name not exists");
  88. }
  89. }
  90. /**
  91. * 功能描述:验证索引是否存在
  92. * @param index 索引名
  93. */
  94. public boolean indexExist(String index) {
  95. IndicesExistsRequest inExistsRequest = new IndicesExistsRequest(index);
  96. IndicesExistsResponse inExistsResponse = client.admin().indices()
  97. .exists(inExistsRequest).actionGet();
  98. return inExistsResponse.isExists();
  99. }
  100. /**
  101. * 功能描述:插入数据
  102. * @param index 索引名
  103. * @param type 类型
  104. * @param json 数据
  105. */
  106. public void insertData(String index, String type, String json) {
  107. IndexResponse response = client.prepareIndex(index, type)
  108. .setSource(json)
  109. .get();
  110. }
  111. /**
  112. * 功能描述:插入数据
  113. * @param index 索引名
  114. * @param type 类型
  115. * @param _id 数据id
  116. * @param json 数据
  117. */
  118. public void insertData(String index, String type, String _id, String json) {
  119. IndexResponse response = client.prepareIndex(index, type).setId(_id)
  120. .setSource(json)
  121. .get();
  122. }
  123. /**
  124. * 功能描述:更新数据
  125. * @param index 索引名
  126. * @param type 类型
  127. * @param _id 数据id
  128. * @param json 数据
  129. */
  130. public void updateData(String index, String type, String _id, String json) throws Exception {
  131. try {
  132. UpdateRequest updateRequest = new UpdateRequest(index, type, _id)
  133. .doc(json);
  134. client.update(updateRequest).get();
  135. } catch (Exception e) {
  136. logger.error(e.getMessage());
  137. throw new ESIndexException("update data failed.", e);
  138. }
  139. }
  140. /**
  141. * 功能描述:删除数据
  142. * @param index 索引名
  143. * @param type 类型
  144. * @param _id 数据id
  145. */
  146. public void deleteData(String index, String type, String _id) {
  147. DeleteResponse response = client.prepareDelete(index, type, _id)
  148. .get();
  149. }
  150. /**
  151. * 功能描述:批量插入数据
  152. * @param index 索引名
  153. * @param type 类型
  154. * @param data (_id 主键, json 数据)
  155. */
  156. public void bulkInsertData(String index, String type, Map<String, String> data) {
  157. BulkRequestBuilder bulkRequest = client.prepareBulk();
  158. data.forEach((param1, param2) -> {
  159. bulkRequest.add(client.prepareIndex(index, type, param1)
  160. .setSource(param2)
  161. );
  162. });
  163. BulkResponse bulkResponse = bulkRequest.get();
  164. }
  165. /**
  166. * 功能描述:批量插入数据
  167. * @param index 索引名
  168. * @param type 类型
  169. * @param jsonList 批量数据
  170. */
  171. public void bulkInsertData(String index, String type, List<String> jsonList) {
  172. BulkRequestBuilder bulkRequest = client.prepareBulk();
  173. jsonList.forEach(item -> {
  174. bulkRequest.add(client.prepareIndex(index, type)
  175. .setSource(item)
  176. );
  177. });
  178. BulkResponse bulkResponse = bulkRequest.get();
  179. }
  180. /**
  181. * 功能描述:查询
  182. * @param index 索引名
  183. * @param type 类型
  184. * @param constructor 查询构造
  185. */
  186. public List<Map<String, Object>> search(String index, String type, ESQueryProduce constructor) {
  187. List<Map<String, Object>> result = new ArrayList<>();
  188. SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index).setTypes(type);
  189. //排序
  190. if (StringUtils.isNotEmpty(constructor.getAsc()))
  191. searchRequestBuilder.addSort(constructor.getAsc(), SortOrder.ASC);
  192. if (StringUtils.isNotEmpty(constructor.getDesc()))
  193. searchRequestBuilder.addSort(constructor.getDesc(), SortOrder.DESC);
  194. //设置查询体
  195. searchRequestBuilder.setQuery(constructor.getBoolQueryBuilder());
  196. //返回条目数
  197. int size = constructor.getSize();
  198. if (size < 0) {
  199. size = 0;
  200. }
  201. if (size > MAX) {
  202. size = MAX;
  203. }
  204. //返回条目数
  205. searchRequestBuilder.setSize(size);
  206. searchRequestBuilder.setFrom(constructor.getFrom() < 0 ? 0 : constructor.getFrom());
  207. SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
  208. SearchHits hits = searchResponse.getHits();
  209. SearchHit[] searchHists = hits.getHits();
  210. for (SearchHit sh : searchHists) {
  211. result.add(sh.getSourceAsMap());
  212. }
  213. return result;
  214. }
  215. /**
  216. * 功能描述:统计查询
  217. * @param index 索引名
  218. * @param type 类型
  219. * @param constructor 查询构造
  220. * @param groupBy 统计字段
  221. */
  222. public Map<Object, Object> statSearch(String index, String type, ESQueryProduce constructor, String groupBy) {
  223. Map<Object, Object> map = new HashedMap();
  224. SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index).setTypes(type);
  225. //排序
  226. if (StringUtils.isNotEmpty(constructor.getAsc()))
  227. searchRequestBuilder.addSort(constructor.getAsc(), SortOrder.ASC);
  228. if (StringUtils.isNotEmpty(constructor.getDesc()))
  229. searchRequestBuilder.addSort(constructor.getDesc(), SortOrder.DESC);
  230. //设置查询体
  231. if (null != constructor) {
  232. searchRequestBuilder.setQuery(constructor.getBoolQueryBuilder());
  233. } else {
  234. searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery());
  235. }
  236. int size = constructor.getSize();
  237. if (size < 0) {
  238. size = 0;
  239. }
  240. if (size > MAX) {
  241. size = MAX;
  242. }
  243. //返回条目数
  244. searchRequestBuilder.setSize(size);
  245. searchRequestBuilder.setFrom(constructor.getFrom() < 0 ? 0 : constructor.getFrom());
  246. SearchResponse sr = searchRequestBuilder.addAggregation(
  247. AggregationBuilders.terms("agg").field(groupBy)
  248. ).get();
  249. Terms stateAgg = sr.getAggregations().get("agg");
  250. Iterator<Terms.Bucket> iter = (Iterator<Bucket>) stateAgg.getBuckets().iterator();
  251. while (iter.hasNext()) {
  252. Terms.Bucket gradeBucket = iter.next();
  253. map.put(gradeBucket.getKey(), gradeBucket.getDocCount());
  254. }
  255. return map;
  256. }
  257. /**
  258. * 功能描述:统计查询
  259. * @param index 索引名
  260. * @param type 类型
  261. * @param constructor 查询构造
  262. * @param agg 自定义计算
  263. */
  264. public Map<Object, Object> statSearch(String index, String type, ESQueryProduce constructor, AggregationBuilder agg) {
  265. if (agg == null) {
  266. return null;
  267. }
  268. Map<Object, Object> map = new HashedMap();
  269. SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index).setTypes(type);
  270. //排序
  271. if (StringUtils.isNotEmpty(constructor.getAsc()))
  272. searchRequestBuilder.addSort(constructor.getAsc(), SortOrder.ASC);
  273. if (StringUtils.isNotEmpty(constructor.getDesc()))
  274. searchRequestBuilder.addSort(constructor.getDesc(), SortOrder.DESC);
  275. //设置查询体
  276. if (null != constructor) {
  277. searchRequestBuilder.setQuery(constructor.getBoolQueryBuilder());
  278. } else {
  279. searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery());
  280. }
  281. int size = constructor.getSize();
  282. if (size < 0) {
  283. size = 0;
  284. }
  285. if (size > MAX) {
  286. size = MAX;
  287. }
  288. //返回条目数
  289. searchRequestBuilder.setSize(size);
  290. searchRequestBuilder.setFrom(constructor.getFrom() < 0 ? 0 : constructor.getFrom());
  291. SearchResponse sr = searchRequestBuilder.addAggregation(
  292. agg
  293. ).get();
  294. Terms stateAgg = sr.getAggregations().get("agg");
  295. Iterator<Terms.Bucket> iter = (Iterator<Bucket>) stateAgg.getBuckets().iterator();
  296. while (iter.hasNext()) {
  297. Terms.Bucket gradeBucket = iter.next();
  298. map.put(gradeBucket.getKey(), gradeBucket.getDocCount());
  299. }
  300. return map;
  301. }
  302. /**
  303. * 功能描述:关闭链接
  304. */
  305. public void close() {
  306. client.close();
  307. }
  308. }
  309. package com.zzg.search.component.builder;
  310. import org.elasticsearch.index.query.BoolQueryBuilder;
  311. /**
  312. * es 查询构建工具类
  313. *
  314. * @author zzg
  315. *
  316. */
  317. public interface IESQueryBuilders {
  318. /**
  319. * term 查询
  320. * @param name
  321. * @param valve
  322. * @return
  323. * @throws Exception
  324. */
  325. IESQueryBuilders termQuery(String name, String... values) throws Exception;
  326. /**
  327. * wildcard 查询
  328. * @param name
  329. * @param values
  330. * @return
  331. * @throws Exception
  332. */
  333. IESQueryBuilders wildcardQuery(String name, String... values) throws Exception;
  334. /**
  335. * rang 查询
  336. * @param name
  337. * @param from
  338. * @param to
  339. * @return
  340. * @throws Exception
  341. */
  342. IESQueryBuilders rangeQuery(String name, String from, String to) throws Exception;
  343. /**
  344. * bool 查询
  345. * @return
  346. */
  347. BoolQueryBuilder getBoolQueryBuilder();
  348. }
  349. package com.zzg.search.component.builder;
  350. import org.elasticsearch.index.query.BoolQueryBuilder;
  351. /**
  352. * es 查询 生成类
  353. * @author zzg
  354. *
  355. */
  356. public interface IESQueryProduce {
  357. /**
  358. * bool 查询
  359. * @return
  360. */
  361. BoolQueryBuilder getBoolQueryBuilder();
  362. /**
  363. * es 生成核心方法
  364. * @param esQueryBuilders
  365. * @return
  366. */
  367. IESQueryProduce getProduct(IESQueryBuilders esQueryBuilders);
  368. }
  369. package com.zzg.search.component.builder.factory;
  370. import com.zzg.search.component.builder.IESQueryBuilders;
  371. import com.zzg.search.component.builder.model.ESQueryMode;
  372. public class ESQueryBuilderFactory {
  373. public static IESQueryBuilders creatESQueryBuilder(ESQueryMode mode)
  374. throws ClassNotFoundException, IllegalAccessException, InstantiationException {
  375. Class<?> clazz = Class.forName(mode.mode());
  376. return (IESQueryBuilders) clazz.newInstance();
  377. }
  378. }
  379. package com.zzg.search.component.builder.impl;
  380. import org.elasticsearch.index.query.BoolQueryBuilder;
  381. import org.elasticsearch.index.query.QueryBuilders;
  382. import com.zzg.search.component.builder.IESQueryBuilders;
  383. public class ESFilter implements IESQueryBuilders {
  384. private BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
  385. @Override
  386. public IESQueryBuilders termQuery(String name, String... values) throws Exception {
  387. // TODO Auto-generated method stub
  388. boolQueryBuilder.filter(QueryBuilders.termsQuery(name, values));
  389. return this;
  390. }
  391. @Override
  392. public IESQueryBuilders wildcardQuery(String name, String... values) throws Exception {
  393. // TODO Auto-generated method stub
  394. for (String value : values){
  395. boolQueryBuilder.filter(QueryBuilders.wildcardQuery(name, "*" + value + "*"));
  396. }
  397. return this;
  398. }
  399. @Override
  400. public IESQueryBuilders rangeQuery(String name, String from, String to) throws Exception {
  401. // TODO Auto-generated method stub
  402. return null;
  403. }
  404. @Override
  405. public BoolQueryBuilder getBoolQueryBuilder() {
  406. // TODO Auto-generated method stub
  407. return boolQueryBuilder;
  408. }
  409. }
  410. package com.zzg.search.component.builder.impl;
  411. import org.elasticsearch.index.query.BoolQueryBuilder;
  412. import org.elasticsearch.index.query.QueryBuilders;
  413. import com.zzg.search.component.builder.IESQueryBuilders;
  414. public class ESMust implements IESQueryBuilders {
  415. private BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
  416. @Override
  417. public IESQueryBuilders termQuery(String name, String... values) throws Exception {
  418. // TODO Auto-generated method stub
  419. boolQueryBuilder.must(QueryBuilders.termsQuery(name, values));
  420. return this;
  421. }
  422. @Override
  423. public IESQueryBuilders wildcardQuery(String name, String... values) throws Exception {
  424. // TODO Auto-generated method stub
  425. for (String value : values){
  426. boolQueryBuilder.must(QueryBuilders.wildcardQuery(name, "*" + value + "*"));
  427. }
  428. return this;
  429. }
  430. @Override
  431. public IESQueryBuilders rangeQuery(String name, String from, String to) throws Exception {
  432. // TODO Auto-generated method stub
  433. boolQueryBuilder.must(QueryBuilders.rangeQuery(name).from(from, true).to(to, true));
  434. return this;
  435. }
  436. @Override
  437. public BoolQueryBuilder getBoolQueryBuilder() {
  438. // TODO Auto-generated method stub
  439. return boolQueryBuilder;
  440. }
  441. }
  442. package com.zzg.search.component.builder.impl;
  443. import org.elasticsearch.index.query.BoolQueryBuilder;
  444. import org.elasticsearch.index.query.QueryBuilders;
  445. import com.zzg.search.component.builder.IESQueryBuilders;
  446. public class ESMustNot implements IESQueryBuilders {
  447. private BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
  448. @Override
  449. public IESQueryBuilders termQuery(String name, String... values) throws Exception {
  450. // TODO Auto-generated method stub
  451. boolQueryBuilder.mustNot(QueryBuilders.termQuery(name, values));
  452. return this;
  453. }
  454. @Override
  455. public IESQueryBuilders wildcardQuery(String name, String... values) throws Exception {
  456. // TODO Auto-generated method stub
  457. for (String value : values){
  458. boolQueryBuilder.mustNot(QueryBuilders.wildcardQuery(name, "*" + value + "*"));
  459. }
  460. return this;
  461. }
  462. @Override
  463. public IESQueryBuilders rangeQuery(String name, String from, String to) throws Exception {
  464. // TODO Auto-generated method stub
  465. return null;
  466. }
  467. @Override
  468. public BoolQueryBuilder getBoolQueryBuilder() {
  469. // TODO Auto-generated method stub
  470. return boolQueryBuilder;
  471. }
  472. }
  473. package com.zzg.search.component.builder.impl;
  474. import org.elasticsearch.index.query.BoolQueryBuilder;
  475. import com.zzg.search.component.builder.IESQueryBuilders;
  476. import com.zzg.search.component.builder.IESQueryProduce;
  477. public class ESQueryProduce implements IESQueryProduce{
  478. private BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
  479. private int size = Integer.MAX_VALUE;
  480. private String asc;
  481. private String desc;
  482. private int from = 0;
  483. @Override
  484. public BoolQueryBuilder getBoolQueryBuilder() {
  485. // TODO Auto-generated method stub
  486. return boolQueryBuilder;
  487. }
  488. @Override
  489. public IESQueryProduce getProduct(IESQueryBuilders esQueryBuilders) {
  490. // TODO Auto-generated method stub
  491. boolQueryBuilder.must(esQueryBuilders.getBoolQueryBuilder());
  492. return this;
  493. }
  494. public String getAsc() {
  495. return asc;
  496. }
  497. public void setAsc(String asc) {
  498. this.asc = asc;
  499. }
  500. public String getDesc() {
  501. return desc;
  502. }
  503. public void setDesc(String desc) {
  504. this.desc = desc;
  505. }
  506. public int getSize() {
  507. return size;
  508. }
  509. public void setSize(int size) {
  510. this.size = size;
  511. }
  512. public int getFrom() {
  513. return from;
  514. }
  515. public void setFrom(int from) {
  516. this.from = from;
  517. }
  518. }
  519. package com.zzg.search.component.builder.impl;
  520. import org.elasticsearch.index.query.BoolQueryBuilder;
  521. import org.elasticsearch.index.query.QueryBuilders;
  522. import com.zzg.search.component.builder.IESQueryBuilders;
  523. public class ESShould implements IESQueryBuilders {
  524. private BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
  525. @Override
  526. public IESQueryBuilders termQuery(String name, String... values) throws Exception {
  527. // TODO Auto-generated method stub
  528. boolQueryBuilder.should(QueryBuilders.termsQuery(name, values));
  529. return this;
  530. }
  531. @Override
  532. public IESQueryBuilders wildcardQuery(String name, String... values) throws Exception {
  533. // TODO Auto-generated method stub
  534. for (String value : values){
  535. boolQueryBuilder.should(QueryBuilders.wildcardQuery(name, "*" + value + "*"));
  536. }
  537. return this;
  538. }
  539. @Override
  540. public IESQueryBuilders rangeQuery(String name, String from, String to) throws Exception {
  541. // TODO Auto-generated method stub
  542. return null;
  543. }
  544. @Override
  545. public BoolQueryBuilder getBoolQueryBuilder() {
  546. // TODO Auto-generated method stub
  547. return boolQueryBuilder;
  548. }
  549. }
  550. package com.zzg.search.component.builder.model;
  551. import java.io.Serializable;
  552. public enum ESQueryMode implements Serializable {
  553. MUST("com.zzg.search.component.builder.impl.ESMust"), MUST_NOT("com.zzg.search.component.builder.impl.ESMustNot"), SHOULD(
  554. "com.zzg.search.component.builder.impl.ESShould"), FILTER("com.zzg.search.component.builder.impl.ESFilter");
  555. private String mode;
  556. ESQueryMode(String mode) {
  557. this.mode = mode;
  558. }
  559. public String mode() {
  560. return mode;
  561. }
  562. }
  563. package com.zzg.search.component.exception;
  564. import org.apache.commons.lang3.StringUtils;
  565. @SuppressWarnings("serial")
  566. public class ESFailedException extends RuntimeException {
  567. /** The code. */
  568. private String code;
  569. /** The params. */
  570. private String[] params;
  571. /**
  572. * Gets the code.
  573. *
  574. * @return the code
  575. */
  576. public String getCode() {
  577. return code;
  578. }
  579. /**
  580. * Sets the code.
  581. *
  582. * @param code
  583. * the new code
  584. */
  585. public void setCode(String code) {
  586. this.code = code;
  587. }
  588. /**
  589. * Gets the params.
  590. *
  591. * @return the params
  592. */
  593. public String[] getParams() {
  594. return params;
  595. }
  596. /**
  597. * Sets the params.
  598. *
  599. * @param params
  600. * the new params
  601. */
  602. public void setParams(String[] params) {
  603. this.params = params;
  604. }
  605. /**
  606. * Instantiates a new base runtime exception.
  607. */
  608. public ESFailedException() {
  609. super();
  610. }
  611. /**
  612. * Instantiates a new base runtime exception.
  613. *
  614. * @param message
  615. * the message
  616. * @param e
  617. * the e
  618. */
  619. public ESFailedException(String message, Exception e) {
  620. super(message, e);
  621. }
  622. /**
  623. * Instantiates a new base runtime exception.
  624. *
  625. * @param message
  626. * the message
  627. */
  628. public ESFailedException(String message) {
  629. super(message);
  630. }
  631. /**
  632. * Instantiates a new base runtime exception.
  633. *
  634. * @param e
  635. * the e
  636. */
  637. public ESFailedException(Exception e) {
  638. super(e);
  639. }
  640. /**
  641. * Instantiates a new base runtime exception.
  642. *
  643. * @param code
  644. * the code
  645. * @param params
  646. * the params
  647. */
  648. public ESFailedException(String code, String[] params) {
  649. super(code);
  650. this.setCode(code);
  651. this.setParams(params);
  652. }
  653. /**
  654. * Instantiates a new base runtime exception.
  655. *
  656. * @param code
  657. * the code
  658. * @param params
  659. * the params
  660. * @param e
  661. * the e
  662. */
  663. public ESFailedException(String code, String[] params, Exception e) {
  664. super(e);
  665. this.setCode(code);
  666. this.setParams(params);
  667. }
  668. /*
  669. * (non-Javadoc)
  670. *
  671. * @see java.lang.Throwable#getMessage()
  672. */
  673. @Override
  674. public String getMessage() {
  675. if (code == null || code.length() == 0) {
  676. return super.getMessage();
  677. }
  678. String paramsStr = "NA";
  679. if (params != null) {
  680. paramsStr = StringUtils.join(params, ",");
  681. }
  682. String codeMessage = "code:" + code + ";parameters:" + paramsStr;
  683. return codeMessage;
  684. }
  685. /*
  686. * (non-Javadoc)
  687. *
  688. * @see java.lang.Throwable#toString()
  689. */
  690. @Override
  691. public String toString() {
  692. String s = getClass().getName();
  693. String message = this.getMessage();
  694. return (message != null) ? (s + ": " + message) : s;
  695. }
  696. }
  697. package com.zzg.search.component.exception;
  698. import org.apache.commons.lang3.StringUtils;
  699. @SuppressWarnings("serial")
  700. public class ESIndexException extends RuntimeException {
  701. /** The code. */
  702. private String code;
  703. /** The params. */
  704. private String[] params;
  705. /**
  706. * Gets the code.
  707. *
  708. * @return the code
  709. */
  710. public String getCode() {
  711. return code;
  712. }
  713. /**
  714. * Sets the code.
  715. *
  716. * @param code
  717. * the new code
  718. */
  719. public void setCode(String code) {
  720. this.code = code;
  721. }
  722. /**
  723. * Gets the params.
  724. *
  725. * @return the params
  726. */
  727. public String[] getParams() {
  728. return params;
  729. }
  730. /**
  731. * Sets the params.
  732. *
  733. * @param params
  734. * the new params
  735. */
  736. public void setParams(String[] params) {
  737. this.params = params;
  738. }
  739. /**
  740. * Instantiates a new base runtime exception.
  741. */
  742. public ESIndexException() {
  743. super();
  744. }
  745. /**
  746. * Instantiates a new base runtime exception.
  747. *
  748. * @param message
  749. * the message
  750. * @param e
  751. * the e
  752. */
  753. public ESIndexException(String message, Exception e) {
  754. super(message, e);
  755. }
  756. /**
  757. * Instantiates a new base runtime exception.
  758. *
  759. * @param message
  760. * the message
  761. */
  762. public ESIndexException(String message) {
  763. super(message);
  764. }
  765. /**
  766. * Instantiates a new base runtime exception.
  767. *
  768. * @param e
  769. * the e
  770. */
  771. public ESIndexException(Exception e) {
  772. super(e);
  773. }
  774. /**
  775. * Instantiates a new base runtime exception.
  776. *
  777. * @param code
  778. * the code
  779. * @param params
  780. * the params
  781. */
  782. public ESIndexException(String code, String[] params) {
  783. super(code);
  784. this.setCode(code);
  785. this.setParams(params);
  786. }
  787. /**
  788. * Instantiates a new base runtime exception.
  789. *
  790. * @param code
  791. * the code
  792. * @param params
  793. * the params
  794. * @param e
  795. * the e
  796. */
  797. public ESIndexException(String code, String[] params, Exception e) {
  798. super(e);
  799. this.setCode(code);
  800. this.setParams(params);
  801. }
  802. /*
  803. * (non-Javadoc)
  804. *
  805. * @see java.lang.Throwable#getMessage()
  806. */
  807. @Override
  808. public String getMessage() {
  809. if (code == null || code.length() == 0) {
  810. return super.getMessage();
  811. }
  812. String paramsStr = "NA";
  813. if (params != null) {
  814. paramsStr = StringUtils.join(params, ",");
  815. }
  816. String codeMessage = "code:" + code + ";parameters:" + paramsStr;
  817. return codeMessage;
  818. }
  819. /*
  820. * (non-Javadoc)
  821. *
  822. * @see java.lang.Throwable#toString()
  823. */
  824. @Override
  825. public String toString() {
  826. String s = getClass().getName();
  827. String message = this.getMessage();
  828. return (message != null) ? (s + ": " + message) : s;
  829. }
  830. }

业务逻辑模块domain、mapper、service 和serviceImpl 相关代码省略。

elasticsearch6.x controller 代码:

  1. package com.zzg.search.controller;
  2. import java.util.List;
  3. import java.util.Map;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. import com.zzg.jreport.response.JreportResponse;
  9. import com.zzg.search.component.ESService;
  10. import com.zzg.search.component.builder.IESQueryBuilders;
  11. import com.zzg.search.component.builder.factory.ESQueryBuilderFactory;
  12. import com.zzg.search.component.builder.impl.ESQueryProduce;
  13. import com.zzg.search.component.builder.model.ESQueryMode;
  14. import io.swagger.annotations.Api;
  15. import io.swagger.annotations.ApiOperation;
  16. @Controller
  17. @RequestMapping("/api/es")
  18. @Api(value = "搜索引擎Controlle", tags = "搜索引擎操作服务")
  19. public class ESController {
  20. @ApiOperation(httpMethod = "POST", value = "ES检索")
  21. @RequestMapping(value = "/search", method = { RequestMethod.POST }, produces = "application/json;charset=UTF-8")
  22. @ResponseBody
  23. public JreportResponse search() {
  24. ESService util = new ESService("my-application","192.168.1.74", 9300);
  25. // 验证索引文件是否存在
  26. boolean target = util.indexExist("username");
  27. if(target){
  28. System.out.println("指定索引是否存在:" + target);
  29. util.deleteIndex("username");
  30. System.out.println("指定索引删除成功");
  31. }
  32. return JreportResponse.ok(null);
  33. }
  34. @ApiOperation(httpMethod = "POST", value = "ES检索")
  35. @RequestMapping(value = "/find", method = { RequestMethod.POST }, produces = "application/json;charset=UTF-8")
  36. @ResponseBody
  37. public JreportResponse find() {
  38. ESService util = new ESService("my-application","192.168.1.74", 9300);
  39. ESQueryProduce produce = new ESQueryProduce();
  40. // 构建查询条件
  41. IESQueryBuilders must = null;
  42. try {
  43. must = ESQueryBuilderFactory.creatESQueryBuilder(ESQueryMode.MUST);
  44. must.termQuery("author", "somebody");
  45. must.rangeQuery("postdate", "2015-01-01", "2019-01-01");
  46. } catch (Exception e) {
  47. // TODO Auto-generated catch block
  48. e.printStackTrace();
  49. }
  50. produce.getProduct(must);
  51. List<Map<String,Object>> list = util.search("website", "blog", produce);
  52. return JreportResponse.ok(list);
  53. }
  54. }

application.properties 文件和log4j 文件

  1. # æå®æå¡ç«¯å£
  2. server.port=7090
  3. # æå®æå¡ å称
  4. # server.context-path=/jreport
  5. #mybatis xml æ件éç½®
  6. mybatis.mapper-locations=classpath*:mapper/search/*Mapper.xml
  7. mybatis.type-aliases-package=com.zzg.search.domain
  8. # MyBatis mysql8 éç½®
  9. spring.datasource.url=jdbc:mysql://192.168.*.**:3306/boot-security?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true&allowMultiQueries=true&nullCatalogMeansCurrent=true
  10. spring.datasource.username=root
  11. spring.datasource.password=******
  12. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  13. # Druid éç½®
  14. # åå§åæ¶å»ºç«ç©çè¿æ¥ç个æ°
  15. spring.datasource.druid.initial-size=5
  16. # æ大è¿æ¥æ± æ°é
  17. spring.datasource.druid.max-active=30
  18. # æå°è¿æ¥æ± æ°é
  19. spring.datasource.druid.min-idle=5
  20. # è·åè¿æ¥æ¶æ大ç­å¾æ¶é´ï¼åä½æ¯«ç§
  21. spring.datasource.druid.max-wait=60000
  22. # éç½®é´éå¤ä¹æè¿è¡ä¸æ¬¡æ£æµï¼æ£æµéè¦å³é­ç空é²è¿æ¥ï¼åä½æ¯æ¯«ç§
  23. spring.datasource.druid.time-between-eviction-runs-millis=60000
  24. # è¿æ¥ä¿æ空é²èä¸è¢«é©±éçæå°æ¶é´
  25. spring.datasource.druid.min-evictable-idle-time-millis=300000
  26. # ç¨æ¥æ£æµè¿æ¥æ¯å¦ææçsqlï¼è¦æ±æ¯ä¸ä¸ªæ¥è¯¢è¯­å¥
  27. spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
  28. # 建议é置为trueï¼ä¸å½±åæ§è½ï¼å¹¶ä¸ä¿è¯å®å¨æ§ãç³è¯·è¿æ¥çæ¶åæ£æµï¼å¦æ空é²æ¶é´å¤§äºtimeBetweenEvictionRunsMillisï¼æ§è¡validationQueryæ£æµè¿æ¥æ¯å¦ææã
  29. spring.datasource.druid.test-while-idle=true
  30. # ç³è¯·è¿æ¥æ¶æ§è¡validationQueryæ£æµè¿æ¥æ¯å¦ææï¼åäºè¿ä¸ªéç½®ä¼éä½æ§è½ã
  31. spring.datasource.druid.test-on-borrow=false
  32. # å½è¿è¿æ¥æ¶æ§è¡validationQueryæ£æµè¿æ¥æ¯å¦ææï¼åäºè¿ä¸ªéç½®ä¼éä½æ§è½ã
  33. spring.datasource.druid.test-on-return=false
  34. # æ¯å¦ç¼å­preparedStatementï¼ä¹å°±æ¯PSCacheãPSCache对æ¯æ游æ çæ°æ®åºæ§è½æå巨大ï¼æ¯å¦è¯´oracleãå¨mysqlä¸å»ºè®®å³é­ã
  35. spring.datasource.druid.pool-prepared-statements=true
  36. # è¦å¯ç¨PSCacheï¼å¿é¡»é置大äº0ï¼å½å¤§äº0æ¶ï¼poolPreparedStatementsèªå¨è§¦åä¿®æ¹ä¸ºtrueã
  37. spring.datasource.druid.max-pool-prepared-statement-per-connection-size=50
  38. # éç½®çæ§ç»è®¡æ¦æªçfiltersï¼å»æåçæ§çé¢sqlæ æ³ç»è®¡
  39. #spring.datasource.druid.filters=stat,wall
  40. # éè¿connectPropertieså±æ§æ¥æå¼mergeSqlåè½ï¼æ¢SQLè®°å½
  41. spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
  42. # å并å¤ä¸ªDruidDataSourceççæ§æ°æ®
  43. spring.datasource.druid.use-global-data-source-stat=true
  44. # éç½®sql 注å¥æ¹å¼
  45. spring.datasource.druid.filters=stat
  46. # æ¥å¿æ件éç½®
  47. logging.config=classpath:logback.xml
  48. # elasticsearch éç½®å±æ§
  49. spring.data.elasticsearch.clusterName=my-application
  50. spring.data.elasticsearch.clusterNodes=192.168.1.74:9300

log4j.properties 配置文件:未编写。

项目整体结构:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3pob3V6aGl3ZW5nYW5n_size_16_color_FFFFFF_t_70

发表评论

表情:
评论列表 (有 0 条评论,214人围观)

还没有评论,来说两句吧...

相关阅读