org.elasticsearch.action.count.CountRequest Java Examples

The following examples show how to use org.elasticsearch.action.count.CountRequest. 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 File: RestCountAction.java    From Elasticsearch with 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
Source File: AbstractIMAPRiverUnitTest.java    From elasticsearch-imap with Apache License 2.0 6 votes vote down vote up
protected long getCount(final List<String> indices, final String type) {
    logger.debug("getCount() for index {} and type", indices, type);
    
    esSetup.client().admin().indices().refresh(new RefreshRequest()).actionGet();

    long count = 0;
    
    for (Iterator<String> iterator = indices.iterator(); iterator.hasNext();) {
        String index = (String) iterator.next();
         long lcount = esSetup.client().count(new CountRequest(index).types(type)).actionGet().getCount();
         logger.debug("Count for index {} (type {}) is {}", index, type, lcount);
         count += lcount;
    }

    return count;
}
 
Example #3
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public ActionFuture<CountResponse> count(final CountRequest request) {
    AdapterActionFuture<CountResponse, SearchResponse> actionFuture = new AdapterActionFuture<CountResponse, SearchResponse>() {
        @Override
        protected CountResponse convert(SearchResponse listenerResponse) {
            return new CountResponse(listenerResponse);
        }
    };
    deprecationLogger.deprecated("the count api is deprecated and will be removed from the java api in the next major version");
    execute(SearchAction.INSTANCE, request.toSearchRequest(), actionFuture);
    return actionFuture;
}
 
Example #4
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void count(final CountRequest request, final ActionListener<CountResponse> listener) {
    deprecationLogger.deprecated("the count api is deprecated and will be removed from the java api in the next major version");
    execute(SearchAction.INSTANCE, request.toSearchRequest(), new DelegatingActionListener<SearchResponse, CountResponse>(listener) {
        @Override
        protected CountResponse getDelegatedFromInstigator(SearchResponse response) {
            return new CountResponse(response);
        }
    });
}
 
Example #5
Source File: RestCountAction.java    From Elasticsearch with 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 #6
Source File: TestElasticSearchSink.java    From suro with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultArgument() throws IOException {
    String index = "topic";

    createDefaultESSink(index);

    refresh();
    CountResponse countResponse = client().count(new CountRequest(index)).actionGet();
    assertEquals(countResponse.getCount(), 100);
}
 
Example #7
Source File: CountRequestBuilder.java    From elasticshell with Apache License 2.0 5 votes vote down vote up
@Override
protected XContentBuilder toXContent(CountRequest request, CountResponse response, XContentBuilder builder) throws IOException {
    builder.startObject();
    builder.field(Fields.COUNT, response.getCount());
    buildBroadcastShardsHeader(builder, response);
    builder.endObject();
    return builder;
}
 
Example #8
Source File: TestElasticSearchSink.java    From suro with Apache License 2.0 4 votes vote down vote up
@Test
public void testIndexInfoBuilder() throws IOException {
    ObjectMapper jsonMapper = new DefaultObjectMapper();
    Properties props = new Properties();
    props.setProperty("dateFormat", "YYYYMMdd");
    ElasticSearchSink sink = new ElasticSearchSink(
        "testIndexInfoBuilder",
        null,
        1,
        1000,
        Lists.newArrayList("localhost:" + getPort()),
        new DefaultIndexInfoBuilder(
            null,
            null,
            new TimestampField("ts", null),
            new IndexSuffixFormatter("date", props),
            null,
            jsonMapper),
        0,0,0,0,0,
        null,
        false,
        jsonMapper,
        null
    );
    sink.open();

    DateTime dt = new DateTime("2014-10-12T12:12:12.000Z");

    Map<String, Object> msg = new ImmutableMap.Builder<String, Object>()
        .put("f1", "v1")
        .put("f2", "v2")
        .put("f3", "v3")
        .put("ts", dt.getMillis())
        .build();

    String routingKey = "topic";
    String index = "topic20141012";
    for (int i = 0; i < 100; ++i) {
        sink.writeTo(new DefaultMessageContainer(new Message(routingKey, jsonMapper.writeValueAsBytes(msg)), jsonMapper));
    }
    sink.close();

    refresh();
    CountResponse countResponse = client().count(new CountRequest(index)).actionGet();
    assertEquals(countResponse.getCount(), 100);
}
 
