ElasticSearch第七篇:SpringBoot集成ES

新建項(xiàng)目 springboot-es-test
源代碼:http://gitlab.heatedloan.com/cici/springboot-es-test
本地項(xiàng)目:/Users/cici/workspace/cici/springboot-es-test

第一步:pom.xml

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.4.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>6.4.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch</groupId>
                    <artifactId>elasticsearch</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.39</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>

第二步:application.properties

# Elasticsearch
# 9200端口是用來(lái)讓HTTP REST API來(lái)訪問(wèn)ElasticSearch,而9300端口是傳輸層監(jiān)聽(tīng)的默認(rèn)端口
elasticsearch.ip=127.0.0.1
elasticsearch.port=9300
elasticsearch.pool=5
#注意cluster.name需要與config/elasticsearch.yml中的cluster.name一致
elasticsearch.cluster.name=elasticsearch_cici
server.port=8181

第三步:創(chuàng)建員工對(duì)象實(shí)體類 和 分頁(yè)類

員工對(duì)象實(shí)體類

/**
 * @Description:Book實(shí)體 加上了@Document注解之后七咧,默認(rèn)情況下這個(gè)實(shí)體中所有的屬性都會(huì)被建立索引跃惫、并且分詞
 */
@Data
@ToString
@NoArgsConstructor
public class Employee {
    private String id;
    private Long version;
    String firstName;
    String lastName;
    String age;
    String[] interests;
}

分頁(yè)類

@Data
@ToString
public class EsPage {

    /**
     * 當(dāng)前頁(yè)
     */
    private int currentPage;
    /**
     * 每頁(yè)顯示多少條
     */
    private int pageSize;

    /**
     * 總記錄數(shù)
     */
    private int recordCount;
    /**
     * 本頁(yè)的數(shù)據(jù)列表
     */
    private List<Map<String, Object>> recordList;

    /**
     * 總頁(yè)數(shù)
     */
    private int pageCount;
    /**
     * 頁(yè)碼列表的開(kāi)始索引(包含)
     */
    private int beginPageIndex;
    /**
     * 頁(yè)碼列表的結(jié)束索引(包含)
     */
    private int endPageIndex;

    /**
     * 只接受前4個(gè)必要的屬性叮叹,會(huì)自動(dòng)的計(jì)算出其他3個(gè)屬性的值
     *
     * @param currentPage
     * @param pageSize
     * @param recordCount
     * @param recordList
     */
    public EsPage(int currentPage, int pageSize, int recordCount, List<Map<String, Object>> recordList) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.recordCount = recordCount;
        this.recordList = recordList;

        // 計(jì)算總頁(yè)碼
        pageCount = (recordCount + pageSize - 1) / pageSize;

        // 計(jì)算 beginPageIndex 和 endPageIndex
        // >> 總頁(yè)數(shù)不多于10頁(yè)艾栋,則全部顯示
        if (pageCount <= 10) {
            beginPageIndex = 1;
            endPageIndex = pageCount;
        }
        // 總頁(yè)數(shù)多于10頁(yè),則顯示當(dāng)前頁(yè)附近的共10個(gè)頁(yè)碼
        else {
            // 當(dāng)前頁(yè)附近的共10個(gè)頁(yè)碼(前4個(gè) + 當(dāng)前頁(yè) + 后5個(gè))
            beginPageIndex = currentPage - 4;
            endPageIndex = currentPage + 5;
            // 當(dāng)前面的頁(yè)碼不足4個(gè)時(shí)蛉顽,則顯示前10個(gè)頁(yè)碼
            if (beginPageIndex < 1) {
                beginPageIndex = 1;
                endPageIndex = 10;
            }
            // 當(dāng)后面的頁(yè)碼不足5個(gè)時(shí)蝗砾,則顯示后10個(gè)頁(yè)碼
            if (endPageIndex > pageCount) {
                endPageIndex = pageCount;
                beginPageIndex = pageCount - 10 + 1;
            }
        }
    }
}

第四步:創(chuàng)建config類

