elasticsearch源码分析-07GET查询流程

GET查询流程

ES的读取分为Get和search两种,get是根据id从索引中获取内容,而search是根据关键词从倒排索引中获取内容

get请求的处理在actionModule中进行注册

//get请求处理
registerHandler.accept(new RestGetAction());

请求的路由为

public List<Route> routes() {
   
    return unmodifiableList(asList(
        new Route(GET, "/{index}/_doc/{id}"),
        new Route(HEAD, "/{index}/_doc/{id}"),
        // Deprecated typed endpoints.
        new Route(GET, "/{index}/{type}/{id}"),
        new Route(HEAD, "/{index}/{type}/{id}")));
}

当节点接收到rest请求后会分发到不同的handler进行处理

void dispatchRequest(final RestRequest restRequest, final RestChannel channel, final Throwable badRequestCause) {
   
    final ThreadContext threadContext = threadPool.getThreadContext();
    try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
   
        //错误请求处理
        if (badRequestCause != null) {
   
            dispatcher.dispatchBadRequest(channel, threadContext, badRequestCause);
        } else {
   
            //正常请求处理
            dispatcher.dispatchRequest(restRequest, channel, threadContext);
        }
    }
}

public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) {
   
    //小图标请求处理
    if (request.rawPath().equals("/favicon.ico")) {
   
        handleFavicon(request.method(), request.uri(), channel);
        return;
    }
    try {
   
        //处理rest请求
        tryAllHandlers(request, channel, threadContext);
    } catch (Exception e) {
   
        try {
   
            channel.sendResponse(new BytesRestResponse(channel, e));
        } catch (Exception inner) {
   
            inner.addSuppressed(e);
            logger.error(() ->
                         new ParameterizedMessage("failed to send failure response for uri [{}]", request.uri()), inner);
        }
    }
}

最终执行到BaseRestHandler的handleRequest方法

@Override
    public final void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
   
        // prepare the request for execution; has the side effect of touching the request parameters
        //处理请求
        final RestChannelConsumer action = prepareRequest(request, client);

        // validate unconsumed params, but we must exclude params used to format the response
        // use a sorted set so the unconsumed parameters appear in a reliable sorted order
        final SortedSet<String> unconsumedParams =
            request.unconsumedParams().stream().filter(p -> !responseParams().contains(p)).collect(Collectors.toCollection(TreeSet::new));

        // validate the non-response params
        if (!unconsumedParams.isEmpty()) {
   
            final Set<String> candidateParams = new HashSet<>();
            candidateParams.addAll(request.consumedParams());
            candidateParams.addAll(responseParams());
            throw new IllegalArgumentException(unrecognized(request, unconsumedParams, candidateParams, "parameter"));
        }

        if (request.hasContent() && request.isContentConsumed() == false) {
   
            throw new IllegalArgumentException("request [" + request.method() + " " + request.path() + "] does not support having a body");
        }

        usageCount.increment();
        // execute the action
        action.accept(channel);
    }

调用prepareRequest将请求参数封装近request中

@Override
    public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
   
        GetRequest getRequest;
        //处理type类型默认_doc
        if (request.hasParam("type")) {
   
            deprecationLogger.deprecatedAndMaybeLog("get_with_types", TYPES_DEPRECATION_MESSAGE);
            getRequest = new GetRequest(request.param("index"), request.param("type"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值