Java org.elasticsearch.action.count.CountResponse 代码实例

・13 分钟阅读

以下是展示如何使用org.elasticsearch.action.count.CountResponse的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


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();
}
 

实例 2


@Override
public long countEntities( Class<?> resultType,
                           Specification<Composite> whereClause,
                           Map<String, Object> variables )
    throws EntityFinderException
{
    // Prepare request
    CountRequestBuilder request = support.client().prepareCount( support.index() );
    AndFilterBuilder filterBuilder = baseFilters( resultType );
    QueryBuilder queryBuilder = processWhereSpecification( filterBuilder, whereClause, variables );
    request.setQuery( filteredQuery( queryBuilder, filterBuilder ) );
    // Log
    LOGGER.debug( "Will count Entities: {}", request );
    // Execute
    CountResponse count = request.execute().actionGet();
    return count.getCount();
}
 

实例 3


@Override
public IndexStat getIndexStat() {
  CountRequestBuilder countRequest = client.prepareCount(this.getIndexName())
    .setTypes(this.getIndexType())
    .setQuery(QueryBuilders.matchAllQuery());
  CountResponse response = countRequest.get();
  return new IndexStat(getLastSynchronization(), response.getCount());
}
 

实例 4


@Override
public Long count() {
    CountResponse response = this.elastichSearchClient
            .prepareCount(getIndexName())
            .setTypes(getIndexType())
            .execute().actionGet();
    return response.getCount();
}
 

实例 5


@Override
public int getPendingDocuments() {
    try {
        CountResponse response = client.prepareCount(indexName)
                .setQuery(filteredQuery(matchAllQuery(), missingFilter(SearchService.FIELD_CONTENTS)))
                .execute()
                .actionGet();
        return (int) response.getCount();
    } catch (Exception e) {
        log.error("problem getting pending docs: " + e.getMessage());
    }
    return 0;
}
 

实例 6


private static long countLocal(String index) {
    try {
        CountResponse response = elasticsearch_client.prepareCount(index)
            .setQuery(QueryBuilders.matchAllQuery())
            .execute()
            .actionGet();
        return response.getCount();
    } catch (Throwable e) {
        e.printStackTrace();
        return 0;
    }
}
 

实例 7


@Override
protected Number executeCountQuery(Table table, List<FilterItem> whereItems, boolean functionApproximationAllowed) {
    if (!whereItems.isEmpty()) {
        // not supported - will have to be done by counting client-side
        return null;
    }
    final String documentType = table.getName();
    final CountResponse response = elasticSearchClient.prepareCount(indexName)
            .setQuery(QueryBuilders.termQuery("_type", documentType)).execute().actionGet();
    return response.getCount();
}
 

实例 8


protected List<Map<String, Object>> getDocumentListByQuery(final FessEsClient fessEsClient, final QueryBuilder queryBuilder,
        final String[] fields) {
    final FieldHelper fieldHelper = ComponentUtil.getFieldHelper();
    final CountResponse countResponse =
            fessEsClient.prepareCount(fieldHelper.docIndex).setTypes(fieldHelper.docType).setQuery(queryBuilder).execute().actionGet();
    final long numFound = countResponse.getCount();
    // TODO max threshold
    return fessEsClient.getDocumentList(fieldHelper.docIndex, fieldHelper.docType, requestBuilder -> {
        requestBuilder.setQuery(queryBuilder).setSize((int) numFound);
        if (fields != null) {
            requestBuilder.addFields(fields);
        }
        return true;
    });
}
 

实例 9


public static void renderIndex(SitemapRender render) throws UnsupportedEncodingException {
        Client client = ESNodeHodel.getClient();
        CountResponse countResponse = client.prepareCount("gazetteer")
                        .setTypes(IndexHolder.LOCATION)
                        .setQuery(QueryBuilders.termsQuery("type", config.listSiteMapTypes())).get();
        long count = countResponse.getCount();
        render.indexBegin();
        for(int i = 0; i <= count / pageSize; i++) {
                render.page(i);
        }
        render.indexEnd();
}
 

实例 10


public long getDocumentCount() throws Exception {
    try (Client client = getClient()) {
        client.admin().indices().refresh(new RefreshRequest(INDEX_NAME)).actionGet();
        ActionFuture<CountResponse> response = client.count(new CountRequest(INDEX_NAME).types(DOCUMENT_TYPE));
        CountResponse countResponse = response.get();
        return countResponse.getCount();
    }
}
 

实例 11


