org.elasticsearch.action.count.CountResponse Java Examples

The following examples show how to use org.elasticsearch.action.count.CountResponse. 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: EsFileTest.java    From test-data-generator with Apache License 2.0 6 votes vote down vote up
protected void testEsContent(Client client) throws SQLException {
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    CountResponse countResponse = client.prepareCount(indexName).setQuery(QueryBuilders.termQuery("_type", "user"))
            .execute().actionGet();
    Assert.assertEquals(11, countResponse.getCount());

    countResponse = client.prepareCount(indexName).setQuery(QueryBuilders.termQuery("_type", "training"))
            .execute().actionGet();
    Assert.assertEquals(55, countResponse.getCount());

    countResponse = client.prepareCount(indexName).setQuery(QueryBuilders.termQuery("_type", "exercise"))
            .execute().actionGet();
    Assert.assertEquals(275, countResponse.getCount());
}
 
Example #2
Source File: EsDirectTest.java    From test-data-generator with Apache License 2.0 6 votes vote down vote up
protected void testEsContent(Client client) {
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    CountResponse countResponse = client.prepareCount(indexName).setQuery(QueryBuilders.termQuery("_type", "user"))
            .execute().actionGet();
    Assert.assertEquals(11, countResponse.getCount());

    countResponse = client.prepareCount(indexName).setQuery(QueryBuilders.termQuery("_type", "training"))
            .execute().actionGet();
    Assert.assertEquals(55, countResponse.getCount());

    countResponse = client.prepareCount(indexName).setQuery(QueryBuilders.termQuery("_type", "exercise"))
            .execute().actionGet();
    Assert.assertEquals(275, countResponse.getCount());
}
 
Example #3
Source File: ExtendTemplateTest.java    From test-data-generator with Apache License 2.0 6 votes vote down vote up
protected void testEsContent(Client client) {
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    CountResponse countResponse = client.prepareCount(indexName).setQuery(QueryBuilders.termQuery("_type", "user"))
            .execute().actionGet();
    Assert.assertEquals(11, countResponse.getCount());

    countResponse = client.prepareCount(indexName).setQuery(QueryBuilders.termQuery("_type", "training"))
            .execute().actionGet();
    Assert.assertEquals(55, countResponse.getCount());

    countResponse = client.prepareCount(indexName).setQuery(QueryBuilders.termQuery("_type", "exercise"))
            .execute().actionGet();
    Assert.assertEquals(275, countResponse.getCount());
}
 
Example #4
Source File: MergeSchemaTest.java    From test-data-generator with Apache License 2.0 6 votes vote down vote up
protected void testEsContent(Client client) {
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    CountResponse countResponse = client.prepareCount(indexName).setQuery(QueryBuilders.termQuery("_type", "user"))
            .execute().actionGet();
    Assert.assertEquals(11, countResponse.getCount());

    countResponse = client.prepareCount(indexName).setQuery(QueryBuilders.termQuery("_type", "training"))
            .execute().actionGet();
    Assert.assertEquals(55, countResponse.getCount());

    countResponse = client.prepareCount(indexName).setQuery(QueryBuilders.termQuery("_type", "exercise"))
            .execute().actionGet();
    Assert.assertEquals(275, countResponse.getCount());
}
 
Example #5
Source File: BaseElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public int getPendingDocuments() {
    try {
        CountResponse response = client.prepareCount(indexName)
                .setQuery(filteredQuery(matchAllQuery(), orFilter(
                        missingFilter(SearchService.FIELD_INDEXED),
                        termFilter(SearchService.FIELD_INDEXED, false))))
                .execute()
                .actionGet();
        return (int) response.getCount();
    } catch (Exception e) {
        getLog().error("Problem getting pending docs for index builder [" + getName() + "]", e);
    }
    return 0;
}
 
Example #6
Source File: Neo4jRiverIntTest.java    From elasticsearch-river-neo4j with Apache License 2.0 5 votes vote down vote up
/**
 * I use this test for a shell like experience, has to be cancelled manually.
 */
