Java Code Examples for org.elasticsearch.client.Client#prepareMultiSearch()

The following examples show how to use org.elasticsearch.client.Client#prepareMultiSearch() . 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: DistributedTableMetadataManager.java    From foxtrot with Apache License 2.0 4 votes vote down vote up
private Map<String, EstimationData> estimateFirstPhaseData(String table, String index, Client client,
                                                           Map<String, FieldMetadata> fields) {
    Map<String, EstimationData> estimationDataMap = Maps.newHashMap();
    int subListSize;
    if(cardinalityConfig == null || cardinalityConfig.getSubListSize() == 0) {
        subListSize = ElasticsearchUtils.DEFAULT_SUB_LIST_SIZE;
    } else {
        subListSize = cardinalityConfig.getSubListSize();
    }

    List<Map<String, FieldMetadata>> listOfMaps = fields.entrySet()
            .stream()
            .collect(mapSize(subListSize));

    for(Map<String, FieldMetadata> innerMap : listOfMaps) {
        MultiSearchRequestBuilder multiQuery = client.prepareMultiSearch();
        innerMap.values()
                .forEach(fieldMetadata -> {
                    String field = fieldMetadata.getField();
                    SearchRequestBuilder query = client.prepareSearch(index)
                            .setIndicesOptions(Utils.indicesOptions())
                            .setQuery(QueryBuilders.existsQuery(field))
                            .setSize(0);
                    switch (fieldMetadata.getType()) {
                        case STRING:
                            evaluateStringAggregation(table, field, fieldMetadata.getType(), query);
                            break;
                        case INTEGER:
                        case LONG:
                        case FLOAT:
                        case DOUBLE:
                            evaluateDoubleAggregation(table, field, fieldMetadata.getType(), query);
                            break;
                        case BOOLEAN:
                        case DATE:
                        case OBJECT:
                        case KEYWORD:
                        case TEXT:
                    }
                    multiQuery.add(query);
                });
        Stopwatch stopwatch = Stopwatch.createStarted();
        MultiSearchResponse multiResponse;
        try {
            multiResponse = multiQuery.execute()
                    .actionGet();
        } finally {
            logger.info("Cardinality query on table {} for {} fields took {} ms", table, fields.size(),
                        stopwatch.elapsed(TimeUnit.MILLISECONDS)
                       );
        }
        handleFirstPhaseMultiSearchResponse(multiResponse, table, fields, estimationDataMap);
    }
    return estimationDataMap;
}
 
Example 2
Source File: DistributedTableMetadataManager.java    From foxtrot with Apache License 2.0 4 votes vote down vote up
private Map<String, EstimationData> estimateSecondPhaseData(String table, String index, Client client,
                                                            Map<String, EstimationData> estimationData) {
    long maxDocuments = estimationData.values()
            .stream()
            .map(EstimationData::getCount)
            .max(Comparator.naturalOrder())
            .orElse(0L);
    if(maxDocuments == 0) {
        return estimationData;
    }

    MultiSearchRequestBuilder multiQuery = client.prepareMultiSearch();
    estimationData.forEach((key, value) -> value.accept(new EstimationDataVisitor<Void>() {
        @Override
        public Void visit(FixedEstimationData fixedEstimationData) {
            return null;
        }

        @Override
        public Void visit(PercentileEstimationData percentileEstimationData) {
            return null;
        }

        @Override
        public Void visit(CardinalityEstimationData cardinalityEstimationData) {
            if(cardinalityEstimationData.getCount() > 0 && cardinalityEstimationData.getCardinality() > 0) {
                int countToCardinalityRatio = (int)(cardinalityEstimationData.getCount() / cardinalityEstimationData.getCardinality());
                int documentToCountRatio = (int)(maxDocuments / cardinalityEstimationData.getCount());
                if(cardinalityEstimationData.getCardinality() <= 100 || (countToCardinalityRatio > 100 && documentToCountRatio < 100 &&
                                                                         cardinalityEstimationData.getCardinality() <= 5000)) {
                    logger.info("field:{} maxCount:{} countToCardinalityRatio:{} documentToCountRatio:{}", key, maxDocuments,
                                countToCardinalityRatio, documentToCountRatio
                               );
                    SearchRequestBuilder query = client.prepareSearch(index)
                            .setIndicesOptions(Utils.indicesOptions())
                            .setQuery(QueryBuilders.existsQuery(key))
                            .addAggregation(AggregationBuilders.terms(key)
                                                    .field(key)
                                                    .size(ElasticsearchQueryUtils.QUERY_SIZE))
                            .setSize(0);
                    multiQuery.add(query);
                }
            }
            return null;
        }

        @Override
        public Void visit(TermHistogramEstimationData termHistogramEstimationData) {
            return null;
        }
    }));


    Map<String, EstimationData> estimationDataMap = Maps.newHashMap(estimationData);
    MultiSearchResponse multiResponse = multiQuery.execute()
            .actionGet();
    handleSecondPhaseMultiSearchResponse(multiResponse, table, estimationDataMap);
    return estimationDataMap;
}