org.elasticsearch.action.search.MultiSearchRequestBuilder Java Examples

The following examples show how to use org.elasticsearch.action.search.MultiSearchRequestBuilder. 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: TransportListStoresAction.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
@Override
protected void masterOperation(ListStoresActionRequest request, ClusterState state,
                               ActionListener<ListStoresActionResponse> listener) throws Exception {
    String[] names = indexNameExpressionResolver.concreteIndexNames(state,
            new ClusterStateRequest().indices(IndexFeatureStore.DEFAULT_STORE, IndexFeatureStore.STORE_PREFIX + "*"));
    final MultiSearchRequestBuilder req = client.prepareMultiSearch();
    final List<Tuple<String, Integer>> versions = new ArrayList<>();
    Stream.of(names)
            .filter(IndexFeatureStore::isIndexStore)
            .map((s) -> clusterService.state().metaData().getIndices().get(s))
            .filter(Objects::nonNull)
            .filter((im) -> STORE_VERSION_PROP.exists(im.getSettings()))
            .forEach((m) -> {
                req.add(countSearchRequest(m));
                versions.add(tuple(m.getIndex().getName(),STORE_VERSION_PROP.get(m.getSettings())));
            });
    if (versions.isEmpty()) {
        listener.onResponse(new ListStoresActionResponse(Collections.emptyList()));
    } else {
        req.execute(wrap((r) -> listener.onResponse(toResponse(r, versions)), listener::onFailure));
    }
}
 
Example #2
Source File: MultiQueryAction.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
@Override
public MultiSearchRequestBuilder getRequestBuilder(MultiQueryRequest parameter) {

    MultiSearchRequestBuilder multiSearchRequestBuilder = getConnection().getClient()
            .prepareMultiSearch();

    for(Map.Entry<String, ActionRequest> entry : parameter.getRequests()
            .entrySet()) {
        ActionRequest request = entry.getValue();
        Action<ActionRequest> action = analyticsLoader.getAction(request);
        if(null == action) {
            throw FoxtrotExceptions.queryCreationException(request, null);
        }
        ActionRequestBuilder requestBuilder = action.getRequestBuilder(request);
        if(requestBuilder instanceof SearchRequestBuilder) {
            multiSearchRequestBuilder.add((SearchRequestBuilder)requestBuilder);
        }
    }
    return multiSearchRequestBuilder;
}
 
Example #3
Source File: MultiQueryAction.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
@Override
public ActionResponse execute(MultiQueryRequest parameter) {
    MultiSearchRequestBuilder multiSearchRequestBuilder = getRequestBuilder(parameter);
    try {
        LOGGER.info("Search: {}", multiSearchRequestBuilder);
        MultiSearchResponse multiSearchResponse = multiSearchRequestBuilder.execute()
                .actionGet();
        return getResponse(multiSearchResponse, parameter);
    } catch (ElasticsearchException e) {
        throw FoxtrotExceptions.createQueryExecutionException(parameter, e);
    }
}
 
Example #4
Source File: ClientWithStats.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public MultiSearchRequestBuilder prepareMultiSearch() {
	return wrapped.prepareMultiSearch();
}
 
Example #5
Source File: DefaultElasticSearchService.java    From vertx-elasticsearch-service with Apache License 2.0 4 votes vote down vote up
@Override
public void multiSearch(final List<MultiSearchQueryOptions> multiSearchQueryOptions,
                        final MultiSearchOptions options,
                        final Handler<AsyncResult<com.hubrick.vertx.elasticsearch.model.MultiSearchResponse>> resultHandler) {

    final MultiSearchRequestBuilder builder = client.prepareMultiSearch();

    if (options != null) {
        if (options.getMaxConcurrentSearchRequests() != null) {
            builder.setMaxConcurrentSearchRequests(options.getMaxConcurrentSearchRequests());
        }

        if (options.getIndicesOptions() != null) {
            final IndicesOptions defaultIndicesOptions = IndicesOptions.strictExpandOpenAndForbidClosed();
            builder.setIndicesOptions(
                    IndicesOptions.fromOptions(
                            Optional.ofNullable(options.getIndicesOptions().getIgnoreUnavailable()).orElse(defaultIndicesOptions.ignoreUnavailable()),
                            Optional.ofNullable(options.getIndicesOptions().getAllowNoIndices()).orElse(defaultIndicesOptions.allowNoIndices()),
                            Optional.ofNullable(options.getIndicesOptions().getExpandToOpenIndices()).orElse(defaultIndicesOptions.expandWildcardsOpen()),
                            Optional.ofNullable(options.getIndicesOptions().getExpandToClosedIndices()).orElse(defaultIndicesOptions.expandWildcardsClosed()),
                            Optional.ofNullable(options.getIndicesOptions().getAllowAliasesToMultipleIndices()).orElse(defaultIndicesOptions.allowAliasesToMultipleIndices()),
                            Optional.ofNullable(options.getIndicesOptions().getForbidClosedIndices()).orElse(defaultIndicesOptions.forbidClosedIndices()),
                            Optional.ofNullable(options.getIndicesOptions().getIgnoreAliases()).orElse(defaultIndicesOptions.ignoreAliases())
                    )
            );
        }
    }

    for (MultiSearchQueryOptions multiSearchQueryOptionsItem : multiSearchQueryOptions) {
        final SearchRequestBuilder searchRequestBuilder = client.prepareSearch(multiSearchQueryOptionsItem.getIndices().toArray(new String[0]));
        populateSearchRequestBuilder(searchRequestBuilder, multiSearchQueryOptionsItem.getSearchOptions());
        builder.add(searchRequestBuilder);
    }


    builder.execute(new ActionListener<MultiSearchResponse>() {
        @Override
        public void onResponse(final MultiSearchResponse multiSearchResponse) {
            resultHandler.handle(Future.succeededFuture(mapToMultiSearchResponse(multiSearchResponse)));
        }

        @Override
        public void onFailure(final Exception e) {
            handleFailure(resultHandler, e);
        }
    });
}
 
Example #6
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 #7
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;
}
 
Example #8
Source File: FessEsClient.java    From fess with Apache License 2.0 4 votes vote down vote up
@Override
public MultiSearchRequestBuilder prepareMultiSearch() {
    return client.prepareMultiSearch();
}