SearchResponse searchResponse = esClient.prepareSearch(riverIndexName) .setQuery(QueryBuilders.queryString(MongoDBRiver.TYPE).defaultField("type")).setFrom(from).setSize(count).get(); long totalHits = searchResponse.getHits().totalHits(); logger.trace("totalHits: {}", totalHits); Map<String, Object> data = new HashMap<String, Object>();
@Override public Iterable<RawQuery.Result<String>> query(RawQuery query, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException { SearchRequestBuilder srb = client.prepareSearch(indexName); srb.setTypes(query.getStore()); srb.setQuery(QueryBuilders.queryStringQuery(query.getQuery())); srb.setFrom(query.getOffset()); if (query.hasLimit()) srb.setSize(query.getLimit()); else srb.setSize(maxResultsSize); srb.setNoFields(); //srb.setExplain(true); SearchResponse response = srb.execute().actionGet(); log.debug("Executed query [{}] in {} ms", query.getQuery(), response.getTookInMillis()); SearchHits hits = response.getHits(); if (!query.hasLimit() && hits.totalHits() >= maxResultsSize) log.warn("Query result set truncated to first [{}] elements for query: {}", maxResultsSize, query); List<RawQuery.Result<String>> result = new ArrayList<RawQuery.Result<String>>(hits.hits().length); for (SearchHit hit : hits) { result.add(new RawQuery.Result<String>(hit.id(),hit.getScore())); } return result; }
log.debug("Executed query [{}] in {} ms", query.getCondition(), response.getTookInMillis()); SearchHits hits = response.getHits(); if (!query.hasLimit() && hits.totalHits() >= maxResultsSize) log.warn("Query result set truncated to first [{}] elements for query: {}", maxResultsSize, query); List<String> result = new ArrayList<String>(hits.hits().length);
@Override public long totalHits() { return hits.totalHits(); } };
/** * The total number of hits that matches the search request. */ public long getTotalHits() { return totalHits(); }
@Override public Page<T> mapResults(SearchResponse response) { long totalHits = response.getHits().totalHits(); List<T> results = new ArrayList<T>(); for (SearchHit hit : response.getHits()) { if (hit != null) { results.add(mapResult(hit.sourceAsString(), elementType)); } } return new PageImpl<T>(results, pageable, totalHits); } };
@Override public List<Span> apply(SearchResponse response) { if (response.getHits().totalHits() == 0) { return null; } ImmutableList.Builder<Span> trace = ImmutableList.builder(); for (SearchHit hit : response.getHits()) { trace.add(Codec.JSON.readSpan(hit.getSourceRef().toBytes())); } return trace.build(); } });
@Override public EsServiceMapping getServiceMappingByName(String name) throws Exception { SearchResponse response = this.retrieveByField("name", name, EsServiceMapping.class); if (response.getHits().totalHits() > 0) { String str = response.getHits().getAt(0).getSourceAsString(); EsServiceMapping serviceMapping = mapper.readValue(str, EsServiceMapping.class); return serviceMapping; } return null; } }
@Override public EsAwsStatus getAwsStatus(String instanceId) throws Exception { SearchResponse response = this.retrieveByField("id", instanceId, EsAwsStatus.class); if (response.getHits().totalHits() > 0) { String str = response.getHits().getAt(0).getSourceAsString(); EsAwsStatus status = insertMapper.readValue(str, EsAwsStatus.class); return status; } return null; }
private void findNumHits() { SearchRequestBuilder request = client.prepareSearch(indexName); if (mappingName != null && mappingName.length() > 0) { request.setTypes(mappingName); } request.setSearchType(SearchType.COUNT); if (queryJSON != null && queryJSON.length() > 0) { request.setQuery(queryJSON); } SearchResponse response = request.execute().actionGet(); this.numHits = response.getHits().totalHits(); LOG.info("Ran query: "+String.valueOf(numHits)+" hits"); }
protected <E extends EsInstance> E getInstanceByIdForClass(String instanceId, Class objectClass) throws Exception { SearchResponse response = this.retrieveByField("id", instanceId, objectClass); if (response.getHits().totalHits() > 0) { String str = response.getHits().getAt(0).getSourceAsString(); E inst = (E) insertMapper.readValue(str, objectClass); inst.setVersion(response.getHits().getAt(0).getVersion()); return inst; } return null; }
private Table buildTable(RestRequest request, SearchResponse response) { Table table = getTableWithHeader(request); table.startRow(); table.addCell(response.getHits().totalHits()); table.endRow(); return table; } }
@Override public long countUnrestricted(final QueryBuilder query) { if(!validateQuery(query)) { return 0; } final String[] searchableIndexes = getSearchableIndexes(false); if (searchableIndexes.length == 0) { return 0; } SearchRequestBuilder count = client.get().prepareSearch(searchableIndexes).setQuery(query).setSize(0); SearchResponse response = count.execute().actionGet(); return response.getHits().totalHits(); }
public CountResponse(SearchResponse searchResponse) { super(searchResponse.getTotalShards(), searchResponse.getSuccessfulShards(), searchResponse.getFailedShards(), Arrays.asList(searchResponse.getShardFailures())); this.count = searchResponse.getHits().totalHits(); this.terminatedEarly = searchResponse.isTerminatedEarly() != null && searchResponse.isTerminatedEarly(); }
private Table buildTable(RestRequest request, SearchResponse response) { Table table = getTableWithHeader(request); long time = System.currentTimeMillis(); table.startRow(); table.addCell(TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS)); table.addCell(dateFormat.print(time)); table.addCell(response.getHits().totalHits()); table.endRow(); return table; } }
private void initiate_search() { SearchResponse response = client.prepareSearch(indexName) .setTypes(objType) .setSearchType(SearchType.COUNT) .setQuery(QueryBuilders.queryString(queryString)) .setSize(requestSize) .execute() .actionGet(); this.numHits = response.getHits().totalHits(); if(numSplits > numHits) numSplits = numHits; // This could be bad this.numSplitRecords = (numHits/numSplits); }
@Override protected void moveToSecondPhase() throws Exception { final InternalSearchResponse internalResponse = searchPhaseController.merge(SearchPhaseController.EMPTY_DOCS, firstResults, (AtomicArray<? extends FetchSearchResultProvider>) AtomicArray.empty(), request); String scrollId = null; if (request.scroll() != null) { scrollId = TransportSearchHelper.buildScrollId(request.searchType(), firstResults, ImmutableMap.of("total_hits", Long.toString (internalResponse.hits().totalHits()))); } listener.onResponse(new SearchResponse(internalResponse, scrollId, expectedSuccessfulOps, successfulOps.get(), buildTookInMillis(), buildShardFailures())); } }
public static void testDocumentsCount( String index, String type, long count ) throws Exception { SearchRequest request = new SearchRequestBuilder( client, SearchAction.INSTANCE ) .setIndices( index ) .setTypes( type) .request(); SearchResponse response = client.search( request ).actionGet(); long c = response.getHits().totalHits(); assert count == c; }
@Test public void eventShouldDoNothingWhenNoQuotaEvent() throws Exception { quotaMailboxListener.event(EVENT); embeddedElasticSearch.awaitForElasticSearch(); SearchResponse searchResponse = client.prepareSearch(QuotaRatioElasticSearchConstants.DEFAULT_QUOTA_RATIO_READ_ALIAS.getValue()) .setTypes(QuotaRatioElasticSearchConstants.QUOTA_RATIO_TYPE.getValue()) .setQuery(matchAllQuery()) .execute() .get(); assertThat(searchResponse.getHits().totalHits()).isEqualTo(0); }
@Test public void eventShouldIndexEventWhenQuotaEvent() throws Exception { quotaMailboxListener.event(new QuotaUsageUpdatedEvent(BOB_SESSION, QUOTAROOT, Counts._52_PERCENT, Sizes._55_PERCENT, NOW)); embeddedElasticSearch.awaitForElasticSearch(); SearchResponse searchResponse = client.prepareSearch(QuotaRatioElasticSearchConstants.DEFAULT_QUOTA_RATIO_READ_ALIAS.getValue()) .setTypes(QuotaRatioElasticSearchConstants.QUOTA_RATIO_TYPE.getValue()) .setQuery(matchAllQuery()) .execute() .get(); assertThat(searchResponse.getHits().totalHits()).isEqualTo(1); } }