// @Test
public void infiniteTest() throws InterruptedException {
    Thread.sleep(200); // allow river to start
    while (true) {
        Thread.sleep(200); // time for poller to index
        refreshIndex();
        CountResponse
                resp =
                node.client().count(countRequest(index).types(type).source(queryString("mowbray").defaultField("manager").toString())).actionGet();
        logger.debug("How many moggas? {} !", resp.getCount());
    }
}
 
Example #7
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 #8
Source File: BaseElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public int getNDocs() {
    assureIndex();
    CountResponse response = client.prepareCount(indexName)
            .setQuery(filteredQuery(matchAllQuery(),termFilter(SearchService.FIELD_INDEXED, true)))
            .execute()
            .actionGet();
    return (int) response.getCount();
}
 
Example #9
Source File: BaseElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public int getPendingDocuments() {
    try {
        CountResponse response = client.prepareCount(indexName)
                .setQuery(filteredQuery(matchAllQuery(), orFilter(
                        missingFilter(SearchService.FIELD_INDEXED),
                        termFilter(SearchService.FIELD_INDEXED, false))))
                .execute()
                .actionGet();
        return (int) response.getCount();
    } catch (Exception e) {
        getLog().error("Problem getting pending docs for index builder [" + getName() + "]", e);
    }
    return 0;
}
 
Example #10
Source File: BaseElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public int getNDocs() {
    assureIndex();
    CountResponse response = client.prepareCount(indexName)
            .setQuery(filteredQuery(matchAllQuery(),termFilter(SearchService.FIELD_INDEXED, true)))
            .execute()
            .actionGet();
    return (int) response.getCount();
}
 
Example #11
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 #12
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 #13
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 #14
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 #15
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 #16
Source File: ESJavaRDDFT.java    From deep-spark with Apache License 2.0 4 votes vote down vote up
/**
 * Imports dataset
 *
 * @throws java.io.IOException
 */
private static void dataSetImport()
        throws IOException, ExecutionException, IOException, InterruptedException, ParseException {

    JSONParser parser = new JSONParser();
    URL url = Resources.getResource(DATA_SET_NAME);
    Object obj = parser.parse(new FileReader(url.getFile()));

    JSONObject jsonObject = (JSONObject) obj;

    IndexResponse responseBook = client.prepareIndex(ES_INDEX_BOOK, ES_TYPE_INPUT, "id")
            .setSource(jsonObject.toJSONString())
            .execute()
            .actionGet();

    String json2 = "{" +

            "\"message\":\"" + MESSAGE_TEST + "\"" +
            "}";

    IndexResponse response2 = client.prepareIndex(ES_INDEX_MESSAGE, ES_TYPE_MESSAGE).setCreate(true)
            .setSource(json2).setReplicationType(ReplicationType.ASYNC)
            .execute()
            .actionGet();

    String json = "{" +
            "\"user\":\"kimchy\"," +
            "\"postDate\":\"2013-01-30\"," +
            "\"message\":\"trying out Elasticsearch\"" +
            "}";

    IndexResponse response = client.prepareIndex(ES_INDEX, ES_TYPE).setCreate(true)
            .setSource(json)
            .execute()
            .actionGet();

    String index = response.getIndex();
    String _type = response.getType();
    String _id = response.getId();
    try {
        CountResponse countResponse = client.prepareCount(ES_INDEX).setTypes(ES_TYPE)
                .execute()
                .actionGet();

        SearchResponse searchResponse = client.prepareSearch(ES_INDEX_BOOK).setTypes(ES_TYPE_INPUT)
                .execute()
                .actionGet();

        //searchResponse.getHits().hits();
        //assertEquals(searchResponse.getCount(), 1);
    } catch (AssertionError | Exception e) {
        cleanup();
        e.printStackTrace();
    }
}
 
