public void testAnalyze() {
    String analyzeShardAction = AnalyzeAction.NAME + "[s]";
    interceptTransportActions(analyzeShardAction);

    AnalyzeRequest analyzeRequest = new AnalyzeRequest(randomIndexOrAlias());
    analyzeRequest.text("text");
    internalCluster().clientNodeClient().admin().indices().analyze(analyzeRequest).actionGet();

    clearInterceptedActions();
    assertSameIndices(analyzeRequest, analyzeShardAction);
  }
Пример #2
0
  @Override
  public void handleRequest(final RestRequest request, final RestChannel channel) {
    String text = request.param("text");
    if (text == null && request.hasContent()) {
      text = request.content().toUtf8();
    }
    if (text == null) {
      try {
        channel.sendResponse(
            new XContentThrowableRestResponse(
                request, new ElasticsearchIllegalArgumentException("text is missing")));
      } catch (IOException e1) {
        logger.warn("Failed to send response", e1);
      }
      return;
    }

    AnalyzeRequest analyzeRequest = new AnalyzeRequest(request.param("index"), text);
    analyzeRequest.listenerThreaded(false);
    analyzeRequest.preferLocal(
        request.paramAsBoolean("prefer_local", analyzeRequest.preferLocalShard()));
    analyzeRequest.analyzer(request.param("analyzer"));
    analyzeRequest.field(request.param("field"));
    analyzeRequest.tokenizer(request.param("tokenizer"));
    analyzeRequest.tokenFilters(
        request.paramAsStringArray(
            "token_filters", request.paramAsStringArray("filters", analyzeRequest.tokenFilters())));
    analyzeRequest.charFilters(
        request.paramAsStringArray("char_filters", analyzeRequest.charFilters()));
    client
        .admin()
        .indices()
        .analyze(
            analyzeRequest,
            new ActionListener<AnalyzeResponse>() {
              @Override
              public void onResponse(AnalyzeResponse response) {
                try {
                  XContentBuilder builder = restContentBuilder(request, null);
                  builder.startObject();
                  response.toXContent(builder, request);
                  builder.endObject();
                  channel.sendResponse(new XContentRestResponse(request, OK, builder));
                } catch (Throwable e) {
                  onFailure(e);
                }
              }

              @Override
              public void onFailure(Throwable e) {
                try {
                  channel.sendResponse(new XContentThrowableRestResponse(request, e));
                } catch (IOException e1) {
                  logger.error("Failed to send failure response", e1);
                }
              }
            });
  }