es基本用法(六)——整合SpringBoot进行增删改查

微信公众号:Feature社区

引入依赖

 

说明:transport-netty4-client依赖是报了netty被占用(貌似是这个异常)的时候需要引入的包,通常redis与es一起使用时会报错,此时需要引入该包,一般情况不需要引入此包

<!-- Spring Boot Elasticsearch 依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <version>${spring.boot.version}</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.plugin</groupId>
    <artifactId>transport-netty4-client</artifactId>
    <version>6.2.3</version>
</dependency>

es配置信息

spring:
    data:
  elasticsearch:
    cluster-name: [集群名称]
    cluster-nodes: [集群节点1,集群节点2]
    repositories:
      enabled: true

首先实体类添加注解,指定索引、类型、分片、备份信息

@Data
@Document(indexName = "argus", type = "event", shards = 5, replicas = 1)
public class Event implements Serializable {
    @Id
    private Integer id;

    private Integer type;

    private Integer level;

    private Integer projectName;

    private String instanceId;

    private Integer projectType;

    private String instanceName;

    private Date eventTime;

    private String message;

    private Date gmtCreate;

    private Date gmtModified;
}

接着创建一个针对该实体的接口,接口继承ElasticsearchRepository<T,ID>接口,该接口有es的若干方法

@Component
public interface EventRepository extends ElasticsearchRepository<Event, Long> {

}

es继承关系图谱

写个测试类

@Test
public void esSaveTest() {
    Event event = new Event() {{
        Date date = new Date(Date.from(LocalDateTime.now().toInstant(ZoneOffset.of("+8"))).getTime());
        setId(1);
        setType(0);
        setLevel(4);
        setProjectName(1);
        setInstanceId("1");
        setProjectType(1);
        setInstanceName("测试project");
        setEventTime(date);
        setMessage("测试message");
        setGmtCreate(date);
        setGmtModified(date);
    }};
    Event save = eventRepository.save(event);
    System.out.println(save.toString());
}

kibana查看运行结果

GET argus/_search
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "argus",
        "_type": "event",
        "_id": "1",
        "_score": 1,
        "_source": {
          "id": 1,
          "type": 0,
          "level": 4,
          "projectName": 1,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project",
          "eventTime": 1578968455089,
          "message": "测试message",
          "gmtCreate": 1578968455089,
          "gmtModified": 1578968455089
        }
      }
    ]
  }
}

java查询es结果

@Test
public void queryES() {
    Optional<Event> byId = eventRepository.findById(1L);
    Event event = byId.get();
    System.out.println(event.toString());
}

Console结果

Event(id=1, type=0, level=4, projectName=1, instanceId=1, projectType=1, instanceName=测试project, eventTime=2020-01-14, message=测试message, gmtCreate=2020-01-14, gmtModified=2020-01-14)

批量保存

@Test
public void esTest1() {
    List<Event> eventList = new ArrayList<>();
    for (int i = 1; i <= 30; i++) {
        Integer id = i;
        Event event = new Event() {{
            Date date = new Date(Date.from(LocalDateTime.now().toInstant(ZoneOffset.of("+8"))).getTime());
            setId(id);
            setType(0);
            setLevel(4);
            setProjectName(id);
            setInstanceId("1");
            setProjectType(1);
            setInstanceName("测试project" + id);
            setEventTime(date);
            setMessage("测试message" + id);
            setGmtCreate(date);
            setGmtModified(date);
        }};
        eventList.add(event);
    }
    Iterable<Event> events = eventRepository.saveAll(eventList);
}

kibana获取查询结果

GET argus/_search