@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  函數(shù)名字
     *
     * @return
     */
    @Bean(name = "transportClient")
    public TransportClient transportClient() {
        LOGGER.info("Elasticsearch初始化開(kāi)始。携冤。悼粮。。曾棕。");
        TransportClient transportClient = null;
        try {
            // 配置信息
            Settings esSetting = Settings.builder()
                    .put("cluster.name", clusterName) //集群名字
                    .put("client.transport.sniff", true)//增加嗅探機(jī)制扣猫,找到ES集群
                    .put("thread_pool.search.size", Integer.parseInt(poolSize))//增加線程池個(gè)數(shù),暫時(shí)設(shè)為5
                    .build();
            //配置信息Settings自定義
            transportClient = new PreBuiltTransportClient(esSetting);
            TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port));
            transportClient.addTransportAddresses(transportAddress);
        } catch (Exception e) {
            LOGGER.error("elasticsearch TransportClient create error!!", e);
        }
        return transportClient;
    }
}

第五步:創(chuàng)建ES操作工具類

Elasticsearch JAVA操作有三種客戶端:
1翘地、TransportClient
2申尤、JestClient
3、RestClient
還有種是2.3中有的NodeClient,在5.5.1中好像沒(méi)有了衙耕。
還有種是spring-data-elasticsearch,這里先以TransportClient為例子昧穿。

@Component
public class ElasticsearchUtil {

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

    @Autowired
    private TransportClient transportClient;

    private static TransportClient client;

    /**
     * @PostContruct是spring框架的注解 spring容器初始化的時(shí)候執(zhí)行該方法
     */
    @PostConstruct
    public void init() {
        client = this.transportClient;
    }

    /**
     * 創(chuàng)建索引
     *
     * @param index
     * @return
     */
    public static boolean createIndex(String index) {
        if (!isIndexExist(index)) {
            LOGGER.info("Index is not exits!");
        }
        CreateIndexResponse indexresponse = client.admin().indices().prepareCreate(index).execute().actionGet();
        LOGGER.info("執(zhí)行建立成功?" + indexresponse.isAcknowledged());
        return indexresponse.isAcknowledged();
    }

    /**
     * 刪除索引
     *
     * @param index
     * @return
     */
    public static boolean deleteIndex(String index) {
        if (!isIndexExist(index)) {
            LOGGER.info("Index is not exits!");
        }
        DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(index).execute().actionGet();
        if (dResponse.isAcknowledged()) {
            LOGGER.info("delete index " + index + "  successfully!");
        } else {
            LOGGER.info("Fail to delete index " + index);
        }
        return dResponse.isAcknowledged();
    }

    /**
     * 判斷索引是否存在
     *
     * @param index
     * @return
     */
    public static boolean isIndexExist(String index) {
        IndicesExistsResponse inExistsResponse = client.admin().indices().exists(new IndicesExistsRequest(index)).actionGet();
        if (inExistsResponse.isExists()) {
            LOGGER.info("Index [" + index + "] is exist!");
        } else {
            LOGGER.info("Index [" + index + "] is not exist!");
        }
        return inExistsResponse.isExists();
    }

    /**
     * @Description: 判斷inde下指定type是否存在
     */
    public boolean isTypeExist(String index, String type) {
        return isIndexExist(index)
                ? client.admin().indices().prepareTypesExists(index).setTypes(type).execute().actionGet().isExists()
                : false;
    }

    /**
     * 數(shù)據(jù)添加橙喘,正定ID
     *
     * @param jsonObject 要增加的數(shù)據(jù)
     * @param index      索引时鸵,類似數(shù)據(jù)庫(kù)
     * @param type       類型,類似表
     * @param id         數(shù)據(jù)ID
     * @return
     */
    public static String addData(JSONObject jsonObject, String index, String type, String id) {
        IndexResponse response = client.prepareIndex(index, type, id).setSource(jsonObject).get();
        LOGGER.info("addData response status:{},id:{}", response.status().getStatus(), response.getId());
        return response.getId();
    }