public long countEntities( Class<?> resultType,
                           BooleanExpression whereClause )
        throws EntityFinderException
{
    // Prepare request
    CountRequestBuilder request = support.client().prepareCount( support.index() );
    AndFilterBuilder filterBuilder = baseFilters( resultType );
    //QueryBuilder queryBuilder = processWhereSpecification( filterBuilder, whereClause );
    QueryBuilder queryBuilder = new ElasticSearchQueryParserImpl().getQueryBuilder( filterBuilder, whereClause );
    request.setQuery( filteredQuery( queryBuilder, filterBuilder ) );
    // Log
    LOGGER.debug( "Will count Entities: {}", request );
    // Execute
    CountResponse count = request.execute().actionGet();
    return count.getCount();
}
 

实例 12


private void run(final CommandLineOptions commandLineOptions, final String[] args) {
    startEsNode();
    final Client client = node.client();
    final SearchRequestBuilder srb = client.prepareSearch();
    final CountResponse countResponse = client.prepareCount(commandLineOptions.getIndicesArray()).execute().actionGet();
    final long totalCount = countResponse.getCount();
    long changedCount = 0;
    long processedCount = 0;
    srb.setIndices(commandLineOptions.getIndicesArray());
    srb.setSearchType(SearchType.SCAN);
    srb.setScroll(TimeValue.timeValueMinutes(commandLineOptions.getScrollTimeout()));
    srb.setQuery(QueryBuilders.matchAllQuery());
    srb.setSize(commandLineOptions.getBatchSize());
    srb.addField("_id");
    srb.addField("timestamp");
    srb.addField("_source");
    final SearchRequest request = srb.request();
    final SearchResponse response = client.search(request).actionGet();
    if (! commandLineOptions.isFix()) {
        LOG.warn("Not executing update because '-F' command line flag not given!");
    }
    while (true) {
        final SearchResponse r = client.prepareSearchScroll(response.getScrollId()).setScroll(TimeValue.timeValueMinutes(1)).execute().actionGet();
        if (r.getHits().getHits().length == 0) {
            LOG.debug("No more hits, done!");
            break;
        }
        final BulkRequestBuilder bulk = client.prepareBulk();
        for (SearchHit hit : r.getHits()) {
            processedCount++;
            try {
                if (handleHit(hit, bulk)) {
                    changedCount++;
                }
            } catch (Exception e) {
                LOG.error("Error handling document " + hit.getId(), e);
            }
        }
        processBulk(bulk, commandLineOptions.isFix());
        LOG.info("Changed {} of total {} documents ({}% checked)", changedCount, totalCount, String.format(Locale.ENGLISH, "%.2f", ((double) processedCount / totalCount) * 100));
    }
    stopEsNode();
}
 

实例 13


public Map<String, Map<String, TfIdf>> getTfIdfVectors(String documentId, String... fields)
{
    //TODO: if field is not set or with wrong mapping
    TermVectorResponse termVector = client.prepareTermVector(index, type, documentId)
            .setTermStatistics(true)
            .setFieldStatistics(false)
            .setSelectedFields(fields)
            .setPositions(false)
            .setOffsets(false)
            .setPayloads(false)
            .execute()
            .actionGet();
    CountResponse numDocuments = client.prepareCount(index, type)
            .execute()
            .actionGet();
    Map<String, Map<String, TfIdf>> req = new HashMap<>();
    try 
    {
        Fields _fields = termVector.getFields();
        for(String field: _fields)
        {
            Terms terms = _fields.terms(field);
            TermsEnum termsEnum = terms.iterator(TermsEnum.EMPTY);
            long effectiveDocumentLength = 0;   //sum of term frequencies
            Map<String, TfIdf> tfIdf_vector = new HashMap<>();
            while (termsEnum.next() != null)                  
            {                                   
                //term frequency in the field
                int termFreq = termsEnum.docs(null, null, DocsEnum.FLAG_FREQS).freq();  //lucene version 4
                //int termFreq = termsEnum.postings(null, null, PostingsEnum.FREQS).freq(); //lucene version 5
                effectiveDocumentLength += termFreq;
                TfIdf ftIdf = new TfIdf();
                ftIdf.setTermFrequency(termFreq);
                ftIdf.setDocumentFrequency(termsEnum.docFreq());
                tfIdf_vector.put(termsEnum.term().utf8ToString(), ftIdf);
            }
            long numberOfDocuments = numDocuments.getCount();
            for(Map.Entry<String, TfIdf> item: tfIdf_vector.entrySet())
            {
                TfIdf tfIdf = item.getValue();
                tfIdf.setNumberOfDocuments(numberOfDocuments);
                tfIdf.setDocumentLength(effectiveDocumentLength);
            }
            req.put(field, tfIdf_vector);
        }
    } 
    catch (IOException ex) 
    {
        LOGGER.warn("Can not create TF-IDF vectors because: {}", ex.getMessage());
    } 
    return req;
}
 
讨论
淘淘あ西西 profile image