Java Code Examples for org.elasticsearch.client.Client#search()

The following examples show how to use org.elasticsearch.client.Client#search() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Elasticsearch   File: RestCountAction.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void doRequest(final RestRequest request, final RestChannel channel, final Client client) {
    String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    CountRequest countRequest = new CountRequest(indices);
    String source = request.param("source");
    if (source != null) {
        countRequest.source(source);
    } else {
        QuerySourceBuilder querySourceBuilder = RestActions.parseQuerySource(request);
        if (querySourceBuilder != null) {
            countRequest.source(querySourceBuilder);
        }
    }

    client.search(countRequest.toSearchRequest(), new RestResponseListener<SearchResponse>(channel) {
        @Override
        public RestResponse buildResponse(SearchResponse countResponse) throws Exception {
            return RestTable.buildResponse(buildTable(request, countResponse), channel);
        }
    });
}
 
Example 2
/**
 * Make async request to get the number of detectors in AnomalyDetector.ANOMALY_DETECTORS_INDEX if necessary
 * and, onResponse, gather the cluster statistics
 *
 * @param client Client
 * @param listener MultiResponsesDelegateActionListener to be used once both requests complete
 * @param adStatsRequest Request containing stats to be retrieved
 */
private void getClusterStats(
    Client client,
    MultiResponsesDelegateActionListener<ADStatsResponse> listener,
    ADStatsRequest adStatsRequest
) {
    ADStatsResponse adStatsResponse = new ADStatsResponse();
    if (adStatsRequest.getStatsToBeRetrieved().contains(StatNames.DETECTOR_COUNT.getName())) {
        if (clusterService.state().getRoutingTable().hasIndex(AnomalyDetector.ANOMALY_DETECTORS_INDEX)) {
            final SearchRequest request = client
                .prepareSearch(AnomalyDetector.ANOMALY_DETECTORS_INDEX)
                .setSize(0)
                .setTrackTotalHits(true)
                .request();
            client.search(request, ActionListener.wrap(indicesStatsResponse -> {
                adStats.getStat(StatNames.DETECTOR_COUNT.getName()).setValue(indicesStatsResponse.getHits().getTotalHits().value);
                adStatsResponse.setClusterStats(getClusterStatsMap(adStatsRequest));
                listener.onResponse(adStatsResponse);
            }, e -> listener.onFailure(new RuntimeException("Failed to get AD cluster stats", e))));
        } else {
            adStats.getStat(StatNames.DETECTOR_COUNT.getName()).setValue(0L);
            adStatsResponse.setClusterStats(getClusterStatsMap(adStatsRequest));
            listener.onResponse(adStatsResponse);
        }
    } else {
        adStatsResponse.setClusterStats(getClusterStatsMap(adStatsRequest));
        listener.onResponse(adStatsResponse);
    }
}
 
Example 3
Source Project: Elasticsearch   File: RestCountAction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    CountRequest countRequest = new CountRequest(Strings.splitStringByCommaToArray(request.param("index")));
    countRequest.indicesOptions(IndicesOptions.fromRequest(request, countRequest.indicesOptions()));
    if (RestActions.hasBodyContent(request)) {
        countRequest.source(RestActions.getRestContent(request));
    } else {
        QuerySourceBuilder querySourceBuilder = RestActions.parseQuerySource(request);
        if (querySourceBuilder != null) {
            countRequest.source(querySourceBuilder);
        }
    }
    countRequest.routing(request.param("routing"));
    countRequest.minScore(request.paramAsFloat("min_score", DEFAULT_MIN_SCORE));
    countRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
    countRequest.preference(request.param("preference"));

    final int terminateAfter = request.paramAsInt("terminate_after", DEFAULT_TERMINATE_AFTER);
    if (terminateAfter < 0) {
        throw new IllegalArgumentException("terminateAfter must be > 0");
    } else if (terminateAfter > 0) {
        countRequest.terminateAfter(terminateAfter);
    }
    client.search(countRequest.toSearchRequest(), new RestBuilderListener<SearchResponse>(channel) {
        @Override
        public RestResponse buildResponse(SearchResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            if (terminateAfter != DEFAULT_TERMINATE_AFTER) {
                builder.field("terminated_early", response.isTerminatedEarly());
            }
            builder.field("count", response.getHits().totalHits());
            buildBroadcastShardsHeader(builder, request, response.getTotalShards(), response.getSuccessfulShards(),
                    response.getFailedShards(), response.getShardFailures());

            builder.endObject();
            return new BytesRestResponse(response.status(), builder);
        }
    });
}
 
Example 4
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    SearchRequest searchRequest = RestSearchAction.parseSearchRequest(request);
    searchRequest.listenerThreaded(false);
    client.search(searchRequest, new CSVToXContentListener(channel,
            request.paramAsStringArray("keys", null),
            request.paramAsBoolean("with_index", false),
                    request.paramAsBoolean("with_type", false),
                    request.paramAsBoolean("with_id", false))
    );
}
 
Example 5
Source Project: Elasticsearch   File: RestSearchAction.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    SearchRequest searchRequest = new SearchRequest();
    RestSearchAction.parseSearchRequest(searchRequest, request, parseFieldMatcher, null);
    client.search(searchRequest, new RestStatusToXContentListener<SearchResponse>(channel));
}