    /**
     * 數(shù)據(jù)添加
     *
     * @param jsonObject 要增加的數(shù)據(jù)
     * @param index      索引厅瞎,類似數(shù)據(jù)庫(kù)
     * @param type       類型饰潜,類似表
     * @return
     */
    public static String addData(JSONObject jsonObject, String index, String type) {
        return addData(jsonObject, index, type, UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
    }

    /**
     * 通過(guò)ID刪除數(shù)據(jù)
     *
     * @param index 索引初坠,類似數(shù)據(jù)庫(kù)
     * @param type  類型,類似表
     * @param id    數(shù)據(jù)ID
     */
    public static void deleteDataById(String index, String type, String id) {

        DeleteResponse response = client.prepareDelete(index, type, id).execute().actionGet();

        LOGGER.info("deleteDataById response status:{},id:{}", response.status().getStatus(), response.getId());
    }

    /**
     * 通過(guò)ID 更新數(shù)據(jù)
     *
     * @param jsonObject 要增加的數(shù)據(jù)
     * @param index      索引彭雾,類似數(shù)據(jù)庫(kù)
     * @param type       類型某筐,類似表
     * @param id         數(shù)據(jù)ID
     * @return
     */
    public static void updateDataById(JSONObject jsonObject, String index, String type, String id) {

        UpdateRequest updateRequest = new UpdateRequest();

        updateRequest.index(index).type(type).id(id).doc(jsonObject);

        client.update(updateRequest);

    }

    /**
     * 通過(guò)ID獲取數(shù)據(jù)
     *
     * @param index  索引,類似數(shù)據(jù)庫(kù)
     * @param type   類型冠跷,類似表
     * @param id     數(shù)據(jù)ID
     * @param fields 需要顯示的字段南誊,逗號(hào)分隔(缺省為全部字段)
     * @return
     */
    public static Map<String, Object> searchDataById(String index, String type, String id, String fields) {

        GetRequestBuilder getRequestBuilder = client.prepareGet(index, type, id);

        if (StringUtils.isNotEmpty(fields)) {
            getRequestBuilder.setFetchSource(fields.split(","), null);
        }

        GetResponse getResponse = getRequestBuilder.execute().actionGet();

        return getResponse.getSource();
    }


    /**
     * 使用分詞查詢,并分頁(yè)
     *
     * @param index          索引名稱
     * @param type           類型名稱,可傳入多個(gè)type逗號(hào)分隔
     * @param startPage      當(dāng)前頁(yè)
     * @param pageSize       每頁(yè)顯示條數(shù)
     * @param query          查詢條件
     * @param fields         需要顯示的字段,逗號(hào)分隔(缺省為全部字段)
     * @param sortField      排序字段
     * @param highlightField 高亮字段
     * @return
     */
    public static EsPage searchDataPage(String index, String type, int startPage, int pageSize, QueryBuilder query, String fields, String sortField, String highlightField) {
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
        if (StringUtils.isNotEmpty(type)) {
            searchRequestBuilder.setTypes(type.split(","));
        }
        searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH);

        // 需要顯示的字段蜜托,逗號(hào)分隔(缺省為全部字段)
        if (StringUtils.isNotEmpty(fields)) {
            searchRequestBuilder.setFetchSource(fields.split(","), null);
        }

        //排序字段
        if (StringUtils.isNotEmpty(sortField)) {
            searchRequestBuilder.addSort(sortField, SortOrder.DESC);
        }

        // 高亮(xxx=111,aaa=222)
        if (StringUtils.isNotEmpty(highlightField)) {
            HighlightBuilder highlightBuilder = new HighlightBuilder();

            //highlightBuilder.preTags("<span style='color:red' >");//設(shè)置前綴
            //highlightBuilder.postTags("</span>");//設(shè)置后綴

            // 設(shè)置高亮字段
            highlightBuilder.field(highlightField);
            searchRequestBuilder.highlighter(highlightBuilder);
        }

        //searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery());
        searchRequestBuilder.setQuery(query);

        // 分頁(yè)應(yīng)用
        searchRequestBuilder.setFrom(startPage).setSize(pageSize);

        // 設(shè)置是否按查詢匹配度排序
        searchRequestBuilder.setExplain(true);

        //打印的內(nèi)容 可以在 Elasticsearch head 和 Kibana  上執(zhí)行查詢
        LOGGER.info("\n{}", searchRequestBuilder);

        // 執(zhí)行搜索,返回搜索響應(yīng)信息
        SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

        long totalHits = searchResponse.getHits().totalHits;
        long length = searchResponse.getHits().getHits().length;

        LOGGER.debug("共查詢到[{}]條數(shù)據(jù),處理數(shù)據(jù)條數(shù)[{}]", totalHits, length);

        if (searchResponse.status().getStatus() == 200) {
            // 解析對(duì)象
            List<Map<String, Object>> sourceList = setSearchResponse(searchResponse, highlightField);

            return new EsPage(startPage, pageSize, (int) totalHits, sourceList);
        }

        return null;

    }


