/** * Count the number of documents matching the given {@link Query}. * * @param entityType must not be {@literal null}. * @return a {@link Mono} emitting the nr of matching documents. */ default Mono<Long> count(Class<?> entityType) { return count(new StringQuery(QueryBuilders.matchAllQuery().toString()), entityType, null); }
@Test public void testMultipleShardSearch() throws Exception { Random random = new Random(); int docs = 3 + random.nextInt(10001 - 3);; int batchSize = 1 + random.nextInt(5000 - 1); int shards = 1 + random.nextInt(5 - 1); long totalHitsReceived = 0; elastic.populate(shards, 0, schema, table, docs, ElasticsearchCluster.PRIMITIVE_TYPES); assertThat(elastic.search(schema, table).count, equalTo((long) docs)); ClusterState state = state(schema); IndexRoutingTable irt = routing(state, schema); ShardsIterator iter = irt.randomAllActiveShardsIt(); DiscoveryNodes nodes = state.nodes(); assertThat(iter.size(), equalTo(shards)); ShardRouting routing; while ((routing = iter.nextOrNull()) != null) { DiscoveryNode dn = nodes.get(routing.currentNodeId()); logger.info("--> executing search on: [{}] batch size: [{}]", routing, batchSize); Search<byte[]> search = new SearchBytes() .setQuery(String.format("{ \"query\": %s } ", QueryBuilders.matchAllQuery().toString())) .setResource(schema + "/" + table) .setParameter("scroll", "10s") .setParameter("preference", "_shards:" + routing.getId()) .setParameter("size", Integer.toString(batchSize)); byte[] result = connection.execute(search); totalHitsReceived += ElasticsearchCluster.asJsonObject(result).get("hits").getAsJsonObject().get("total").getAsInt(); } logger.info("--> total hits received across [{}] shards: [{}]", iter.size(), totalHitsReceived); assertThat(totalHitsReceived, equalTo((long) docs)); }
@Test public void testSingleShardSearch() throws Exception { Random random = new Random(); int docs = 999 + random.nextInt(10001 - 999); int batchSize = 1 + random.nextInt(5000 - 1); long totalHitsReceived = 0; elastic.populate(1, 0, schema, table, docs, ElasticsearchCluster.PRIMITIVE_TYPES); assertThat(elastic.search(schema, table).count, equalTo((long) docs)); ClusterState state = state(schema); IndexRoutingTable irt = routing(state, schema); ShardsIterator iter = irt.randomAllActiveShardsIt(); DiscoveryNodes nodes = state.nodes(); assertThat(iter.size(), equalTo(1)); ShardRouting routing = iter.nextOrNull(); assertNotNull(routing); assertNull(iter.nextOrNull()); DiscoveryNode dn = nodes.get(routing.currentNodeId()); logger.info("--> executing search on: [{}] batch size: [{}]", routing, batchSize); Search<byte[]> search = new SearchBytes() .setQuery(String.format("{ \"query\": %s } ", QueryBuilders.matchAllQuery().toString())) .setResource(schema + "/" + table) .setParameter("scroll", "10s") .setParameter("size", Integer.toString(batchSize)); byte[] result = connection.execute(search); totalHitsReceived = ElasticsearchCluster.asJsonObject(result).get(ElasticsearchConstants.HITS).getAsJsonObject().get(ElasticsearchConstants.TOTAL_HITS).getAsInt(); assertThat(totalHitsReceived, equalTo((long) docs)); }