在查询过程中,ES是将整个查询分成几个阶段的,大体如下:

  • QueryPhase

  • rescorePhase

  • suggestPhase

  • aggregationPhase

  • FetchPhase

对于全文检索,可能还有DFSPhase。从源代码QueryPhase 类可以看出

@Override
    public void execute(SearchContext searchContext) throws QueryPhaseExecutionException {
         //创建AggregationContext,
        //初始化所有的Aggregator
        aggregationPhase.preProcess(searchContext);
        //实际query,还有聚合操作其实是在这部完成的
        boolean rescore = execute(searchContext, searchContext.searcher());

        //如果是全文检索,并且需要打分
        if (rescore) { // only if we do a regular search
            rescorePhase.execute(searchContext);
        }
        suggestPhase.execute(searchContext);
         //获取聚合结果
        aggregationPhase.execute(searchContext);

        if (searchContext.getProfilers() != null) {
            List shardResults = Profiler.buildShardResults(searchContext.getProfilers().getProfilers());
            searchContext.queryResult().profileResults(shardResults);
        }
    }