total有29条

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 29,
    "max_score": 1,
    "hits": [
      {
        "_index": "argus",
        "_type": "event",
        "_id": "14",
        "_score": 1,
        "_source": {
          "id": 14,
          "type": 0,
          "level": 4,
          "projectName": 14,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project14",
          "eventTime": 1578986293526,
          "message": "测试message14",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        }
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "19",
        "_score": 1,
        "_source": {
          "id": 19,
          "type": 0,
          "level": 4,
          ...
          ...

java查询

范围查询

@Test
public void queryES() {
    //创建查询体
    BoolQueryBuilder builder = QueryBuilders.boolQuery();
    //设置聚合条件(查询id为4-10不包括4包括10)
    RangeQueryBuilder query = QueryBuilders
            .rangeQuery("id")
            .from(4)    //从4开始
            .to(10)     //到10结束
            .includeLower(false)    //不包括下界
            .includeUpper(true);    //包括上界
    //将聚合条件设置入查询体中
    builder.must(query);
    //进行查询
    Iterable<Event> search = eventRepository.search(builder);
    search.forEach(item -> {
        System.out.println(item.toString());
    });
}
Event(id=5, type=0, level=4, projectName=5, instanceId=1, projectType=1, instanceName=测试project5, eventTime=2020-01-14, message=测试message5, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=8, type=0, level=4, projectName=8, instanceId=1, projectType=1, instanceName=测试project8, eventTime=2020-01-14, message=测试message8, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=9, type=0, level=4, projectName=9, instanceId=1, projectType=1, instanceName=测试project9, eventTime=2020-01-14, message=测试message9, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=10, type=0, level=4, projectName=10, instanceId=1, projectType=1, instanceName=测试project10, eventTime=2020-01-14, message=测试message10, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=6, type=0, level=4, projectName=6, instanceId=1, projectType=1, instanceName=测试project6, eventTime=2020-01-14, message=测试message6, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=7, type=0, level=4, projectName=7, instanceId=1, projectType=1, instanceName=测试project7, eventTime=2020-01-14, message=测试message7, gmtCreate=2020-01-14, gmtModified=2020-01-14)

精准短语查询

@Test
public void queryES() {
    //创建查询体
    BoolQueryBuilder builder = QueryBuilders.boolQuery();
    //精准短语匹配
    MatchPhraseQueryBuilder query = QueryBuilders.matchPhraseQuery("instanceName", "测试project19");
    //将聚合条件设置入查询体中
    builder.must(query);
    //进行查询
    Iterable<Event> search = eventRepository.search(builder);
    search.forEach(item -> {
        System.out.println(item.toString());
    });
}
Event(id=19, type=0, level=4, projectName=19, instanceId=1, projectType=1, instanceName=测试project19, eventTime=2020-01-14, message=测试message19, gmtCreate=2020-01-14, gmtModified=2020-01-14)

完全匹配(匹配分开结果,相似度)

@Test
public void queryES() {
    //创建查询体
    BoolQueryBuilder builder = QueryBuilders.boolQuery();
    //短语匹配
    MatchQueryBuilder query = QueryBuilders.matchQuery("instanceName", "测试project19");
    //将聚合条件设置入查询体中
    builder.must(query);
    //进行查询
    Iterable<Event> search = eventRepository.search(builder);
    search.forEach(item -> {
        System.out.println(item.toString());
    });
}
Event(id=19, type=0, level=4, projectName=19, instanceId=1, projectType=1, instanceName=测试project19, eventTime=2020-01-14, message=测试message19, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=14, type=0, level=4, projectName=14, instanceId=1, projectType=1, instanceName=测试project14, eventTime=2020-01-14, message=测试message14, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=22, type=0, level=4, projectName=22, instanceId=1, projectType=1, instanceName=测试project22, eventTime=2020-01-14, message=测试message22, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=24, type=0, level=4, projectName=24, instanceId=1, projectType=1, instanceName=测试project24, eventTime=2020-01-14, message=测试message24, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=25, type=0, level=4, projectName=25, instanceId=1, projectType=1, instanceName=测试project25, eventTime=2020-01-14, message=测试message25, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=26, type=0, level=4, projectName=26, instanceId=1, projectType=1, instanceName=测试project26, eventTime=2020-01-14, message=测试message26, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=29, type=0, level=4, projectName=29, instanceId=1, projectType=1, instanceName=测试project29, eventTime=2020-01-14, message=测试message29, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=5, type=0, level=4, projectName=5, instanceId=1, projectType=1, instanceName=测试project5, eventTime=2020-01-14, message=测试message5, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=8, type=0, level=4, projectName=8, instanceId=1, projectType=1, instanceName=测试project8, eventTime=2020-01-14, message=测试message8, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=9, type=0, level=4, projectName=9, instanceId=1, projectType=1, instanceName=测试project9, eventTime=2020-01-14, message=测试message9, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=10, type=0, level=4, projectName=10, instanceId=1, projectType=1, instanceName=测试project10, eventTime=2020-01-14, message=测试message10, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=12, type=0, level=4, projectName=12, instanceId=1, projectType=1, instanceName=测试project12, eventTime=2020-01-14, message=测试message12, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=21, type=0, level=4, projectName=21, instanceId=1, projectType=1, instanceName=测试project21, eventTime=2020-01-14, message=测试message21, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=2, type=0, level=4, projectName=2, instanceId=1, projectType=1, instanceName=测试project2, eventTime=2020-01-14, message=测试message2, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=4, type=0, level=4, projectName=4, instanceId=1, projectType=1, instanceName=测试project4, eventTime=2020-01-14, message=测试message4, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=6, type=0, level=4, projectName=6, instanceId=1, projectType=1, instanceName=测试project6, eventTime=2020-01-14, message=测试message6, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=15, type=0, level=4, projectName=15, instanceId=1, projectType=1, instanceName=测试project15, eventTime=2020-01-14, message=测试message15, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=20, type=0, level=4, projectName=20, instanceId=1, projectType=1, instanceName=测试project20, eventTime=2020-01-14, message=测试message20, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=27, type=0, level=4, projectName=27, instanceId=1, projectType=1, instanceName=测试project27, eventTime=2020-01-14, message=测试message27, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=1, type=0, level=4, projectName=1, instanceId=1, projectType=1, instanceName=测试project, eventTime=2020-01-14, message=测试message, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=7, type=0, level=4, projectName=7, instanceId=1, projectType=1, instanceName=测试project7, eventTime=2020-01-14, message=测试message7, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=13, type=0, level=4, projectName=13, instanceId=1, projectType=1, instanceName=测试project13, eventTime=2020-01-14, message=测试message13, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=16, type=0, level=4, projectName=16, instanceId=1, projectType=1, instanceName=测试project16, eventTime=2020-01-14, message=测试message16, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=18, type=0, level=4, projectName=18, instanceId=1, projectType=1, instanceName=测试project18, eventTime=2020-01-14, message=测试message18, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=28, type=0, level=4, projectName=28, instanceId=1, projectType=1, instanceName=测试project28, eventTime=2020-01-14, message=测试message28, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=3, type=0, level=4, projectName=3, instanceId=1, projectType=1, instanceName=测试project3, eventTime=2020-01-14, message=测试message3, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=11, type=0, level=4, projectName=11, instanceId=1, projectType=1, instanceName=测试project11, eventTime=2020-01-14, message=测试message11, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=17, type=0, level=4, projectName=17, instanceId=1, projectType=1, instanceName=测试project17, eventTime=2020-01-14, message=测试message17, gmtCreate=2020-01-14, gmtModified=2020-01-14)
Event(id=23, type=0, level=4, projectName=23, instanceId=1, projectType=1, instanceName=测试project23, eventTime=2020-01-14, message=测试message23, gmtCreate=2020-01-14, gmtModified=2020-01-14)

三种分页查询

1.from+size浅分页

查询所有数据,返回五条数据,从第10条开始

 

GET argus/_search
{
  "query": {
    "match_all": {}
  },
  "size": 5,
  "from": 10,
  "sort": [
    {
      "id": {
        "order": "asc"
      }
    }
  ]
}

原理:查询所有的数据,做数据偏移,截取目标数据,随着数据量越大、开始截取的偏移位置越靠后,性能越慢

 

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 29,
    "max_score": null,
    "hits": [
      {
        "_index": "argus",
        "_type": "event",
        "_id": "11",
        "_score": null,
        "_source": {
          "id": 11,
          "type": 0,
          "level": 4,
          "projectName": 11,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project11",
          "eventTime": 1578986293526,
          "message": "测试message11",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          11
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "12",
        "_score": null,
        "_source": {
          "id": 12,
          "type": 0,
          "level": 4,
          "projectName": 12,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project12",
          "eventTime": 1578986293526,
          "message": "测试message12",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          12
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "13",
        "_score": null,
        "_source": {
          "id": 13,
          "type": 0,
          "level": 4,
          "projectName": 13,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project13",
          "eventTime": 1578986293526,
          "message": "测试message13",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          13
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "14",
        "_score": null,
        "_source": {
          "id": 14,
          "type": 0,
          "level": 4,
          "projectName": 14,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project14",
          "eventTime": 1578986293526,
          "message": "测试message14",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          14
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "15",
        "_score": null,
        "_source": {
          "id": 15,
          "type": 0,
          "level": 4,
          "projectName": 15,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project15",
          "eventTime": 1578986293526,
          "message": "测试message15",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          15
        ]
      }
    ]
  }
}

2.scroll深分页

 

from + size 不能超过10000,也就是说在前10000条之内,可以随意翻页,10000条之后就不行了。scroll 类似于sql中的cursor,使用scroll,每次只能获取一页的内容,然后会返回一个scroll_id。根据返回的这个scroll_id可以不断地获取下一页的内容,所以scroll并不适用于有跳页的情景。

scroll=1m表示scroll_id保留一分钟可用,超时清除

使用scroll必须将from设置为0

 

GET argus/_search?scroll=1m
{
  "query": {
    "match_all": {}
  },
  "size": 5,
  "from": 0,
  "sort": [
    {
      "id": {
        "order": "asc"
      }
    }
  ]
}
{
  "_scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAQWXhFmM1VkY1N3JjVDktRFVocXlpZGZhWUEAAAAAAEGOBhZMaXkwQUV3eFN5R3dxT015R1hweUhRAAAAAABBjggWTGl5MEFFd3hTeUd3cU9NeUdYcHlIUQAAAAAAQWXiFmM1VkY1N3JjVDktRFVocXlpZGZhWUEAAAAAAEGOBxZMaXkwQUV3eFN5R3dxT015R1hweUhR",
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 29,
    "max_score": null,
    "hits": [
      {
        "_index": "argus",
        "_type": "event",
        "_id": "1",
        "_score": null,
        "_source": {
          "id": 1,
          "type": 0,
          "level": 4,
          "projectName": 1,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project",
          "eventTime": 1578968455089,
          "message": "测试message",
          "gmtCreate": 1578968455089,
          "gmtModified": 1578968455089
        },
        "sort": [
          1
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "2",
        "_score": null,
        "_source": {
          "id": 2,
          "type": 0,
          "level": 4,
          "projectName": 2,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project2",
          "eventTime": 1578986288353,
          "message": "测试message2",
          "gmtCreate": 1578986288353,
          "gmtModified": 1578986288353
        },
        "sort": [
          2
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "3",
        "_score": null,
        "_source": {
          "id": 3,
          "type": 0,
          "level": 4,
          "projectName": 3,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project3",
          "eventTime": 1578986290198,
          "message": "测试message3",
          "gmtCreate": 1578986290198,
          "gmtModified": 1578986290198
        },
        "sort": [
          3
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "4",
        "_score": null,
        "_source": {
          "id": 4,
          "type": 0,
          "level": 4,
          "projectName": 4,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project4",
          "eventTime": 1578986291160,
          "message": "测试message4",
          "gmtCreate": 1578986291160,
          "gmtModified": 1578986291160
        },
        "sort": [
          4
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "5",
        "_score": null,
        "_source": {
          "id": 5,
          "type": 0,
          "level": 4,
          "projectName": 5,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project5",
          "eventTime": 1578986293526,
          "message": "测试message5",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          5
        ]
      }
    ]
  }
}

将返回的scroll_id进行读取下一页内容,直到数据读取完毕或者保留时间截止

GET _search/scroll
{
  "scroll_id":"DnF1ZXJ5VGhlbkZldGNoBQAAAAAAQY-nFkxpeTBBRXd4U3lHd3FPTXlHWHB5SFEAAAAAAEFndBZjNVZGNTdyY1Q5LURVaHF5aWRmYVlBAAAAAABBZ3UWYzVWRjU3cmNUOS1EVWhxeWlkZmFZQQAAAAAAQY-oFkxpeTBBRXd4U3lHd3FPTXlHWHB5SFEAAAAAAEFndhZjNVZGNTdyY1Q5LURVaHF5aWRmYVlB",
  "scroll": "1m"
}

从6开始

{
  "_scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQAAAAAAQY-nFkxpeTBBRXd4U3lHd3FPTXlHWHB5SFEAAAAAAEFndBZjNVZGNTdyY1Q5LURVaHF5aWRmYVlBAAAAAABBZ3UWYzVWRjU3cmNUOS1EVWhxeWlkZmFZQQAAAAAAQY-oFkxpeTBBRXd4U3lHd3FPTXlHWHB5SFEAAAAAAEFndhZjNVZGNTdyY1Q5LURVaHF5aWRmYVlB",
  "took": 18,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 29,
    "max_score": null,
    "hits": [
      {
        "_index": "argus",
        "_type": "event",
        "_id": "6",
        "_score": null,
        "_source": {
          "id": 6,
          "type": 0,
          "level": 4,
          "projectName": 6,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project6",
          "eventTime": 1578986293526,
          "message": "测试message6",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          6
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "7",
        "_score": null,
        "_source": {
          "id": 7,
          "type": 0,
          "level": 4,
          "projectName": 7,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project7",
          "eventTime": 1578986293526,
          "message": "测试message7",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          7
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "8",
        "_score": null,
        "_source": {
          "id": 8,
          "type": 0,
          "level": 4,
          "projectName": 8,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project8",
          "eventTime": 1578986293526,
          "message": "测试message8",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          8
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "9",
        "_score": null,
        "_source": {
          "id": 9,
          "type": 0,
          "level": 4,
          "projectName": 9,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project9",
          "eventTime": 1578986293526,
          "message": "测试message9",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          9
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "10",
        "_score": null,
        "_source": {
          "id": 10,
          "type": 0,
          "level": 4,
          "projectName": 10,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project10",
          "eventTime": 1578986293526,
          "message": "测试message10",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          10
        ]
      }
    ]
  }
}

重复上一步查询scroll_id步骤则继续读取

 

 

3.search_after深分页

 

scroll 的方式,官方的建议不用于实时的请求(一般用于数据导出),因为每一个 scroll_id 不仅会占用大量的资源,而且会生成历史快照,对于数据的变更不会反映到快照上。

search_after 分页的方式是根据上一页的最后一条数据来确定下一页的位置,同时在分页请求的过程中,如果有索引数据的增删改查,这些变更也会实时的反映到游标上。但是需要注意,因为每一页的数据依赖于上一页最后一条数据,所以无法跳页请求。

为了找到每一页最后一条数据,每个文档必须有一个全局唯一值,官方推荐使用 _uid 作为全局唯一值,其实使用业务层的 id 也可以。

使用search_after必须要设置from=0

search_after为排序字段最后一条数据的值

 

GET argus/_search
{
  "query": {
    "match_all": {
      
    }
  },
  "size": 5,
  "from": 0,
  "search_after": [
    7
  ],
  "sort": [
    {
      "id": {
        "order": "asc"
      }
    }
  ]
}
{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 29,
    "max_score": null,
    "hits": [
      {
        "_index": "argus",
        "_type": "event",
        "_id": "8",
        "_score": null,
        "_source": {
          "id": 8,
          "type": 0,
          "level": 4,
          "projectName": 8,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project8",
          "eventTime": 1578986293526,
          "message": "测试message8",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          8
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "9",
        "_score": null,
        "_source": {
          "id": 9,
          "type": 0,
          "level": 4,
          "projectName": 9,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project9",
          "eventTime": 1578986293526,
          "message": "测试message9",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          9
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "10",
        "_score": null,
        "_source": {
          "id": 10,
          "type": 0,
          "level": 4,
          "projectName": 10,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project10",
          "eventTime": 1578986293526,
          "message": "测试message10",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          10
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "11",
        "_score": null,
        "_source": {
          "id": 11,
          "type": 0,
          "level": 4,
          "projectName": 11,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project11",
          "eventTime": 1578986293526,
          "message": "测试message11",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          11
        ]
      },
      {
        "_index": "argus",
        "_type": "event",
        "_id": "12",
        "_score": null,
        "_source": {
          "id": 12,
          "type": 0,
          "level": 4,
          "projectName": 12,
          "instanceId": "1",
          "projectType": 1,
          "instanceName": "测试project12",
          "eventTime": 1578986293526,
          "message": "测试message12",
          "gmtCreate": 1578986293526,
          "gmtModified": 1578986293526
        },
        "sort": [
          12
        ]
      }
    ]
  }
}

 

 

 

发布了56 篇原创文章 · 获赞 34 · 访问量 3万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览