    /**
     * 使用分詞查詢
     *
     * @param index          索引名稱
     * @param type           類型名稱,可傳入多個(gè)type逗號(hào)分隔
     * @param query          查詢條件
     * @param size           文檔大小限制
     * @param fields         需要顯示的字段抄囚,逗號(hào)分隔(缺省為全部字段)
     * @param sortField      排序字段
     * @param highlightField 高亮字段
     * @return
     */
    public static List<Map<String, Object>> searchListData(
            String index, String type, QueryBuilder query, Integer size,
            String fields, String sortField, String highlightField) {

        SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
        if (StringUtils.isNotEmpty(type)) {
            searchRequestBuilder.setTypes(type.split(","));
        }

        if (StringUtils.isNotEmpty(highlightField)) {
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            // 設(shè)置高亮字段
            highlightBuilder.field(highlightField);
            searchRequestBuilder.highlighter(highlightBuilder);
        }

        searchRequestBuilder.setQuery(query);

        if (StringUtils.isNotEmpty(fields)) {
            searchRequestBuilder.setFetchSource(fields.split(","), null);
        }
        searchRequestBuilder.setFetchSource(true);

        if (StringUtils.isNotEmpty(sortField)) {
            searchRequestBuilder.addSort(sortField, SortOrder.DESC);
        }

        if (size != null && size > 0) {
            searchRequestBuilder.setSize(size);
        }

        //打印的內(nèi)容 可以在 Elasticsearch head 和 Kibana  上執(zhí)行查詢
        LOGGER.info("\n{}", searchRequestBuilder);

        SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

        long totalHits = searchResponse.getHits().totalHits;
        long length = searchResponse.getHits().getHits().length;

        LOGGER.info("共查詢到[{}]條數(shù)據(jù),處理數(shù)據(jù)條數(shù)[{}]", totalHits, length);

        if (searchResponse.status().getStatus() == 200) {
            // 解析對(duì)象
            return setSearchResponse(searchResponse, highlightField);
        }
        return null;

    }


    /**
     * 高亮結(jié)果集 特殊處理
     *
     * @param searchResponse
     * @param highlightField
     */
    private static List<Map<String, Object>> setSearchResponse(SearchResponse searchResponse, String highlightField) {
        List<Map<String, Object>> sourceList = new ArrayList<Map<String, Object>>();
        StringBuffer stringBuffer = new StringBuffer();

        for (SearchHit searchHit : searchResponse.getHits().getHits()) {
            searchHit.getSourceAsMap().put("id", searchHit.getId());

            if (StringUtils.isNotEmpty(highlightField)) {

                System.out.println("遍歷 高亮結(jié)果集,覆蓋 正常結(jié)果集" + searchHit.getSourceAsMap());
                Text[] text = searchHit.getHighlightFields().get(highlightField).getFragments();

                if (text != null) {
                    for (Text str : text) {
                        stringBuffer.append(str.string());
                    }
                    //遍歷 高亮結(jié)果集橄务,覆蓋 正常結(jié)果集
                    searchHit.getSourceAsMap().put(highlightField, stringBuffer.toString());
                }
            }
            sourceList.add(searchHit.getSourceAsMap());
        }
        return sourceList;
    }
}

第六步:創(chuàng)建controller

@RestController
@RequestMapping("/es")
public class EsController {

    /**
     * 測(cè)試索引
     */
    private String indexName = "megacorp";

    /**
     * 類型
     */
    private String esType = "employee";

