org.elasticsearch.action.get.MultiGetRequest Java Examples

The following examples show how to use org.elasticsearch.action.get.MultiGetRequest. 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: MultiGetApiMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    try{
        RestHighLevelClient client = HighLevelClient.getInstance();
        MultiGetRequest multiGetRequest = new MultiGetRequest();

        multiGetRequest.add(new MultiGetRequest.Item("jingma2_20180716","testlog","1"));
        multiGetRequest.add(new MultiGetRequest.Item("jingma2_20180716","testlog","2"));
        multiGetRequest.add(new MultiGetRequest.Item("jingma2_20180716","testlog","3"));

        MultiGetResponse multiGetResponse = client.multiGet(multiGetRequest);
        MultiGetItemResponse[] itemResponses = multiGetResponse.getResponses();
        for(int i=0;i<itemResponses.length;i++){
            System.out.println(itemResponses[i].getResponse());
        }
    }finally{
        HighLevelClient.close();
    }
}
 
Example #2
Source File: RestMultiGetAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws Exception {
    MultiGetRequest multiGetRequest = new MultiGetRequest();
    multiGetRequest.refresh(request.paramAsBoolean("refresh", multiGetRequest.refresh()));
    multiGetRequest.preference(request.param("preference"));
    multiGetRequest.realtime(request.paramAsBoolean("realtime", null));
    multiGetRequest.ignoreErrorsOnGeneratedFields(request.paramAsBoolean("ignore_errors_on_generated_fields", false));

    String[] sFields = null;
    String sField = request.param("fields");
    if (sField != null) {
        sFields = Strings.splitStringByCommaToArray(sField);
    }

    FetchSourceContext defaultFetchSource = FetchSourceContext.parseFromRestRequest(request);
    multiGetRequest.add(request.param("index"), request.param("type"), sFields, defaultFetchSource, request.param("routing"), RestActions.getRestContent(request), allowExplicitIndex);

    client.multiGet(multiGetRequest, new RestToXContentListener<MultiGetResponse>(channel));
}
 
Example #3
Source File: RestGetAnomalyDetectorAction.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    if (!EnabledSetting.isADPluginEnabled()) {
        throw new IllegalStateException(CommonErrorMessages.DISABLED_ERR_MSG);
    }
    String detectorId = request.param(DETECTOR_ID);
    String typesStr = request.param(TYPE);
    String rawPath = request.rawPath();
    if (!Strings.isEmpty(typesStr) || rawPath.endsWith(PROFILE) || rawPath.endsWith(PROFILE + "/")) {
        boolean all = request.paramAsBoolean("_all", false);
        return channel -> profileRunner
            .profile(detectorId, getProfileActionListener(channel, detectorId), getProfilesToCollect(typesStr, all));
    } else {
        boolean returnJob = request.paramAsBoolean("job", false);
        MultiGetRequest.Item adItem = new MultiGetRequest.Item(ANOMALY_DETECTORS_INDEX, detectorId)
            .version(RestActions.parseVersion(request));
        MultiGetRequest multiGetRequest = new MultiGetRequest().add(adItem);
        if (returnJob) {
            MultiGetRequest.Item adJobItem = new MultiGetRequest.Item(ANOMALY_DETECTOR_JOB_INDEX, detectorId)
                .version(RestActions.parseVersion(request));
            multiGetRequest.add(adJobItem);
        }

        return channel -> client.multiGet(multiGetRequest, onMultiGetResponse(channel, returnJob, detectorId));
    }
}
 
Example #4
Source File: ElasticSearchUtil.java    From ranger with Apache License 2.0 5 votes vote down vote up
public MultiGetItemResponse[] fetch(RestHighLevelClient client, String index, SearchHit... hits) throws IOException {
    if(0 == hits.length) {
        return new MultiGetItemResponse[0];
    }
    MultiGetRequest multiGetRequest = new MultiGetRequest();
    for (SearchHit hit : hits) {
        MultiGetRequest.Item item = new MultiGetRequest.Item(index, null, hit.getId());
        item.fetchSourceContext(FetchSourceContext.FETCH_SOURCE);
        multiGetRequest.add(item);
    }
    return client.multiGet(multiGetRequest, RequestOptions.DEFAULT).getResponses();
}
 
