org.elasticsearch.action.search.MultiSearchResponse Java Examples

The following examples show how to use org.elasticsearch.action.search.MultiSearchResponse. 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: AbstractEs6_4ClientInstrumentationTest.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
protected MultiSearchResponse doMultiSearch(MultiSearchRequest searchRequest) throws IOException, ExecutionException, InterruptedException {
    if (async) {
        ClientMethod<MultiSearchRequest, MultiSearchResponse> method =
            (request, options, listener) -> client.msearchAsync(request, options, listener);
        return invokeAsync(searchRequest, method);
    }
    return client.msearch(searchRequest, RequestOptions.DEFAULT);
}
 
Example #2
Source File: DistributedTableMetadataManager.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
private void handleSecondPhaseMultiSearchResponse(MultiSearchResponse multiResponse, String table,
                                                  Map<String, EstimationData> estimationDataMap) {
    for(MultiSearchResponse.Item item : multiResponse.getResponses()) {
        SearchResponse response = validateAndGetSearchResponse(item, table);
        if(null == response) {
            continue;
        }
        final long hits = response.getHits()
                .getTotalHits();
        Map<String, Aggregation> output = response.getAggregations().asMap();
        output.forEach((key, value) -> {
            Terms terms = (Terms)output.get(key);
            estimationDataMap.put(key, TermHistogramEstimationData.builder()
                    .count(hits)
                    .termCounts(terms.getBuckets()
                            .stream()
                            .collect(Collectors.toMap(Terms.Bucket::getKeyAsString, Terms.Bucket::getDocCount)))
                    .build());
        });
    }
}
 
Example #3
Source File: TransportListStoresAction.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
private ListStoresActionResponse toResponse(MultiSearchResponse response, List<Tuple<String, Integer>> versions) {
    assert versions.size() == response.getResponses().length;
    Iterator<Tuple<String, Integer>> vs = versions.iterator();
    Iterator<MultiSearchResponse.Item> rs = response.iterator();
    List<ListStoresAction.IndexStoreInfo> infos = new ArrayList<>(versions.size());
    while (vs.hasNext() && rs.hasNext()) {
        MultiSearchResponse.Item it = rs.next();
        Tuple<String, Integer> idxAndVersion = vs.next();
        Map<String, Integer> counts = Collections.emptyMap();
        if (!it.isFailure()) {
            Terms aggs = it.getResponse()
                    .getAggregations()
                    .get("type");
            counts = aggs
                    .getBuckets()
                    .stream()
                    .collect(toMap(MultiBucketsAggregation.Bucket::getKeyAsString,
                            (b) -> (int) b.getDocCount()));
        }
        infos.add(new ListStoresAction.IndexStoreInfo(idxAndVersion.v1(), idxAndVersion.v2(), counts));
    }
    return new ListStoresActionResponse(infos);
}
 
Example #4
Source File: ElasticSearchServiceMapper.java    From vertx-elasticsearch-service with Apache License 2.0 6 votes vote down vote up
public static com.hubrick.vertx.elasticsearch.model.MultiSearchResponse mapToMultiSearchResponse(MultiSearchResponse esMultiSearchResponse) {
    final com.hubrick.vertx.elasticsearch.model.MultiSearchResponse multiSearchResponse = new com.hubrick.vertx.elasticsearch.model.MultiSearchResponse();

    multiSearchResponse.setRawResponse(readResponse(esMultiSearchResponse));
    multiSearchResponse.setResponses(
            Arrays.asList(
                    esMultiSearchResponse.getResponses()).stream()
                    .map(e -> {
                        final MultiSearchResponseItem multiSearchResponseItem = new MultiSearchResponseItem().setFailureMessage(e.getFailureMessage());
                        if(e.getResponse() != null) {
                            multiSearchResponseItem.setSearchResponse(mapToSearchResponse(e.getResponse()));
                        }

                        return multiSearchResponseItem;
                    }).collect(Collectors.toList())
    );

    return multiSearchResponse;
}
 