Example #17
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 #18
Source File: Neo4jRiverIntTest.java    From elasticsearch-river-neo4j with Apache License 2.0 4 votes vote down vote up
@Test
  public void testAddAndRemoveNodes() throws InterruptedException {

      Thread.sleep(200); // allow river to start
      String name = UUID.randomUUID().toString();

      // add node to neo4j
      HashMap<String, Object> map = new HashMap<String, Object>();
      map.put("name", name);
      Transaction tx = db.beginTx();
      ArrayList<String> labels = new ArrayList<String>();
org.neo4j.graphdb.Node n = db.createNode(map, labels);
      tx.success();

      CountResponse resp = null;

      int k = 0;
      while (k++ < 100) {

          Thread.sleep(1000); // time for poller to index
          refreshIndex();

          logger.debug("Count request [index={}, type={}, name={}]", new Object[]{index, type, name});
          resp = node.client().count(countRequest(index).types(type).source(queryString(name).defaultField("name").toString())).actionGet();
          if (1 == resp.getCount())
              break;

      }
      assertEquals(1, resp.getCount());

      db.remove(n);

      k = 0;
      while (k++ < 100) {

          Thread.sleep(1000); // time for poller to index
          refreshIndex();

          logger.debug("Count request [index={}, type={}, name={}]", new Object[]{index, type, name});
          resp = node.client().count(countRequest(index).types(type).source(queryString(name).defaultField("name").toString())).actionGet();
          if (0 == resp.getCount())
              break;

      }

      assertEquals(0, resp.getCount());

      shutdown();
  }
 
Example #19
Source File: IndexService.java    From disthene-reader with MIT License 4 votes vote down vote up
public String getPathsWithStats(String tenant, String wildcard) throws TooMuchDataExpectedException {
    String regEx = WildcardUtil.getPathsRegExFromWildcard(wildcard);

    SearchResponse response = client.prepareSearch(indexConfiguration.getIndex())
            .setScroll(new TimeValue(indexConfiguration.getTimeout()))
            .setSize(indexConfiguration.getScroll())
            .setQuery(QueryBuilders.filteredQuery(
                    QueryBuilders.regexpQuery("path", regEx),
                    FilterBuilders.termFilter("tenant", tenant)))
            .addField("path")
            .execute().actionGet();

    // if total hits exceeds maximum - abort right away returning empty array
    if (response.getHits().totalHits() > indexConfiguration.getMaxPaths()) {
        logger.debug("Total number of paths exceeds the limit: " + response.getHits().totalHits());
        throw new TooMuchDataExpectedException("Total number of paths exceeds the limit: " + response.getHits().totalHits() + " (the limit is " + indexConfiguration.getMaxPaths() + ")");
    }

    List<String> paths = new ArrayList<>();
    while (response.getHits().getHits().length > 0) {
        for (SearchHit hit : response.getHits()) {
            paths.add(String.valueOf(hit.field("path").getValue()));
        }
        response = client.prepareSearchScroll(response.getScrollId())
                .setScroll(new TimeValue(indexConfiguration.getTimeout()))
                .execute().actionGet();
    }

    Collections.sort(paths);

    // we got the paths. Now let's get the counts
    List<String> result = new ArrayList<>();
    for (String path : paths) {
        CountResponse countResponse = client.prepareCount(indexConfiguration.getIndex())
                .setQuery(QueryBuilders.filteredQuery(
                        QueryBuilders.regexpQuery("path", path + "\\..*"),
                        FilterBuilders.boolFilter()
                            .must(FilterBuilders.termFilter("tenant", tenant))
                            .must(FilterBuilders.termFilter("leaf", true))))
                .execute().actionGet();
        long count = countResponse.getCount();
        result.add("{\"path\": \"" + path + "\",\"count\":" + countResponse.getCount() + "}");
    }


    return "[" + joiner.join(result) + "]";
}
 
Example #20
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 #21
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 #22
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);