    /**
     * 創(chuàng)建索引
     * http://127.0.0.1:8080/es/createIndex
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/createIndex")
    public String createIndex(HttpServletRequest request, HttpServletResponse response) {
        if (!ElasticsearchUtil.isIndexExist(indexName)) {
            ElasticsearchUtil.createIndex(indexName);
        } else {
            return "索引已經(jīng)存在";
        }
        return "索引創(chuàng)建成功";
    }

    /**
     * 插入記錄
     *
     * @return
     */
    @RequestMapping("/insertJson")
    public String insertJson() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", DateUtil.formatDate(new Date()));
        jsonObject.put("age", 25);
        jsonObject.put("first_name", "j-" + new Random(100).nextInt());
        jsonObject.put("last_name", "cccc");
        jsonObject.put("about", "i like xiaofeng baby");
        jsonObject.put("date", new Date());
        String id = ElasticsearchUtil.addData(jsonObject, indexName, esType, jsonObject.getString("id"));
        return id;
    }

    /**
     * 插入記錄
     *
     * @return
     */
    @RequestMapping("/insertModel")
    public String insertModel() {
        Employee employee = new Employee();
        employee.setId("66");
        employee.setFirstName("m-" + new Random(100).nextInt());
        employee.setAge("24");
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(employee);
        String id = ElasticsearchUtil.addData(jsonObject, indexName, esType, jsonObject.getString("id"));
        return id;
    }

    /**
     * 刪除記錄
     *
     * @return
     */
    @RequestMapping("/delete")
    public String delete(String id) {
        if (StringUtils.isNotBlank(id)) {
            ElasticsearchUtil.deleteDataById(indexName, esType, id);
            return "刪除id=" + id;
        } else {
            return "id為空";
        }
    }

    /**
     * 更新數(shù)據(jù)
     *
     * @return
     */
    @RequestMapping("/update")
    public String update(String id) {
        if (StringUtils.isNotBlank(id)) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", id);
            jsonObject.put("age", 31);
            jsonObject.put("name", "修改");
            jsonObject.put("date", new Date());
            ElasticsearchUtil.updateDataById(jsonObject, indexName, esType, id);
            return "id=" + id;
        } else {
            return "id為空";
        }
    }

    /**
     * 獲取數(shù)據(jù)
     * http://127.0.0.1:8080/es/getData?id=2018-04-25%2016:33:44
     *
     * @param id
     * @return
     */
    @RequestMapping("/getData")
    public String getData(String id) {
        if (StringUtils.isNotBlank(id)) {
            Map<String, Object> map = ElasticsearchUtil.searchDataById(indexName, esType, id, null);
            return JSONObject.toJSONString(map);
        } else {
            return "id為空";
        }
    }

    /**
     * 查詢數(shù)據(jù)
     * 模糊查詢
     *
     * @return
     */
    @RequestMapping("/queryMatchData")
    public String queryMatchData() {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolean matchPhrase = false;
        if (matchPhrase == Boolean.TRUE) {
            //不進(jìn)行分詞搜索
            boolQuery.must(QueryBuilders.matchPhraseQuery("first_name", "cici"));
        } else {
            boolQuery.must(QueryBuilders.matchQuery("last_name", "cici"));
        }
        List<Map<String, Object>> list = ElasticsearchUtil.
                searchListData(indexName, esType, boolQuery, 10, "first_name", null, "last_name");
        return JSONObject.toJSONString(list);
    }

    /**
     * 通配符查詢數(shù)據(jù)
     * 通配符查詢 ?用來(lái)匹配1個(gè)任意字符幔托,*用來(lái)匹配零個(gè)或者多個(gè)字符
     *
     * @return
     */
    @RequestMapping("/queryWildcardData")
    public String queryWildcardData() {
        QueryBuilder queryBuilder = QueryBuilders.wildcardQuery("first_name.keyword", "cici");
        List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, queryBuilder, 10, null, null, null);
        return JSONObject.toJSONString(list);
    }

    /**
     * 正則查詢
     *
     * @return
     */
    @RequestMapping("/queryRegexpData")
    public String queryRegexpData() {
        QueryBuilder queryBuilder = QueryBuilders.regexpQuery("first_name.keyword", "m--[0-9]{1,11}");
        List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, queryBuilder, 10, null, null, null);
        return JSONObject.toJSONString(list);
    }

    /**
     * 查詢數(shù)字范圍數(shù)據(jù)
     *
     * @return
     */
    @RequestMapping("/queryIntRangeData")
    public String queryIntRangeData() {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.must(QueryBuilders.rangeQuery("age").from(24)
                .to(25));
        List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, boolQuery, 10, null, null, null);
        return JSONObject.toJSONString(list);
    }

    /**
     * 查詢?nèi)掌诜秶鷶?shù)據(jù)
     *
     * @return
     */
    @RequestMapping("/queryDateRangeData")
    public String queryDateRangeData() {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.must(QueryBuilders.rangeQuery("age").from("20")
                .to("50"));
        List<Map<String, Object>> list = ElasticsearchUtil.searchListData(indexName, esType, boolQuery, 10, null, null, null);
        return JSONObject.toJSONString(list);
    }

    /**
     * 查詢分頁(yè)
     *
     * @param startPage 第幾條記錄開(kāi)始
     *                  從0開(kāi)始
     *                  第1頁(yè) :http://127.0.0.1:8080/es/queryPage?startPage=0&pageSize=2
     *                  第2頁(yè) :http://127.0.0.1:8080/es/queryPage?startPage=2&pageSize=2
     * @param pageSize  每頁(yè)大小
     * @return
     */
    @RequestMapping("/queryPage")
    public String queryPage(String startPage, String pageSize) {
        if (StringUtils.isNotBlank(startPage) && StringUtils.isNotBlank(pageSize)) {
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
            boolQuery.must(QueryBuilders.rangeQuery("age").from("20")
                    .to("100"));
            EsPage list = ElasticsearchUtil.searchDataPage(indexName, esType, Integer.parseInt(startPage), Integer.parseInt(pageSize), boolQuery, null, null, null);
            return JSONObject.toJSONString(list);
        } else {
            return "startPage或者pageSize缺失";
        }
    }
}