Example #5
Source File: NestedLoopsElasticExecutor.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private int combineResultsFromMultiResponses(List<SearchHit> combinedResults, int totalLimit, int currentCombinedResults, SearchHit[] hits, int currentIndex, MultiSearchRequest multiSearchRequest) {
    MultiSearchResponse.Item[] responses = client.multiSearch(multiSearchRequest).actionGet().getResponses();
    String t1Alias = nestedLoopsRequest.getFirstTable().getAlias();
    String t2Alias = nestedLoopsRequest.getSecondTable().getAlias();

    for(int j =0 ; j < responses.length && currentCombinedResults < totalLimit ; j++){
        SearchHit hitFromFirstTable = hits[currentIndex+j];
        onlyReturnedFields(hitFromFirstTable.getSourceAsMap(), nestedLoopsRequest.getFirstTable().getReturnedFields(),nestedLoopsRequest.getFirstTable().getOriginalSelect().isSelectAll());

        SearchResponse multiItemResponse = responses[j].getResponse();
        updateMetaSearchResults(multiItemResponse);

        //todo: if responseForHit.getHits.length < responseForHit.getTotalHits(). need to fetch more!
        SearchHits responseForHit = multiItemResponse.getHits();

        if(responseForHit.getHits().length == 0 && nestedLoopsRequest.getJoinType() == SQLJoinTableSource.JoinType.LEFT_OUTER_JOIN){
            SearchHit unmachedResult = createUnmachedResult(nestedLoopsRequest.getSecondTable().getReturnedFields(), currentCombinedResults, t1Alias, t2Alias, hitFromFirstTable);
            combinedResults.add(unmachedResult);
            currentCombinedResults++;
            continue;
        }

        for(SearchHit matchedHit : responseForHit.getHits() ){
            SearchHit searchHit = getMergedHit(currentCombinedResults, t1Alias, t2Alias, hitFromFirstTable, matchedHit);
            combinedResults.add(searchHit);
            currentCombinedResults++;
            if(currentCombinedResults >= totalLimit) break;
        }
        if(currentCombinedResults >= totalLimit) break;

    }
    return currentCombinedResults;
}
 
Example #6
Source File: InternalEsClient.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * 非同期でドキュメントをマルチ検索.
 * 存在しないインデックスに対して本メソッドを使用すると、TransportSerializationExceptionがスローされるので注意すること
 * @param index インデックス名
 * @param type タイプ名
 * @param routingId routingId
 * @param queryList マルチ検索用のクエリ情報リスト
 * @return 非同期応答
 */
public ActionFuture<MultiSearchResponse> asyncMultiSearch(
        String index,
        String type,
        String routingId,
        List<Map<String, Object>> queryList) {
    MultiSearchRequest mrequest = new MultiSearchRequest();
    if (queryList == null || queryList.size() == 0) {
        throw new EsMultiSearchQueryParseException();
    }
    for (Map<String, Object> query : queryList) {
        SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT);
        if (type != null) {
            req.types(type);
        }
        // クエリ指定なしの場合はタイプに対する全件検索を行う
        if (query != null) {
            req.source(query);
        }
        if (routingFlag) {
            req = req.routing(routingId);
        }
        mrequest.add(req);
    }

    ActionFuture<MultiSearchResponse> ret = esTransportClient.multiSearch(mrequest);
    this.fireEvent(Event.afterRequest, index, type, null, JSONArray.toJSONString(queryList), "MultiSearch");
    return ret;
}
 
Example #7
Source File: EsIndexImpl.java    From io with Apache License 2.0 5 votes vote down vote up
@Override
MultiSearchResponse onParticularError(ElasticsearchException e) {
    if (e instanceof SearchPhaseExecutionException) {
        throw new EsClientException("unknown property was appointed.", e);
    }
    throw e;
}
 
Example #8
Source File: EsTypeImpl.java    From io with Apache License 2.0 5 votes vote down vote up
@Override
MultiSearchResponse onParticularError(ElasticsearchException e) {
    if (e instanceof SearchPhaseExecutionException) {
        throw new EsClientException("unknown property was appointed.", e);
    }
    throw e;
}
 
