@Override public void handleRequest(final RestRequest request, final RestChannel channel) { MultiSearchRequest multiSearchRequest = new MultiSearchRequest(); multiSearchRequest.listenerThreaded(false); String[] indices = Strings.splitStringByCommaToArray(request.param("index")); String[] types = Strings.splitStringByCommaToArray(request.param("type")); IndicesOptions indicesOptions = IndicesOptions.fromRequest(request, multiSearchRequest.indicesOptions()); try { multiSearchRequest.add( request.content(), request.contentUnsafe(), indices, types, request.param("search_type"), request.param("routing"), indicesOptions, allowExplicitIndex); } catch (Exception e) { try { XContentBuilder builder = restContentBuilder(request); channel.sendResponse( new XContentRestResponse( request, BAD_REQUEST, builder.startObject().field("error", e.getMessage()).endObject())); } catch (IOException e1) { logger.error("Failed to send failure response", e1); } return; } client.multiSearch( multiSearchRequest, new ActionListener<MultiSearchResponse>() { @Override public void onResponse(MultiSearchResponse response) { try { XContentBuilder builder = restContentBuilder(request); 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); } } }); }
@Override public void handleRequest( final RestRequest request, final RestChannel channel, final NodeClient client) { ForceMergeRequest mergeRequest = new ForceMergeRequest(Strings.splitStringByCommaToArray(request.param("index"))); mergeRequest.indicesOptions(IndicesOptions.fromRequest(request, mergeRequest.indicesOptions())); mergeRequest.maxNumSegments( request.paramAsInt("max_num_segments", mergeRequest.maxNumSegments())); mergeRequest.onlyExpungeDeletes( request.paramAsBoolean("only_expunge_deletes", mergeRequest.onlyExpungeDeletes())); mergeRequest.flush(request.paramAsBoolean("flush", mergeRequest.flush())); client .admin() .indices() .forceMerge( mergeRequest, new RestBuilderListener<ForceMergeResponse>(channel) { @Override public RestResponse buildResponse( ForceMergeResponse response, XContentBuilder builder) throws Exception { builder.startObject(); buildBroadcastShardsHeader(builder, request, response); builder.endObject(); return new BytesRestResponse(OK, builder); } }); }
private RestChannelConsumer parseExistingDocPercolate( PercolateRequest percolateRequest, RestRequest restRequest, NodeClient client) { String index = restRequest.param("index"); String type = restRequest.param("type"); percolateRequest.indices( Strings.splitStringByCommaToArray(restRequest.param("percolate_index", index))); percolateRequest.documentType(restRequest.param("percolate_type", type)); GetRequest getRequest = new GetRequest(index, type, restRequest.param("id")); getRequest.routing(restRequest.param("routing")); getRequest.preference(restRequest.param("preference")); getRequest.refresh(restRequest.paramAsBoolean("refresh", getRequest.refresh())); getRequest.realtime(restRequest.paramAsBoolean("realtime", getRequest.realtime())); getRequest.version(RestActions.parseVersion(restRequest)); getRequest.versionType( VersionType.fromString(restRequest.param("version_type"), getRequest.versionType())); percolateRequest.getRequest(getRequest); percolateRequest.routing(restRequest.param("percolate_routing")); percolateRequest.preference(restRequest.param("percolate_preference")); percolateRequest.source(restRequest.contentOrSourceParam()); percolateRequest.indicesOptions( IndicesOptions.fromRequest(restRequest, percolateRequest.indicesOptions())); return channel -> executePercolate(client, percolateRequest, channel); }
private RestChannelConsumer parseDocPercolate( PercolateRequest percolateRequest, RestRequest restRequest, NodeClient client) { percolateRequest.indices(Strings.splitStringByCommaToArray(restRequest.param("index"))); percolateRequest.documentType(restRequest.param("type")); percolateRequest.routing(restRequest.param("routing")); percolateRequest.preference(restRequest.param("preference")); percolateRequest.source(restRequest.contentOrSourceParam()); percolateRequest.indicesOptions( IndicesOptions.fromRequest(restRequest, percolateRequest.indicesOptions())); return channel -> executePercolate(client, percolateRequest, channel); }
@Override public void handleRequest(final RestRequest request, final RestChannel channel) { final String[] aliases = request.paramAsStringArrayOrEmptyIfAll("name"); final String[] indices = Strings.splitStringByCommaToArray(request.param("index")); final GetAliasesRequest getAliasesRequest = new GetAliasesRequest(aliases); getAliasesRequest.indices(indices); getAliasesRequest.indicesOptions( IndicesOptions.fromRequest(request, getAliasesRequest.indicesOptions())); getAliasesRequest.local(request.paramAsBoolean("local", getAliasesRequest.local())); client .admin() .indices() .getAliases( getAliasesRequest, new RestBuilderListener<GetAliasesResponse>(channel) { @Override public RestResponse buildResponse( GetAliasesResponse response, XContentBuilder builder) throws Exception { // empty body, if indices were specified but no aliases were if (indices.length > 0 && response.getAliases().isEmpty()) { return new BytesRestResponse(OK, builder.startObject().endObject()); } else if (response.getAliases().isEmpty()) { String message = String.format( Locale.ROOT, "alias [%s] missing", toNamesString(getAliasesRequest.aliases())); builder .startObject() .field("error", message) .field("status", RestStatus.NOT_FOUND.getStatus()) .endObject(); return new BytesRestResponse(RestStatus.NOT_FOUND, builder); } builder.startObject(); for (ObjectObjectCursor<String, List<AliasMetaData>> entry : response.getAliases()) { builder.startObject(entry.key, XContentBuilder.FieldCaseConversion.NONE); builder.startObject(Fields.ALIASES); for (AliasMetaData alias : entry.value) { AliasMetaData.Builder.toXContent(alias, builder, ToXContent.EMPTY_PARAMS); } builder.endObject(); builder.endObject(); } builder.endObject(); return new BytesRestResponse(OK, builder); } }); }
@Override public void handleRequest( final RestRequest request, final RestChannel channel, final Client client) throws IOException { final SearchRequest searchRequest = new SearchRequest( Strings.splitStringByCommaToArray(request.param("index")), new SearchSourceBuilder()); searchRequest.indicesOptions( IndicesOptions.fromRequest(request, searchRequest.indicesOptions())); if (RestActions.hasBodyContent(request)) { final BytesReference sourceBytes = RestActions.getRestContent(request); try (XContentParser parser = XContentFactory.xContent(sourceBytes).createParser(sourceBytes)) { final QueryParseContext context = new QueryParseContext(queryRegistry, parser, parseFieldMatcher); searchRequest.source().suggest(SuggestBuilder.fromXContent(context, suggesters)); } } else { throw new IllegalArgumentException("no content or source provided to execute suggestion"); } searchRequest.routing(request.param("routing")); searchRequest.preference(request.param("preference")); client.search( searchRequest, new RestBuilderListener<SearchResponse>(channel) { @Override public RestResponse buildResponse(SearchResponse response, XContentBuilder builder) throws Exception { RestStatus restStatus = RestStatus.status( response.getSuccessfulShards(), response.getTotalShards(), response.getShardFailures()); builder.startObject(); buildBroadcastShardsHeader( builder, request, response.getTotalShards(), response.getSuccessfulShards(), response.getFailedShards(), response.getShardFailures()); Suggest suggest = response.getSuggest(); if (suggest != null) { suggest.toInnerXContent(builder, request); } builder.endObject(); return new BytesRestResponse(restStatus, builder); } }); }
void parseDocPercolate( PercolateRequest percolateRequest, RestRequest restRequest, RestChannel restChannel, final Client client) { percolateRequest.indices(Strings.splitStringByCommaToArray(restRequest.param("index"))); percolateRequest.documentType(restRequest.param("type")); percolateRequest.routing(restRequest.param("routing")); percolateRequest.preference(restRequest.param("preference")); percolateRequest.source(RestActions.getRestContent(restRequest)); percolateRequest.indicesOptions( IndicesOptions.fromRequest(restRequest, percolateRequest.indicesOptions())); executePercolate(percolateRequest, restChannel, client); }
@Override public void handleRequest(final RestRequest request, final RestChannel channel) { String[] indices = Strings.splitStringByCommaToArray(request.param("index")); final ClusterSearchShardsRequest clusterSearchShardsRequest = Requests.clusterSearchShardsRequest(indices); clusterSearchShardsRequest.local( request.paramAsBoolean("local", clusterSearchShardsRequest.local())); clusterSearchShardsRequest.listenerThreaded(false); clusterSearchShardsRequest.types(Strings.splitStringByCommaToArray(request.param("type"))); clusterSearchShardsRequest.routing(request.param("routing")); clusterSearchShardsRequest.preference(request.param("preference")); clusterSearchShardsRequest.indicesOptions( IndicesOptions.fromRequest(request, clusterSearchShardsRequest.indicesOptions())); client .admin() .cluster() .searchShards( clusterSearchShardsRequest, new ActionListener<ClusterSearchShardsResponse>() { @Override public void onResponse(ClusterSearchShardsResponse response) { try { XContentBuilder builder = RestXContentBuilder.restContentBuilder(request); builder.startObject(); response.toXContent(builder, request); builder.endObject(); channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder)); } catch (Throwable e) { onFailure(e); } } @Override public void onFailure(Throwable e) { try { channel.sendResponse(new BytesRestResponse(request, e)); } catch (IOException e1) { logger.error("Failed to send failure response", e1); } } }); }
@Override public void handleRequest(final RestRequest request, final RestChannel channel) { PutWarmerRequest putWarmerRequest = new PutWarmerRequest(request.param("name")); putWarmerRequest.listenerThreaded(false); SearchRequest searchRequest = new SearchRequest(Strings.splitStringByCommaToArray(request.param("index"))) .types(Strings.splitStringByCommaToArray(request.param("type"))) .source(request.content(), request.contentUnsafe()); searchRequest.indicesOptions( IndicesOptions.fromRequest(request, searchRequest.indicesOptions())); putWarmerRequest.searchRequest(searchRequest); putWarmerRequest.timeout(request.paramAsTime("timeout", putWarmerRequest.timeout())); putWarmerRequest.masterNodeTimeout( request.paramAsTime("master_timeout", putWarmerRequest.masterNodeTimeout())); client .admin() .indices() .putWarmer( putWarmerRequest, new AcknowledgedRestResponseActionListener(request, channel, logger)); }
public static SearchRequest parseSearchRequest(RestRequest request) { String[] indices = Strings.splitStringByCommaToArray(request.param("index")); SearchRequest searchRequest = new SearchRequest(indices); // get the content, and put it in the body // add content/source as template if template flag is set boolean isTemplateRequest = request.path().endsWith("/template"); if (RestActions.hasBodyContent(request)) { if (isTemplateRequest) { searchRequest.templateSource(RestActions.getRestContent(request)); } else { searchRequest.source(RestActions.getRestContent(request)); } } // do not allow 'query_and_fetch' or 'dfs_query_and_fetch' search types // from the REST layer. these modes are an internal optimization and should // not be specified explicitly by the user. String searchType = request.param("search_type"); if (SearchType.fromString(searchType).equals(SearchType.QUERY_AND_FETCH) || SearchType.fromString(searchType).equals(SearchType.DFS_QUERY_AND_FETCH)) { throw new IllegalArgumentException("Unsupported search type [" + searchType + "]"); } else { searchRequest.searchType(searchType); } searchRequest.extraSource(parseSearchSource(request)); searchRequest.queryCache(request.paramAsBoolean("query_cache", null)); String scroll = request.param("scroll"); if (scroll != null) { searchRequest.scroll(new Scroll(parseTimeValue(scroll, null, "scroll"))); } searchRequest.types(Strings.splitStringByCommaToArray(request.param("type"))); searchRequest.routing(request.param("routing")); searchRequest.preference(request.param("preference")); searchRequest.indicesOptions( IndicesOptions.fromRequest(request, searchRequest.indicesOptions())); return searchRequest; }
@Override public void handleRequest(final RestRequest request, final RestChannel channel) { TypesExistsRequest typesExistsRequest = new TypesExistsRequest( Strings.splitStringByCommaToArray(request.param("index")), Strings.splitStringByCommaToArray(request.param("type"))); typesExistsRequest.listenerThreaded(false); typesExistsRequest.local(request.paramAsBoolean("local", typesExistsRequest.local())); typesExistsRequest.indicesOptions( IndicesOptions.fromRequest(request, typesExistsRequest.indicesOptions())); client .admin() .indices() .typesExists( typesExistsRequest, new ActionListener<TypesExistsResponse>() { @Override public void onResponse(TypesExistsResponse response) { try { if (response.isExists()) { channel.sendResponse(new BytesRestResponse(OK)); } else { channel.sendResponse(new BytesRestResponse(NOT_FOUND)); } } catch (Throwable e) { onFailure(e); } } @Override public void onFailure(Throwable e) { try { channel.sendResponse(new BytesRestResponse(ExceptionsHelper.status(e))); } catch (Exception e1) { logger.error("Failed to send failure response", e1); } } }); }
@Override public void handleRequest( final RestRequest request, final RestChannel channel, final Client client) throws IOException { DeleteByQueryRequest delete = new DeleteByQueryRequest(Strings.splitStringByCommaToArray(request.param("index"))); delete.indicesOptions(IndicesOptions.fromRequest(request, delete.indicesOptions())); delete.routing(request.param("routing")); if (request.hasParam("timeout")) { delete.timeout(request.paramAsTime("timeout", null)); } if (request.hasContent()) { XContentParser requestParser = XContentFactory.xContent(request.content()).createParser(request.content()); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry); context.reset(requestParser); context.parseFieldMatcher(parseFieldMatcher); final QueryBuilder<?> builder = context.parseInnerQueryBuilder(); delete.query(builder); } else { String source = request.param("source"); if (source != null) { XContentParser requestParser = XContentFactory.xContent(source).createParser(source); QueryParseContext context = new QueryParseContext(indicesQueriesRegistry); context.reset(requestParser); final QueryBuilder<?> builder = context.parseInnerQueryBuilder(); delete.query(builder); } else { QueryBuilder<?> queryBuilder = RestActions.urlParamsToQueryBuilder(request); if (queryBuilder != null) { delete.query(queryBuilder); } } } delete.types(Strings.splitStringByCommaToArray(request.param("type"))); client.execute(INSTANCE, delete, new RestToXContentListener<DeleteByQueryResponse>(channel)); }
@Override public void handleRequest( final RestRequest request, final RestChannel channel, final Client client) { IndicesStatsRequest indicesStatsRequest = new IndicesStatsRequest(); indicesStatsRequest.indicesOptions( IndicesOptions.fromRequest(request, indicesStatsRequest.indicesOptions())); indicesStatsRequest.indices(Strings.splitStringByCommaToArray(request.param("index"))); indicesStatsRequest.types(Strings.splitStringByCommaToArray(request.param("types"))); Set<String> metrics = Strings.splitStringByCommaToSet(request.param("metric", "_all")); // short cut, if no metrics have been specified in URI if (metrics.size() == 1 && metrics.contains("_all")) { indicesStatsRequest.all(); } else { indicesStatsRequest.clear(); indicesStatsRequest.docs(metrics.contains("docs")); indicesStatsRequest.store(metrics.contains("store")); indicesStatsRequest.indexing(metrics.contains("indexing")); indicesStatsRequest.search(metrics.contains("search")); indicesStatsRequest.get(metrics.contains("get")); indicesStatsRequest.merge(metrics.contains("merge")); indicesStatsRequest.refresh(metrics.contains("refresh")); indicesStatsRequest.flush(metrics.contains("flush")); indicesStatsRequest.warmer(metrics.contains("warmer")); indicesStatsRequest.queryCache(metrics.contains("query_cache")); indicesStatsRequest.percolate(metrics.contains("percolate")); indicesStatsRequest.segments(metrics.contains("segments")); indicesStatsRequest.fieldData(metrics.contains("fielddata")); indicesStatsRequest.completion(metrics.contains("completion")); indicesStatsRequest.suggest(metrics.contains("suggest")); indicesStatsRequest.requestCache(metrics.contains("request_cache")); indicesStatsRequest.recovery(metrics.contains("recovery")); indicesStatsRequest.translog(metrics.contains("translog")); } if (request.hasParam("groups")) { indicesStatsRequest.groups(Strings.splitStringByCommaToArray(request.param("groups"))); } if (request.hasParam("types")) { indicesStatsRequest.types(Strings.splitStringByCommaToArray(request.param("types"))); } if (indicesStatsRequest.completion() && (request.hasParam("fields") || request.hasParam("completion_fields"))) { indicesStatsRequest.completionFields( request.paramAsStringArray( "completion_fields", request.paramAsStringArray("fields", Strings.EMPTY_ARRAY))); } if (indicesStatsRequest.fieldData() && (request.hasParam("fields") || request.hasParam("fielddata_fields"))) { indicesStatsRequest.fieldDataFields( request.paramAsStringArray( "fielddata_fields", request.paramAsStringArray("fields", Strings.EMPTY_ARRAY))); } client .admin() .indices() .stats( indicesStatsRequest, new RestBuilderListener<IndicesStatsResponse>(channel) { @Override public RestResponse buildResponse( IndicesStatsResponse response, XContentBuilder builder) throws Exception { builder.startObject(); buildBroadcastShardsHeader(builder, request, response); response.toXContent(builder, request); builder.endObject(); return new BytesRestResponse(OK, builder); } }); }