Example #5
Source File: TermVectorsRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public TermVectorsRequest(MultiGetRequest.Item item) {
    super(item.index());
    this.id = item.id();
    this.type = item.type();
    this.selectedFields(item.fields());
    this.routing(item.routing());
}
 
Example #6
Source File: ClientWithStats.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public ActionFuture<MultiGetResponse> multiGet(MultiGetRequest request) {
	return wrapped.multiGet(request);
}
 
Example #7
Source File: MultiGetRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
@Override
protected ActionFuture<MultiGetResponse> doExecute(MultiGetRequest request) {
    return client.multiGet(request);
}
 
Example #8
Source File: MultiGetRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
public MultiGetRequestBuilder(Client client, JsonToString<JsonInput> jsonToString, StringToJson<JsonOutput> stringToJson) {
    super(client, new MultiGetRequest(), jsonToString, stringToJson);
}
 
Example #9
Source File: FessEsClient.java    From fess with Apache License 2.0 4 votes vote down vote up
@Override
public void multiGet(final MultiGetRequest request, final ActionListener<MultiGetResponse> listener) {
    client.multiGet(request, listener);
}
 
Example #10
Source File: FessEsClient.java    From fess with Apache License 2.0 4 votes vote down vote up
@Override
public ActionFuture<MultiGetResponse> multiGet(final MultiGetRequest request) {
    return client.multiGet(request);
}
 
Example #11
Source File: RequestUtils.java    From ranger with Apache License 2.0 4 votes vote down vote up
public static <Request extends ActionRequest> List<String> getIndexFromRequest(Request request) {
	List<String> indexs = new ArrayList<>();

	if (request instanceof SingleShardRequest) {
		indexs.add(((SingleShardRequest<?>) request).index());
		return indexs;
	}

	if (request instanceof ReplicationRequest) {
		indexs.add(((ReplicationRequest<?>) request).index());
		return indexs;
	}

	if (request instanceof InstanceShardOperationRequest) {
		indexs.add(((InstanceShardOperationRequest<?>) request).index());
		return indexs;
	}

	if (request instanceof CreateIndexRequest) {
		indexs.add(((CreateIndexRequest) request).index());
		return indexs;
	}

	if (request instanceof PutMappingRequest) {
		indexs.add(((PutMappingRequest) request).getConcreteIndex().getName());
		return indexs;
	}

	if (request instanceof SearchRequest) {
		return Arrays.asList(((SearchRequest) request).indices());
	}

	if (request instanceof IndicesStatsRequest) {
		return Arrays.asList(((IndicesStatsRequest) request).indices());
	}

	if (request instanceof OpenIndexRequest) {
		return Arrays.asList(((OpenIndexRequest) request).indices());
	}

	if (request instanceof DeleteIndexRequest) {
		return Arrays.asList(((DeleteIndexRequest) request).indices());
	}

	if (request instanceof BulkRequest) {
		@SuppressWarnings("rawtypes") List<DocWriteRequest<?>> requests = ((BulkRequest) request).requests();

		if (CollectionUtils.isNotEmpty(requests)) {
			for (DocWriteRequest<?> docWriteRequest : requests) {
				indexs.add(docWriteRequest.index());
			}
			return indexs;
		}
	}

	if (request instanceof MultiGetRequest) {
		List<Item> items = ((MultiGetRequest) request).getItems();
		if (CollectionUtils.isNotEmpty(items)) {
			for (Item item : items) {
				indexs.add(item.index());
			}
			return indexs;
		}
	}

	// No matched request type to find specific index , set default value *
	indexs.add("*");
	return indexs;
}
 