Example #9
Source File: DcMultiSearchResponseImpl.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * .
 * @param response .
 * @return .
 */
public static DcMultiSearchResponse getInstance(MultiSearchResponse response) {
    if (response == null) {
        return null;
    }
    return new DcMultiSearchResponseImpl(response);
}
 
Example #10
Source File: DcMultiSearchResponseImpl.java    From io with Apache License 2.0 5 votes vote down vote up
@Override
public DcItem[] getResponses() {
    List<DcItemImpl> list = new ArrayList<DcItemImpl>();
    for (MultiSearchResponse.Item item : this.multiSearchResponse.getResponses()) {
        list.add((DcItemImpl) DcItemImpl.getInstance(item));
    }
    return list.toArray(new DcItemImpl[0]);
}
 
Example #11
Source File: DcMultiSearchResponseImpl.java    From io with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<DcItem> iterator() {
    List<DcItem> list = new ArrayList<DcItem>();
    for (MultiSearchResponse.Item item : this.multiSearchResponse.getResponses()) {
        list.add(DcItemImpl.getInstance(item));
    }
    return list.iterator();
}
 
Example #12
Source File: InternalEsClient.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * 非同期でドキュメントをマルチ検索.
 * 存在しないインデックスに対して本メソッドを使用すると、TransportSerializationExceptionがスローされるので注意すること
 * @param index インデックス名
 * @param type タイプ名
 * @param routingId routingId
 * @param queryList マルチ検索用のクエリ情報リスト
 * @return 非同期応答
 */
public ActionFuture<MultiSearchResponse> asyncMultiSearch(
        String index,
        String type,
        String routingId,
        List<Map<String, Object>> queryList) {
    MultiSearchRequest mrequest = new MultiSearchRequest();
    if (queryList == null || queryList.size() == 0) {
        throw new EsMultiSearchQueryParseException();
    }
    for (Map<String, Object> query : queryList) {
        SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT);
        if (type != null) {
            req.types(type);
        }
        // クエリ指定なしの場合はタイプに対する全件検索を行う
        if (query != null) {
            req.source(query);
        }
        if (routingFlag) {
            req = req.routing(routingId);
        }
        mrequest.add(req);
    }

    ActionFuture<MultiSearchResponse> ret = esTransportClient.multiSearch(mrequest);
    this.fireEvent(Event.afterRequest, index, type, null, JSONArray.toJSONString(queryList), "MultiSearch");
    return ret;
}
 
Example #13
Source File: EsIndexImpl.java    From io with Apache License 2.0 5 votes vote down vote up
@Override
MultiSearchResponse onParticularError(ElasticsearchException e) {
    if (e instanceof SearchPhaseExecutionException) {
        throw new EsClientException("unknown property was appointed.", e);
    }
    throw e;
}
 
Example #14
Source File: EsTypeImpl.java    From io with Apache License 2.0 5 votes vote down vote up
@Override
MultiSearchResponse onParticularError(ElasticsearchException e) {
    if (e instanceof SearchPhaseExecutionException) {
        throw new EsClientException("unknown property was appointed.", e);
    }
    throw e;
}
 
Example #15
Source File: AbstractEs6_4ClientInstrumentationTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiSearchRequest_validateSpanContentAndDbContext() throws InterruptedException, ExecutionException, IOException {
    createDocument();
    reporter.reset();

    MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
    SearchRequest firstSearchRequest = new SearchRequest(INDEX);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchQuery(FOO, BAR));
    firstSearchRequest.source(searchSourceBuilder);
    multiSearchRequest.add(firstSearchRequest);

    MultiSearchResponse response = doMultiSearch(multiSearchRequest);

    List<Span> spans = reporter.getSpans();
    assertThat(spans).hasSize(1);
    Span span = spans.get(0);
    validateSpanContent(span, "Elasticsearch: POST /_msearch", 200, "POST");
    verifyMultiSearchSpanContent(span);

    deleteDocument();
}
 
