声明:本站文章均为作者个人原创,图片均为实际截图。如有需要请收藏网站,禁止转载,谢谢配合!!!

  • from+size

基本使用不做介绍。
深度分页问题。页数越深,处理文档越多,占用内存越多,耗时越长。尽量避免深度分页,es通过 index.max_result_window参数限定最多到10000条数据。

  • scroll

遍历文档集的api,以快照的方式来避免深度分页的问题;
scroll不能用来做实时搜索,因为数据不是实时的。(因为它取的是某个时刻数据的快照,最新的数据进不来)
尽量不要使用复杂的sort 条件,使用 _doc 最高效。

  • search after
    避免深度分页的性能问题,提供实时的下一页文档获取功能;
    缺点是不能使用from 参数,即不能指定页数;
    只能下一页,不能上一页。

实现排序

request.source().sort("price", SortOrder.ASC);

实现分页

request.source().from(1);
request.source().size(10);

完整代码

package cn.bdbk.hotel;

import cn.bdbk.hotel.pojo.HotelDoc;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;


public class HotelSearchTest {

    private RestHighLevelClient client;

    @Test
    void testPageAndSort() throws IOException {
        //准备request
        SearchRequest request = new SearchRequest("hotel");
        //准备dsl
        request.source().query(QueryBuilders.matchAllQuery());
        //排序
        request.source().sort("price", SortOrder.ASC);
        //分页
        request.source().from(1);
        request.source().size(10);
        //发送请求
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //解析响应
        handleResponse(response);
    }

    @BeforeEach
    void setUP(){
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://170.106.116.159:9200")
        ));
    }

    @AfterEach
    void tearTown() throws IOException {
        this.client.close();
    }

}