请教一个 Java 查询 elasticsearch 的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zshineee
V2EX    Java

请教一个 Java 查询 elasticsearch 的问题

  •  
  •   zshineee 2023-12-20 13:16:44 +08:00 2266 次点击
    这是一个创建于 738 天前的主题,其中的信息可能已经有所发展或是发生改变。
    //代码
    Long startTime = System.currentTimeMillis()
    SearchResponse searchRespOnse= restHighlevelclient.search(searchRequest, RequestOptions.DEFAULT);
    Long endTime = System.currentTimeMillis();
    log.info("took time:{},execution time:{}",searchResponse.getTook() , (endTime - startTime) + "ms"):

    //日志
    took time:20ms ,execution time:80ms
    took time:19ms ,execution time:77ms
    took time:48ms ,execution time:349ms
    took time:18ms ,execution time:65ms
    took time:34ms ,execution time:884ms
    took time:19ms ,execution time:59ms
    took time:16ms ,execution time:55ms
    took time:19ms ,execution time:1113ms
    tookt ime:24ms ,execution time:65ms
    tookt ime:16ms ,execution time:56ms
    tookt ime:16ms ,execution time:909ms

    索引总条数是 2w 条数据,查询条数是 1w 条,实际返回 6000 多条,条件很简单就一个 match ,发现代码实际执行时间不太稳定,应该不需要 1s ,请教下是什么原因?该怎么排查?
    7 条回复    2023-12-26 10:10:55 +08:00
    connor123
        1
    connor123  
       2023-12-20 14:05:54 +08:00
    反序列化慢?我觉得是不是一次反序列化数据太多了,先用 10 条试试看
    Morriaty
        2
    Morriaty  
       2023-12-20 14:22:40 +08:00
    The time reported by elasticsearch in the "took" field is the time that it
    took elasticsearch to process the query on its side. It doesn't include

    - serializing the request into JSON on the client

    - sending the request over the network

    - deserializing the request from JSON on the server

    - serializing the response into JSON on the server

    - sending the response over the network

    - deserializing the response from JSON on the client
    lvtuyukuai
        3
    lvtuyukuai  
       2023-12-20 14:25:43 +08:00
    took time 应该是表示 elasticsearch 服务器端的执行时间,看起来比较稳定。
    execution time 大致可以表示:组装请求 + 发送 http 请求到 es + took time + 从 es 接收 http 响应 + 解析响应
    所以问题可能是在 「发送 http 请求到 es 、从 es 接收 http 响应」(网络不稳定?)、解析响应(响应内容太大?)
    lvtuyukuai
        4
    lvtuyukuai  
       2023-12-20 14:26:23 +08:00
    好吧,跟二楼重复了,回复前没刷新
    zshineee
        5
    zshineee  
    OP
       2023-12-20 15:47:38 +08:00
    @connor123 @lvtuyukuai @Morriaty

    调整了下 size ,10 条好像没什么问题,1000 条偶尔会出现问题

    然后试了下 scroll api ,发现 size 是 10 、100 时,took 时长和 execution 时长都会出现 800 多 ms
    lvtuyukuai
        6
    lvtuyukuai  
       2023-12-20 1804:38 +08:00
    scroll api 时,是首次请求以及之后使用 scrollId 调用都是 800 多 ms 吗?需要注意的是,使用 scroll api 时,那个 size 是每一页(每次滚动请求)返回的结果文档数目,而不是总共要返回的文档数目
    Aresxue
        7
    Aresxue  
       2023-12-26 10:10:55 +08:00
    应该不是服务端问题 ,用 arthas trace 一下看看,实际中 es 的连接数、反序列化都会影响最终的性能。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1124 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 33ms UTC 17:47 PVG 01:47 LAX 09:47 JFK 12:47
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86