Example #16
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 #17
Source File: DistributedTableMetadataManager.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
private void handleFirstPhaseMultiSearchResponse(MultiSearchResponse multiResponse, String table, Map<String, FieldMetadata> fields,
                                                 Map<String, EstimationData> estimationDataMap) {
    for(MultiSearchResponse.Item item : multiResponse.getResponses()) {
        SearchResponse response = validateAndGetSearchResponse(item, table);
        if(null == response) {
            continue;
        }
        final long hits = response.getHits()
                .getTotalHits();
        Map<String, Aggregation> output = response.getAggregations().asMap();
        output.forEach((key, value) -> {
            FieldMetadata fieldMetadata = fields.get(key);
            if(fieldMetadata == null) {
                fieldMetadata = fields.get(key.replace("_", ""));
            }
            if(fieldMetadata == null) {
                return;
            }
            switch (fieldMetadata.getType()) {
                case STRING:
                    evaluateStringEstimation(value, table, key, fieldMetadata.getType(), estimationDataMap, hits);
                    break;
                case INTEGER:
                case LONG:
                case FLOAT:
                case DOUBLE:
                    evaluateDoubleEstimation(value, table, key, fieldMetadata.getType(), estimationDataMap, hits);
                    break;
                case BOOLEAN:
                    evaluateBooleanEstimation(key, estimationDataMap);
                break;
                case DATE:
                case OBJECT:
                case TEXT:
                case KEYWORD:
            }
        });
    }
}
 
Example #18
Source File: DistributedTableMetadataManager.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
private SearchResponse validateAndGetSearchResponse(MultiSearchResponse.Item item, String table) {
    if(item.isFailure()) {
        logger.info("FailureInDeducingCardinality table:{} failureMessage:{}", table, item.getFailureMessage());
        return null;
    }
    SearchResponse response = item.getResponse();
    if(null == response.getAggregations()) {
        return null;
    }
    return response;
}
 
Example #19
Source File: TestTransportClient.java    From jframe with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiSearch() {
    SearchRequestBuilder srb1 = client.prepareSearch().setQuery(QueryBuilders.queryStringQuery("elasticsearch")).setSize(1);
    SearchRequestBuilder srb2 = client.prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1);

    MultiSearchResponse sr = client.prepareMultiSearch().add(srb1).add(srb2).execute().actionGet();

    // You will get all individual responses from
    // MultiSearchResponse#getResponses()
    long nbHits = 0;
    for (MultiSearchResponse.Item item : sr.getResponses()) {
        SearchResponse response = item.getResponse();
        nbHits += response.getHits().getTotalHits();
    }
}
 
Example #20
Source File: TransportClient.java    From elasticsearch-jest-example with MIT License 5 votes vote down vote up
/**
 * 
 * @param queryString
 */
private static void multiSearch(String queryString){
	Client client = createTransportClient();
	SearchRequestBuilder srb1 = client.prepareSearch()
			.setQuery(QueryBuilders.queryStringQuery(queryString));
	
	SearchRequestBuilder srb2 = client.prepareSearch()
	        .setQuery(QueryBuilders.matchQuery("desc", queryString));

	MultiSearchResponse sr = client.prepareMultiSearch()
	        .add(srb1)
	        .add(srb2)
	        .execute().actionGet();

	long nbHits = 0;
	for (MultiSearchResponse.Item item : sr.getResponses()) {
	    SearchResponse response = item.getResponse();
	    nbHits += response.getHits().getTotalHits();
	    System.out.println("本次查询共匹配到:"+nbHits+"记录");
	    SearchHits searchHits = response.getHits();
		System.out.println("-----------------搜索关键字为:["+queryString+"]---------------------");
		System.out.println("共匹配到:"+searchHits.getTotalHits()+"条记录!");
		SearchHit[] hits = searchHits.getHits();
		for (SearchHit searchHit : hits) {
			Map<String, Object> sourceAsMap = searchHit.sourceAsMap();
			Set<String> keySet = sourceAsMap.keySet();
			for (String string : keySet) {
				System.out.println(string+":"+sourceAsMap.get(string));
			}
			System.out.println();
		}
	}
}
 
