elasticsearch学习

1.是什么

  • 分布式的实时文件存储,每个字段都被索引并可被搜索
  • 分布式的实时分析搜索引擎
  • 可以扩展到上百台服务器,处理PB级结构化或非结构化数据

2.名词解释

  • index
    类似于MySQL的数据库

  • type
    类似于MySQL的表

  • document
    类似于MySQL的行

3.简单入门

  1. 依赖 ```xml cn.hutool hutool-all 4.0.12 org.elasticsearch elasticsearch 6.2.4 org.elasticsearch.client transport 6.2.4 org.elasticsearch elasticsearch com.alibaba fastjson 1.2.47

org.springframework.boot spring-boot-starter-logging

org.springframework.boot spring-boot-starter-web

  1. 2.
  2. 配置文件
  3. 1.
  4. application.properties
  5. ```pr
  6. # Elasticsearch
  7. # 9200端口是用来让HTTP REST API来访问ElasticSearch,而9300端口是传输层监听的默认端口
  8. elasticsearch.ip=127.0.0.1
  9. elasticsearch.port=9300
  10. # 默认值是核心线程数*4
  11. elasticsearch.pool=5
  12. elasticsearch.cluster.name=qiudx_es_study
  1. es配置文件ElasticsearchConfig ```java

/**

  • 用于定义配置类,可替换xml配置文件 *
  • @author qiudx
  • @version $Id ElasticsearchConfig.java, v 0.1 2018-06-13 13:26 qiudx Exp $$ */ @Configuration public class ElasticsearchConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchConfig.class);

    /**

    • elk集群地址 */ @Value(“${elasticsearch.ip}”) private String hostName;

      /**

    • 端口 */ @Value(“${elasticsearch.port}”) private String port;

      /**

    • 集群名称 */ @Value(“${elasticsearch.cluster.name}”) private String clusterName;

      /**

    • 连接池 */ @Value(“${elasticsearch.pool}”) private String poolSize;

      /**

    • Bean name default 函数名字 */ @Bean(name = “transportClient”) public TransportClient transportClient() { LOGGER.info(“Elasticsearch初始化开始。。。。。”); TransportClient transportClient = null; try {
      1. // 配置信息
      2. Settings esSetting = Settings.builder()
      3. //集群名字
      4. .put("cluster.name", clusterName)
      5. //增加嗅探机制,找到ES集群
      6. .put("client.transport.sniff", true)
      7. //增加线程池个数,暂时设为5
      8. .put("thread_pool.search.size", Integer.parseInt(poolSize))
      9. .build();
      10. //配置信息Settings自定义
      11. transportClient = new PreBuiltTransportClient(esSetting);
      12. TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port));
      13. transportClient.addTransportAddresses(transportAddress);
      } catch (Exception e) {
      1. LOGGER.error("elasticsearch TransportClient create error!!", e);
      } return transportClient; }

}

  1. 3.
  2. 工具类
  3. 1.
  4. es操作进行封装
  5. ```java
  6. /**
  7. * @author qiudx
  8. * @version $Id ElasticsearchUtil.java, v 0.1 2018-06-13 13:29 qiudx Exp $$
  9. */
  10. @Component
  11. public class ElasticsearchUtil {
  12. private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchUtil.class);
  13. private final TransportClient transportClient;
  14. private static TransportClient client;
  15. @Autowired
  16. public ElasticsearchUtil(TransportClient transportClient) {
  17. this.transportClient = transportClient;
  18. }
  19. /**
  20. * spring容器初始化的时候执行该方法
  21. */
  22. @PostConstruct
  23. public void init() {
  24. client = this.transportClient;
  25. }
  26. /**
  27. * 创建索引
  28. */
  29. public static boolean createIndex(String index) {
  30. if (!isIndexExist(index)) {
  31. LOGGER.info("Index is not exits!");
  32. }
  33. CreateIndexResponse indexresponse = client.admin().indices().prepareCreate(index).execute().actionGet();
  34. LOGGER.info("执行建立成功?" + indexresponse.isAcknowledged());
  35. return indexresponse.isAcknowledged();
  36. }
  37. /**
  38. * 删除索引
  39. */
  40. public static boolean deleteIndex(String index) {
  41. if (!isIndexExist(index)) {
  42. LOGGER.info("Index is not exits!");
  43. }
  44. DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(index).execute().actionGet();
  45. if (dResponse.isAcknowledged()) {
  46. LOGGER.info("delete index " + index + " successfully!");
  47. } else {
  48. LOGGER.info("Fail to delete index " + index);
  49. }
  50. return dResponse.isAcknowledged();
  51. }
  52. /**
  53. * 判断索引是否存在
  54. */
  55. public static boolean isIndexExist(String index) {
  56. IndicesExistsResponse inExistsResponse = client.admin().indices().exists(new IndicesExistsRequest(index)).actionGet();
  57. if (inExistsResponse.isExists()) {
  58. LOGGER.info("Index [" + index + "] is exist!");
  59. } else {
  60. LOGGER.info("Index [" + index + "] is not exist!");
  61. }
  62. return inExistsResponse.isExists();
  63. }
  64. /**
  65. * 数据添加,正定ID
  66. *
  67. * @param jsonObject 要增加的数据
  68. * @param index 索引,类似数据库
  69. * @param type 类型,类似表
  70. * @param id 数据ID
  71. */
  72. public static String addData(JSONObject jsonObject, String index, String type, String id) {
  73. IndexResponse response = client.prepareIndex(index, type, id).setSource(jsonObject).get();
  74. LOGGER.info("addData response status:{},id:{}", response.status().getStatus(), response.getId());
  75. return response.getId();
  76. }
  77. /**
  78. * 数据添加
  79. *
  80. * @param jsonObject 要增加的数据
  81. * @param index 索引,类似数据库
  82. * @param type 类型,类似表
  83. */
  84. public static String addData(JSONObject jsonObject, String index, String type) {
  85. return addData(jsonObject, index, type, UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
  86. }
  87. /**
  88. * 通过ID删除数据
  89. *
  90. * @param index 索引,类似数据库
  91. * @param type 类型,类似表
  92. * @param id 数据ID
  93. */
  94. public static void deleteDataById(String index, String type, String id) {
  95. DeleteResponse response = client.prepareDelete(index, type, id).execute().actionGet();
  96. LOGGER.info("deleteDataById response status:{},id:{}", response.status().getStatus(), response.getId());
  97. }
  98. /**
  99. * 通过ID 更新数据
  100. *
  101. * @param jsonObject 要增加的数据
  102. * @param index 索引,类似数据库
  103. * @param type 类型,类似表
  104. * @param id 数据ID
  105. */
  106. public static void updateDataById(JSONObject jsonObject, String index, String type, String id) {
  107. UpdateRequest updateRequest = new UpdateRequest();
  108. updateRequest.index(index).type(type).id(id).doc(jsonObject);
  109. client.update(updateRequest);
  110. }
  111. /**
  112. * 通过ID获取数据
  113. *
  114. * @param index 索引,类似数据库
  115. * @param type 类型,类似表
  116. * @param id 数据ID
  117. * @param fields 需要显示的字段,逗号分隔(缺省为全部字段)
  118. */
  119. public static Map<String, Object> searchDataById(String index, String type, String id, String fields) {
  120. GetRequestBuilder getRequestBuilder = client.prepareGet(index, type, id);
  121. if (StrUtil.isNotEmpty(fields)) {
  122. getRequestBuilder.setFetchSource(fields.split(","), null);
  123. }
  124. GetResponse getResponse = getRequestBuilder.execute().actionGet();
  125. return getResponse.getSource();
  126. }
  127. /**
  128. * 使用分词查询,并分页
  129. *
  130. * @param index 索引名称
  131. * @param type 类型名称,可传入多个type逗号分隔
  132. * @param startPage 当前页
  133. * @param pageSize 每页显示条数
  134. * @param query 查询条件
  135. * @param fields 需要显示的字段,逗号分隔(缺省为全部字段)
  136. * @param sortField 排序字段
  137. * @param highlightField 高亮字段
  138. */
  139. public static EsPage searchDataPage(String index, String type, int startPage, int pageSize, QueryBuilder query, String fields, String sortField, String highlightField) {
  140. SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
  141. if (StrUtil.isNotEmpty(type)) {
  142. searchRequestBuilder.setTypes(type.split(","));
  143. }
  144. searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH);
  145. // 需要显示的字段,逗号分隔(缺省为全部字段)
  146. if (StrUtil.isNotEmpty(fields)) {
  147. searchRequestBuilder.setFetchSource(fields.split(","), null);
  148. }
  149. //排序字段
  150. if (StrUtil.isNotEmpty(sortField)) {
  151. searchRequestBuilder.addSort(sortField, SortOrder.DESC);
  152. }
  153. // 高亮(xxx=111,aaa=222)
  154. if (StrUtil.isNotEmpty(highlightField)) {
  155. HighlightBuilder highlightBuilder = new HighlightBuilder();
  156. //highlightBuilder.preTags("<span style='color:red' >");//设置前缀
  157. //highlightBuilder.postTags("</span>");//设置后缀
  158. // 设置高亮字段
  159. highlightBuilder.field(highlightField);
  160. searchRequestBuilder.highlighter(highlightBuilder);
  161. }
  162. //searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery());
  163. searchRequestBuilder.setQuery(query);
  164. // 分页应用
  165. searchRequestBuilder.setFrom(startPage).setSize(pageSize);
  166. // 设置是否按查询匹配度排序
  167. searchRequestBuilder.setExplain(true);
  168. //打印的内容 可以在 Elasticsearch head 和 Kibana 上执行查询
  169. LOGGER.info("\n{}", searchRequestBuilder);
  170. // 执行搜索,返回搜索响应信息
  171. SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
  172. long totalHits = searchResponse.getHits().totalHits;
  173. long length = searchResponse.getHits().getHits().length;
  174. LOGGER.debug("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length);
  175. if (searchResponse.status().getStatus() == 200) {
  176. // 解析对象
  177. List<Map<String, Object>> sourceList = setSearchResponse(searchResponse, highlightField);
  178. return new EsPage(startPage, pageSize, (int) totalHits, sourceList);
  179. }
  180. return null;
  181. }
  182. /**
  183. * 使用分词查询
  184. *
  185. * @param index 索引名称
  186. * @param type 类型名称,可传入多个type逗号分隔
  187. * @param query 查询条件
  188. * @param size 文档大小限制
  189. * @param fields 需要显示的字段,逗号分隔(缺省为全部字段)
  190. * @param sortField 排序字段
  191. * @param highlightField 高亮字段
  192. */
  193. public static List<Map<String, Object>> searchListData(String index, String type, QueryBuilder query, Integer size, String fields, String sortField, String highlightField) {
  194. SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
  195. if (StrUtil.isNotEmpty(type)) {
  196. searchRequestBuilder.setTypes(type.split(","));
  197. }
  198. if (StrUtil.isNotEmpty(highlightField)) {
  199. HighlightBuilder highlightBuilder = new HighlightBuilder();
  200. // 设置高亮字段
  201. highlightBuilder.field(highlightField);
  202. searchRequestBuilder.highlighter(highlightBuilder);
  203. }
  204. searchRequestBuilder.setQuery(query);
  205. if (StrUtil.isNotEmpty(fields)) {
  206. searchRequestBuilder.setFetchSource(fields.split(","), null);
  207. }
  208. searchRequestBuilder.setFetchSource(true);
  209. if (StrUtil.isNotEmpty(sortField)) {
  210. searchRequestBuilder.addSort(sortField, SortOrder.DESC);
  211. }
  212. if (size != null && size > 0) {
  213. searchRequestBuilder.setSize(size);
  214. }
  215. //打印的内容 可以在 Elasticsearch head 和 Kibana 上执行查询
  216. LOGGER.info("\n{}", searchRequestBuilder);
  217. SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
  218. long totalHits = searchResponse.getHits().totalHits;
  219. long length = searchResponse.getHits().getHits().length;
  220. LOGGER.info("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length);
  221. if (searchResponse.status().getStatus() == 200) {
  222. // 解析对象
  223. return setSearchResponse(searchResponse, highlightField);
  224. }
  225. return null;
  226. }
  227. /**
  228. * 高亮结果集 特殊处理
  229. */
  230. private static List<Map<String, Object>> setSearchResponse(SearchResponse searchResponse, String highlightField) {
  231. List<Map<String, Object>> sourceList = new ArrayList<>();
  232. StringBuilder stringBuffer = new StringBuilder();
  233. for (SearchHit searchHit : searchResponse.getHits().getHits()) {
  234. searchHit.getSourceAsMap().put("id", searchHit.getId());
  235. if (StrUtil.isNotEmpty(highlightField)) {
  236. System.out.println("遍历 高亮结果集,覆盖 正常结果集" + searchHit.getSourceAsMap());
  237. Text[] text = searchHit.getHighlightFields().get(highlightField).getFragments();
  238. if (text != null) {
  239. for (Text str : text) {
  240. stringBuffer.append(str.string());
  241. }
  242. //遍历 高亮结果集,覆盖 正常结果集
  243. searchHit.getSourceAsMap().put(highlightField, stringBuffer.toString());
  244. }
  245. }
  246. sourceList.add(searchHit.getSourceAsMap());
  247. }
  248. return sourceList;
  249. }
  250. }
  1. 分页工具类 ```java /**

    • @author qiudx
    • @version $Id EsPage.java, v 0.1 2018-06-13 13:32 qiudx Exp $$ */ public class EsPage {

    /**

    • 当前页 / private int currentPage; /*
    • 每页显示多少条 */ private int pageSize;

    /**

    • 总记录数 / private int recordCount; /*
    • 本页的数据列表 */ private List> recordList;

    /**

    • 总页数 / private int pageCount; /*
    • 页码列表的开始索引(包含) / private int beginPageIndex; /*
    • 页码列表的结束索引(包含) */ private int endPageIndex;

    /**

    • 只接受前4个必要的属性,会自动的计算出其他3个属性的值 */ public EsPage(int currentPage, int pageSize, int recordCount, List> recordList) { this.currentPage = currentPage; this.pageSize = pageSize; this.recordCount = recordCount; this.recordList = recordList;

      // 计算总页码 pageCount = (recordCount + pageSize - 1) / pageSize;

      // 计算 beginPageIndex 和 endPageIndex // >> 总页数不多于10页,则全部显示 if (pageCount <= 10) {

      1. beginPageIndex = 1;
      2. endPageIndex = pageCount;

      } // >> 总页数多于10页,则显示当前页附近的共10个页码 else { // 当前页附近的共10个页码(前4个 + 当前页 + 后5个)

      1. beginPageIndex = currentPage - 4;
      2. endPageIndex = currentPage + 5;
      3. // 当前面的页码不足4个时,则显示前10个页码
      4. if (beginPageIndex < 1) {
      5. beginPageIndex = 1;
      6. endPageIndex = 10;
      7. }

      // 当后面的页码不足5个时,则显示后10个页码

      1. if (endPageIndex > pageCount) {
      2. endPageIndex = pageCount;
      3. beginPageIndex = pageCount - 10 + 1;
      4. }

      } }

    public int getCurrentPage() { return currentPage; }

    public void setCurrentPage(int currentPage) { this.currentPage = currentPage; }

    public int getPageSize() { return pageSize; }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    public int getRecordCount() { return recordCount; }

    public void setRecordCount(int recordCount) { this.recordCount = recordCount; }

    public List> getRecordList() { return recordList; }

    public void setRecordList(List> recordList) { this.recordList = recordList; }

    public int getPageCount() { return pageCount; }

    public void setPageCount(int pageCount) { this.pageCount = pageCount; }

    public int getBeginPageIndex() { return beginPageIndex; }

    public void setBeginPageIndex(int beginPageIndex) { this.beginPageIndex = beginPageIndex; }

    public int getEndPageIndex() { return endPageIndex; }

    public void setEndPageIndex(int endPageIndex) { this.endPageIndex = endPageIndex; }

}

  1. 4.
  2. 实体
  3. ```java
  4. /**
  5. * @author qiudx
  6. * @version $Id EsModel.java, v 0.1 2018-06-13 13:40 qiudx Exp $$
  7. */
  8. public class EsModel {
  9. private String id;
  10. private String name;
  11. private int age;
  12. private Date date;
  13. /**
  14. * Getter method for property <tt>id</tt>.
  15. *
  16. * @return property value of id
  17. */
  18. public String getId() {
  19. return id;
  20. }
  21. /**
  22. * Setter method for property <tt>id</tt>.
  23. *
  24. * @param id value to be assigned to property id
  25. */
  26. public void setId(String id) {
  27. this.id = id;
  28. }
  29. /**
  30. * Getter method for property <tt>name</tt>.
  31. *
  32. * @return property value of name
  33. */
  34. public String getName() {
  35. return name;
  36. }
  37. /**
  38. * Setter method for property <tt>name</tt>.
  39. *
  40. * @param name value to be assigned to property name
  41. */
  42. public void setName(String name) {
  43. this.name = name;
  44. }
  45. /**
  46. * Getter method for property <tt>age</tt>.
  47. *
  48. * @return property value of age
  49. */
  50. public int getAge() {
  51. return age;
  52. }
  53. /**
  54. * Setter method for property <tt>age</tt>.
  55. *
  56. * @param age value to be assigned to property age
  57. */
  58. public void setAge(int age) {
  59. this.age = age;
  60. }
  61. /**
  62. * Getter method for property <tt>date</tt>.
  63. *
  64. * @return property value of date
  65. */
  66. public Date getDate() {
  67. return date;
  68. }
  69. /**
  70. * Setter method for property <tt>date</tt>.
  71. *
  72. * @param date value to be assigned to property date
  73. */
  74. public void setDate(Date date) {
  75. this.date = date;
  76. }
  77. }
  1. 测试

    1. /**
    2. * @author qiudx
    3. * @version $Id EsController.java, v 0.1 2018-06-13 13:36 qiudx Exp $$
    4. */
    5. @RestController
    6. @EnableAutoConfiguration
    7. @RequestMapping("/es")
    8. public class EsController {
    9. /**
    10. * 测试索引
    11. */
    12. private String indexName = "test_index";
    13. /**
    14. * 类型
    15. */
    16. private String esType = "external";
    17. /**
    18. * http://127.0.0.1:8080/es/createIndex
    19. * 创建索引
    20. */
    21. @RequestMapping("/createIndex")
    22. public String createIndex() {
    23. if (!ElasticsearchUtil.isIndexExist(indexName)) {
    24. ElasticsearchUtil.createIndex(indexName);
    25. } else {
    26. return "索引已经存在";
    27. }
    28. return "索引创建成功";
    29. }
    30. /**
    31. * 插入记录
    32. */
    33. @RequestMapping("/insertJson")
    34. public String insertJson() {
    35. JSONObject jsonObject = new JSONObject();
    36. jsonObject.put("id", DateUtil.formatDate(new Date()));
    37. jsonObject.put("age", 25);
    38. jsonObject.put("name", "j-" + new Random(100).nextInt());
    39. jsonObject.put("date", new Date());
    40. return ElasticsearchUtil.addData(jsonObject, indexName, esType, jsonObject.getString("id"));
    41. }
    42. /**
    43. * 插入记录
    44. */
    45. @RequestMapping("/insertModel")
    46. public String insertModel() {
    47. EsModel esModel = new EsModel();
    48. esModel.setId(DateUtil.formatDate(new Date()));
    49. esModel.setName("m-" + new Random(100).nextInt());
    50. esModel.setAge(30);
    51. esModel.setDate(new Date());
    52. JSONObject jsonObject = (JSONObject) JSONObject.toJSON(esModel);
    53. return ElasticsearchUtil.addData(jsonObject, indexName, esType, jsonObject.getString("id"));
    54. }
    55. /**
    56. * 删除记录
    57. */
    58. @RequestMapping("/delete")
    59. public String delete(String id) {
    60. if (StrUtil.isNotBlank(id)) {
    61. ElasticsearchUtil.deleteDataById(indexName, esType, id);
    62. return "删除id=" + id;
    63. } else {
    64. return "id为空";
    65. }
    66. }
    67. /**
    68. * 更新数据
    69. */
    70. @RequestMapping("/update")
    71. public String update(String id) {
    72. if (StrUtil.isNotBlank(id)) {
    73. JSONObject jsonObject = new JSONObject();
    74. jsonObject.put("id", id);
    75. jsonObject.put("age", 31);
    76. jsonObject.put("name", "修改");
    77. jsonObject.put("date", new Date());
    78. ElasticsearchUtil.updateDataById(jsonObject, indexName, esType, id);
    79. return "id=" + id;
    80. } else {
    81. return "id为空";
    82. }
    83. }
    84. /**
    85. * 获取数据
    86. * http://127.0.0.1:8080/es/getData?id=2018-04-25%2016:33:44
    87. */
    88. @RequestMapping("/getData")
    89. public String getData(String id) {
    90. if (StrUtil.isNotBlank(id)) {
    91. Map<String, Object> map = ElasticsearchUtil.searchDataById(indexName, esType, id, null);
    92. return JSONObject.toJSONString(map);
    93. } else {
    94. return "id为空";
    95. }
    96. }
    97. /**
    98. * 查询数据
    99. * 模糊查询
    100. */
    101. @RequestMapping("/queryMatchData")
    102. @ResponseBody
    103. public String queryMatchData() {
    104. BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    105. boolQuery.must(QueryBuilders.matchQuery("name", "修"));
    106. List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, boolQuery, 10, null, null, null);
    107. return JSONObject.toJSONString(list);
    108. }
    109. /**
    110. * 通配符查询数据
    111. * 通配符查询 ?用来匹配1个任意字符,*用来匹配零个或者多个字符
    112. */
    113. @RequestMapping("/queryWildcardData")
    114. public String queryWildcardData() {
    115. QueryBuilder queryBuilder = QueryBuilders.wildcardQuery("name.keyword", "修*");
    116. List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, queryBuilder, 10, null, null, null);
    117. return JSONObject.toJSONString(list);
    118. }
    119. /**
    120. * 正则查询
    121. */
    122. @RequestMapping("/queryRegexpData")
    123. public String queryRegexpData() {
    124. QueryBuilder queryBuilder = QueryBuilders.regexpQuery("name.keyword", "j--[0-9]{1,11}");
    125. List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, queryBuilder, 10, null, null, null);
    126. return JSONObject.toJSONString(list);
    127. }
    128. /**
    129. * 查询数字范围数据
    130. */
    131. @RequestMapping("/queryIntRangeData")
    132. public String queryIntRangeData() {
    133. BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    134. boolQuery.must(QueryBuilders.rangeQuery("age").from(31)
    135. .to(32));
    136. List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, boolQuery, 10, null, null, null);
    137. return JSONObject.toJSONString(list);
    138. }
    139. /**
    140. * 查询日期范围数据
    141. */
    142. @RequestMapping("/queryDateRangeData")
    143. public String queryDateRangeData() {
    144. BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    145. boolQuery.must(QueryBuilders.rangeQuery("date").from("2018-04-25T08:33:44.840Z")
    146. .to("2018-04-25T10:03:08.081Z"));
    147. List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, boolQuery, 10, null, null, null);
    148. return JSONObject.toJSONString(list);
    149. }
    150. /**
    151. * 查询分页
    152. *
    153. * @param startPage 第几条记录开始
    154. * 从0开始
    155. * 第1页 :http://127.0.0.1:8080/es/queryPage?startPage=0&pageSize=2
    156. * 第2页 :http://127.0.0.1:8080/es/queryPage?startPage=2&pageSize=2
    157. * @param pageSize 每页大小
    158. */
    159. @RequestMapping("/queryPage")
    160. public String queryPage(String startPage, String pageSize) {
    161. if (StrUtil.isNotBlank(startPage) && StrUtil.isNotBlank(pageSize)) {
    162. BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    163. boolQuery.must(QueryBuilders.rangeQuery("date").from("2018-04-25T08:33:44.840Z")
    164. .to("2018-04-25T10:03:08.081Z"));
    165. EsPage list = ElasticsearchUtil.searchDataPage(indexName, esType, Integer.parseInt(startPage), Integer.parseInt(pageSize), boolQuery, null, null, null);
    166. return JSONObject.toJSONString(list);
    167. } else {
    168. return "startPage或者pageSize缺失";
    169. }
    170. }
    171. }