Java Code Examples for org.elasticsearch.action.count.CountResponse#getCount()

The following examples show how to use org.elasticsearch.action.count.CountResponse#getCount() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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();
}