Example #12
Source File: ConfigurationLoader.java    From openshift-elasticsearch-plugin with Apache License 2.0 4 votes vote down vote up
public void loadAsync(final String[] events, final ConfigCallback callback) {        
    if(events == null || events.length == 0) {
        log.warn("No config events requested to load");
        return;
    }
    
    final MultiGetRequest mget = new MultiGetRequest();

    for (int i = 0; i < events.length; i++) {
        final String event = events[i];
        mget.add(searchguardIndex, event, "0");
    }
    
    mget.refresh(true);
    mget.realtime(true);
    
    try (StoredContext ctx = threadContext.stashContext()) {
        threadContext.putHeader(ConfigConstants.SG_CONF_REQUEST_HEADER, "true");
    
        client.multiGet(mget, new ActionListener<MultiGetResponse>() {
            @Override
            public void onResponse(MultiGetResponse response) {
                MultiGetItemResponse[] responses = response.getResponses();
                for (int i = 0; i < responses.length; i++) {
                    MultiGetItemResponse singleResponse = responses[i];
                    if(singleResponse != null && !singleResponse.isFailed()) {
                        GetResponse singleGetResponse = singleResponse.getResponse();
                        if(singleGetResponse.isExists() && !singleGetResponse.isSourceEmpty()) {
                            //success
                            Long version = singleGetResponse.getVersion();
                            final Settings _settings = toSettings(singleGetResponse.getSourceAsBytesRef(), singleGetResponse.getType());
                            if(_settings != null) {
                                callback.success(singleGetResponse.getType(), _settings, version);
                            } else {
                                log.error("Cannot parse settings for " + singleGetResponse.getType());
                            }
                        } else {
                            //does not exist or empty source
                            callback.noData(singleGetResponse.getType());
                        }
                    } else {
                        //failure
                        callback.singleFailure(singleResponse == null ? null : singleResponse.getFailure());
                    }
                }
            }           
            
            @Override
            public void onFailure(Exception e) {
                callback.failure(e);
            }
        });
    }
}
 
Example #13
Source File: DefaultElasticSearchService.java    From vertx-elasticsearch-service with Apache License 2.0 4 votes vote down vote up
@Override
public void multiGet(final List<MultiGetQueryOptions> multiGetQueryOptions,
                     final MultiGetOptions options,
                     final Handler<AsyncResult<com.hubrick.vertx.elasticsearch.model.MultiGetResponse>> resultHandler) {

    final MultiGetRequestBuilder builder = client.prepareMultiGet();

    if (options != null) {
        if (options.getRefresh() != null) {
            builder.setRefresh(options.getRefresh());
        }
        if (options.getRealtime() != null) {
            builder.setRealtime(options.getRealtime());
        }
        if (options.getPreference() != null) {
            builder.setPreference(options.getPreference());
        }
    }

    for (MultiGetQueryOptions multiGetQueryOptionsItem : multiGetQueryOptions) {
        final MultiGetRequest.Item item = new MultiGetRequest.Item(multiGetQueryOptionsItem.getIndex(), multiGetQueryOptionsItem.getType(), multiGetQueryOptionsItem.getId());
        if (multiGetQueryOptionsItem.getParent() != null) item.parent(multiGetQueryOptionsItem.getParent());
        if (multiGetQueryOptionsItem.getRouting() != null) item.routing(multiGetQueryOptionsItem.getRouting());
        if (multiGetQueryOptionsItem.getStoredFields() != null)
            item.storedFields(multiGetQueryOptionsItem.getStoredFields().toArray(new String[0]));
        if (multiGetQueryOptionsItem.getFetchSource() != null) {
            item.fetchSourceContext(
                    new FetchSourceContext(
                            multiGetQueryOptionsItem.getFetchSource(),
                            multiGetQueryOptionsItem.getFetchSourceIncludes().toArray(new String[0]),
                            multiGetQueryOptionsItem.getFetchSourceExcludes().toArray(new String[0])
                    )
            );
        }

        builder.add(item);
    }

    builder.execute(new ActionListener<MultiGetResponse>() {
        @Override
        public void onResponse(final MultiGetResponse multiGetResponse) {
            resultHandler.handle(Future.succeededFuture(mapToMultiGetResponse(multiGetResponse)));
        }

        @Override
        public void onFailure(final Exception e) {
            handleFailure(resultHandler, e);
        }
    });
}
 