Example #21
Source File: DcMultiSearchResponseImpl.java    From io with Apache License 2.0 5 votes vote down vote up
@Override
public DcItem[] getResponses() {
    List<DcItemImpl> list = new ArrayList<DcItemImpl>();
    for (MultiSearchResponse.Item item : this.multiSearchResponse.getResponses()) {
        list.add((DcItemImpl) DcItemImpl.getInstance(item));
    }
    return list.toArray(new DcItemImpl[0]);
}
 
Example #22
Source File: DcMultiSearchResponseImpl.java    From io with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<DcItem> iterator() {
    List<DcItem> list = new ArrayList<DcItem>();
    for (MultiSearchResponse.Item item : this.multiSearchResponse.getResponses()) {
        list.add(DcItemImpl.getInstance(item));
    }
    return list.iterator();
}
 
Example #23
Source File: DcMultiSearchResponseImpl.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * .
 * @param response .
 * @return .
 */
public static DcMultiSearchResponse getInstance(MultiSearchResponse response) {
    if (response == null) {
        return null;
    }
    return new DcMultiSearchResponseImpl(response);
}
 
Example #24
Source File: RestCoordinateMultiSearchAction.java    From siren-join with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws Exception {
  MultiSearchRequest multiSearchRequest = new MultiSearchRequest();

  String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
  String[] types = Strings.splitStringByCommaToArray(request.param("type"));
  String path = request.path();
  boolean isTemplateRequest = isTemplateRequest(path);
  IndicesOptions indicesOptions = IndicesOptions.fromRequest(request, multiSearchRequest.indicesOptions());
  multiSearchRequest.add(RestActions.getRestContent(request), isTemplateRequest, indices, types, request.param("search_type"), request.param("routing"), indicesOptions, allowExplicitIndex);

  client.execute(CoordinateMultiSearchAction.INSTANCE, multiSearchRequest, new RestToXContentListener<MultiSearchResponse>(channel));
}
 
Example #25
Source File: RestMultiSearchAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws Exception {
    MultiSearchRequest multiSearchRequest = new MultiSearchRequest();

    String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    String[] types = Strings.splitStringByCommaToArray(request.param("type"));
    String path = request.path();
    boolean isTemplateRequest = isTemplateRequest(path);
    IndicesOptions indicesOptions = IndicesOptions.fromRequest(request, multiSearchRequest.indicesOptions());
    multiSearchRequest.add(RestActions.getRestContent(request), isTemplateRequest, indices, types, request.param("search_type"), request.param("routing"), indicesOptions, allowExplicitIndex);

    client.multiSearch(multiSearchRequest, new RestToXContentListener<MultiSearchResponse>(channel));
}
 
Example #26
Source File: ClientWithStats.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public ActionFuture<MultiSearchResponse> multiSearch(MultiSearchRequest request) {
	return wrapped.multiSearch(request);
}
 
Example #27
Source File: EsTypeImpl.java    From io with Apache License 2.0 4 votes vote down vote up
@Override
MultiSearchResponse doProcess() {
    return asyncMultiSearch(queryList).actionGet();
}
 
Example #28
Source File: CoordinateMultiSearchResponse.java    From siren-join with GNU Affero General Public License v3.0 4 votes vote down vote up
public CoordinateMultiSearchResponse(Item[] items) {
  super(new MultiSearchResponse.Item[0]); // hack: empty constructor is private
  this.items = items;
}
 
Example #29
Source File: CoordinateMultiSearchResponse.java    From siren-join with GNU Affero General Public License v3.0 4 votes vote down vote up
CoordinateMultiSearchResponse() {
  // hack: empty constructor is private, use this one instead
  // we use an empty array to avoid NPE during serialization
  super(new MultiSearchResponse.Item[0]);
}
 
Example #30
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;
}