Example #9
Source File: TestElasticSearchSink.java    From suro with Apache License 2.0 4 votes vote down vote up
@Test
public void testRecover() throws Exception {
    ObjectMapper jsonMapper = new DefaultObjectMapper();
    ElasticSearchSink sink = new ElasticSearchSink(
        "default",
        null,
        10,
        1000,
        Lists.newArrayList("localhost:" + getPort()),
        null,
        0,0,0,0,
        0,
        null,
        false,
        jsonMapper,
        null
    );
    sink.open();

    DateTime dt = new DateTime("2014-10-12T12:12:12.000Z");

    Map<String, Object> msg = new ImmutableMap.Builder<String, Object>()
        .put("f1", "v1")
        .put("f2", "v2")
        .put("f3", "v3")
        .put("ts", dt.getMillis())
        .build();
    String routingKey = "topicrecover";
    String index = "topicrecover";
    List<Message> msgList = new ArrayList<>();
    int msgCount = 100;
    for (int i = 0; i < msgCount; ++i) {
        msgList.add(new Message(routingKey, jsonMapper.writeValueAsBytes(msg)));
    }

    for (Message m : msgList) {
        sink.recover(m);
    }

    refresh();
    CountResponse countResponse = client().count(new CountRequest(index)).actionGet();
    assertEquals(countResponse.getCount(), 100);
}
 
Example #10
Source File: CountRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
public CountRequestBuilder(Client client, JsonToString<JsonInput> jsonToString, StringToJson<JsonOutput> stringToJson) {
    super(client, new CountRequest(), jsonToString, stringToJson);
}
 
Example #11
Source File: CountRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
@Override
protected ActionFuture<CountResponse> doExecute(CountRequest request) {
    return client.count(request);
}
 
Example #12
Source File: AbstractIMAPRiverUnitTest.java    From elasticsearch-imap with Apache License 2.0 3 votes vote down vote up
protected long getCount(final String index, final String type) {
    logger.debug("getCount()");

    esSetup.client().admin().indices().refresh(new RefreshRequest()).actionGet();

    final CountResponse count = esSetup.client().count(new CountRequest(index).types(type)).actionGet();

    return count.getCount();
}
 
Example #13
Source File: Client.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * A count of all the documents matching a specific query.
 *
 * @param request The count request
 * @return The result future
 * @see Requests#countRequest(String...)
 * @deprecated use {@link #search(SearchRequest)} instead and set size to 0
 */
@Deprecated
ActionFuture<CountResponse> count(CountRequest request);
 
Example #14
Source File: Client.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * A count of all the documents matching a specific query.
 *
 * @param request  The count request
 * @param listener A listener to be notified of the result
 * @see Requests#countRequest(String...)
 * @deprecated use {@link #search(SearchRequest, ActionListener)} instead and set size to 0
 */
@Deprecated
void count(CountRequest request, ActionListener<CountResponse> listener);
 
Example #15
Source File: Requests.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a count request which counts the hits matched against a query. Note, the query itself must be set
 * either using the JSON source of the query, or using a {@link org.elasticsearch.index.query.QueryBuilder} (using {@link org.elasticsearch.index.query.QueryBuilders}).
 *
 * @param indices The indices to count matched documents against a query. Use <tt>null</tt> or <tt>_all</tt> to execute against all indices
 * @return The count request
 * @see org.elasticsearch.client.Client#count(org.elasticsearch.action.count.CountRequest)
 * @deprecated use {@link #searchRequest(String...)} instead and set size to 0
 */
@Deprecated
public static CountRequest countRequest(String... indices) {
    return new CountRequest(indices);
}