Example #14
Source File: ClientWithStats.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void multiGet(MultiGetRequest request, ActionListener<MultiGetResponse> listener) {
	wrapped.multiGet(request, listener);
}
 
Example #15
Source File: ElasticRequestUtils.java    From vind with Apache License 2.0 4 votes vote down vote up
public static MultiGetRequest getRealTimeGetRequest(String index, List<String> docIds) {
    final MultiGetRequest request = new MultiGetRequest();
    docIds.forEach(id -> request.add(new MultiGetRequest.Item(index, id)));
    return request;
}
 
Example #16
Source File: ElasticVindClient.java    From vind with Apache License 2.0 4 votes vote down vote up
public MultiGetResponse realTimeGet(List<String> ids) throws IOException {
    final MultiGetRequest request = ElasticRequestUtils.getRealTimeGetRequest(defaultIndex, ids);
    return client.mget(request, RequestOptions.DEFAULT);
}
 
Example #17
Source File: EsHighLevelRestTest2.java    From java-study with Apache License 2.0 4 votes vote down vote up
/**
	 * 多查询使用
	 * 
	 * @throws IOException
	 */
	private static void multiGet() throws IOException {

		MultiGetRequest request = new MultiGetRequest();
		request.add(new MultiGetRequest.Item("estest", "estest", "1"));
		request.add(new MultiGetRequest.Item("user", "userindex", "2"));
		// 禁用源检索,默认启用
//		request.add(new MultiGetRequest.Item("user", "userindex", "2").fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE));

		// 同步构建
		MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);

		// 异步构建
//		MultiGetResponse response2 = client.mgetAsync(request, RequestOptions.DEFAULT, listener);

		/*
		 * 返回的MultiGetResponse包含在' getResponses中的MultiGetItemResponse的列表,其顺序与请求它们的顺序相同。
		 * 如果成功,MultiGetItemResponse包含GetResponse或MultiGetResponse。如果失败了就失败。
		 * 成功看起来就像一个正常的GetResponse
		 */

		for (MultiGetItemResponse item : response.getResponses()) {
			assertNull(item.getFailure());
			GetResponse get = item.getResponse();
			String index = item.getIndex();
			String type = item.getType();
			String id = item.getId();
			// 如果请求存在
			if (get.isExists()) {
				long version = get.getVersion();
				String sourceAsString = get.getSourceAsString();
				Map<String, Object> sourceAsMap = get.getSourceAsMap();
				byte[] sourceAsBytes = get.getSourceAsBytes();
				System.out.println("查询的结果:" + sourceAsMap);
			} else {
				System.out.println("没有找到该文档!");
			}

		}

	}
 
Example #18
Source File: EnhancedRestHighLevelClient.java    From super-cloudops with Apache License 2.0 2 votes vote down vote up
/**
 * Asynchronously retrieves multiple documents by id using the Multi Get API
 *
 * See <a href=
 * "https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html">Multi
 * Get API on elastic.co</a>
 */
public void multiGetAsync(MultiGetRequest multiGetRequest, ActionListener<MultiGetResponse> listener, Header... headers) {
	execReturnVoid((r) -> r.multiGetAsync(multiGetRequest, listener, headers), false);
}
 
Example #19
Source File: EnhancedRestHighLevelClient.java    From super-cloudops with Apache License 2.0 2 votes vote down vote up
/**
 * Retrieves multiple documents by id using the Multi Get API
 *
 * See <a href=
 * "https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html">Multi
 * Get API on elastic.co</a>
 */
public final MultiGetResponse multiGet(MultiGetRequest multiGetRequest, Header... headers) throws IOException {
	return (MultiGetResponse) exec((r) -> r.multiGet(multiGetRequest, headers));
}