利用postman一個(gè)個(gè)請(qǐng)求。
截取結(jié)果如下:方法都能正常準(zhǔn)確訪問(wèn)

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蜂挪,一起剝皮案震驚了整個(gè)濱河市重挑,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌棠涮,老刑警劉巖谬哀,帶你破解...
    沈念sama閱讀 216,324評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異严肪,居然都是意外死亡史煎,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,356評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)驳糯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)篇梭,“玉大人,你說(shuō)我怎么就攤上這事酝枢√裢担” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,328評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵帘睦,是天一觀的道長(zhǎng)袍患。 經(jīng)常有香客問(wèn)我,道長(zhǎng)官脓,這世上最難降的妖魔是什么协怒? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,147評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮卑笨,結(jié)果婚禮上孕暇,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好妖滔,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,160評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布隧哮。 她就那樣靜靜地躺著,像睡著了一般座舍。 火紅的嫁衣襯著肌膚如雪沮翔。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,115評(píng)論 1 296
  • 那天曲秉,我揣著相機(jī)與錄音采蚀,去河邊找鬼。 笑死承二,一個(gè)胖子當(dāng)著我的面吹牛榆鼠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播亥鸠,決...
    沈念sama閱讀 40,025評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼妆够,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了负蚊?” 一聲冷哼從身側(cè)響起神妹,我...
    開(kāi)封第一講書(shū)人閱讀 38,867評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎家妆,沒(méi)想到半個(gè)月后鸵荠,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,307評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡揩徊,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,528評(píng)論 2 332
  • 正文 我和宋清朗相戀三年腰鬼,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了嵌赠。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片塑荒。...
    茶點(diǎn)故事閱讀 39,688評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖姜挺,靈堂內(nèi)的尸體忽然破棺而出齿税,到底是詐尸還是另有隱情,我是刑警寧澤炊豪,帶...
    沈念sama閱讀 35,409評(píng)論 5 343
  • 正文 年R本政府宣布凌箕,位于F島的核電站,受9級(jí)特大地震影響词渤,放射性物質(zhì)發(fā)生泄漏牵舱。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,001評(píng)論 3 325
  • 文/蒙蒙 一缺虐、第九天 我趴在偏房一處隱蔽的房頂上張望芜壁。 院中可真熱鬧,春花似錦、人聲如沸慧妄。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,657評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)塞淹。三九已至窟蓝,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間饱普,已是汗流浹背运挫。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,811評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留套耕,地道東北人滑臊。 一個(gè)月前我還...
    沈念sama閱讀 47,685評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像箍铲,于是被迫代替她去往敵國(guó)和親雇卷。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,573評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容