Java Code Examples for org.elasticsearch.action.search.MultiSearchResponse#Item

The following examples show how to use org.elasticsearch.action.search.MultiSearchResponse#Item . 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 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
Source File: CoordinateMultiSearchResponse.java    From siren-join with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Iterator<MultiSearchResponse.Item> iterator() {
  return Iterators.forArray((MultiSearchResponse.Item[]) items);
}
 
Example 14
Source File: DcItemImpl.java    From io with Apache License 2.0 2 votes vote down vote up
/**
 *  .
 * @param source .
 * @return .
 */
public static DcItem getInstance(MultiSearchResponse.Item source) {
    return new DcItemImpl(source.getResponse(), source.getFailure());
}
 
Example 15
Source File: DcItemImpl.java    From io with Apache License 2.0 2 votes vote down vote up
/**
 *  .
 * @param source .
 * @return .
 */
public static DcItem getInstance(MultiSearchResponse.Item source) {
    return new DcItemImpl(source.getResponse(), source.getFailureMessage());
}