org.elasticsearch.action.get.MultiGetResponse Java Examples

The following examples show how to use org.elasticsearch.action.get.MultiGetResponse. 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: TableMapStore.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, Table> loadAll(Collection<String> keys) {
    logger.info("Load all called for multiple keys");
    MultiGetResponse response = elasticsearchConnection.getClient()
            .prepareMultiGet()
            .add(TABLE_META_INDEX, TABLE_META_TYPE, keys)
            .execute()
            .actionGet();
    Map<String, Table> tables = Maps.newHashMap();
    for(MultiGetItemResponse multiGetItemResponse : response) {
        try {
            Table table = objectMapper.readValue(multiGetItemResponse.getResponse()
                                                         .getSourceAsString(), Table.class);
            tables.put(table.getName(), table);
        } catch (Exception e) {
            throw new TableMapStoreException("Error getting data for table: " + multiGetItemResponse.getId());
        }
    }
    logger.info("Loaded value count: {}", tables.size());
    return tables;
}
 
Example #2
Source File: OpenShiftRestResponseTest.java    From openshift-elasticsearch-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testMGetResponse() throws Exception {
    
    String body = "{\"docs\":[{\"_index\":\"%1$s\",\"_type\":\"config\",\"_id\":\"0\",\"found\":true"
            + "},{\"_index\":\"%1$s\",\"_type\":\"config\","
            + "\"_id\":\"1\",\"found\":true}]}";
    MultiGetItemResponse [] items = new MultiGetItemResponse [2];
    for (int i = 0; i < items.length; i++) {
        String itemBody =  "{\"_index\":\"%1$s\",\"_type\":\"config\",\"_id\":\"" + i + "\",\"found\":true}";
        XContentParser parser = givenContentParser(itemBody);
        items[i] = new MultiGetItemResponse(GetResponse.fromXContent(parser), null);
    }
    
    MultiGetResponse actionResponse = new MultiGetResponse(items);
    
    OpenShiftRestResponse osResponse = whenCreatingResponseResponse(actionResponse);
    thenResponseShouldBeModified(osResponse, body);
}
 
Example #3
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 #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.MultiGetResponse mapToMultiGetResponse(MultiGetResponse esMultiGetResponse) {
    final com.hubrick.vertx.elasticsearch.model.MultiGetResponse multiGetResponse = new com.hubrick.vertx.elasticsearch.model.MultiGetResponse();

    multiGetResponse.setRawResponse(readResponse(esMultiGetResponse));
    multiGetResponse.setResponses(
            Arrays.asList(
                    esMultiGetResponse.getResponses()).stream()
                    .map(e -> {
                        final MultiGetResponseItem multiGetResponseItem = new MultiGetResponseItem().setId(e.getId()).setType(e.getType()).setIndex(e.getIndex());
                        if(e.getResponse() != null) {
                            multiGetResponseItem.setGetResult(mapToGetResult(e.getResponse()));
                        }
                        if (e.getFailure() != null) {
                            multiGetResponseItem.setFailureMessage(e.getFailure().getMessage());
                        }

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

    return multiGetResponse;
}
 
Example #5
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 #6
Source File: ElasticSearchServer.java    From vind with Apache License 2.0 6 votes vote down vote up
@Override
public GetResult execute(RealTimeGet search, DocumentFactory assets) {
    try {
        final StopWatch elapsedTime = StopWatch.createStarted();
        final MultiGetResponse response = elasticSearchClient.realTimeGet(search.getValues());
        elapsedTime.stop();

        if(response!=null){
            return ResultUtils.buildRealTimeGetResult(response, search, assets, elapsedTime.getTime()).setElapsedTime(elapsedTime.getTime());
        }else {
            log.error("Null result from ElasticClient");
            throw new SearchServerException("Null result from ElasticClient");
        }

    } catch (ElasticsearchException | IOException e) {
        log.error("Cannot execute realTime get query");
        throw new SearchServerException("Cannot execute realTime get query", e);
    }
}
 
Example #7
Source File: BaseDemo.java    From Elasticsearch-Tutorial-zh-CN with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 获取多个对象(根据ID)
 *
 * @param transportClient
 * @throws IOException
 */
private static void queryByMultiGet(TransportClient transportClient) throws IOException {

	MultiGetResponse multiGetItemResponses = transportClient.prepareMultiGet()
			.add("product_index", "product", "1")
			.add("product_index", "product", "2")
			.add("product_index", "product", "3")
			.add("product_index", "product", "4")
			.add("product_index", "product", "5")
			.get();

	String resultJSON = null;
	for (MultiGetItemResponse multiGetItemResponse : multiGetItemResponses) {
		GetResponse getResponse = multiGetItemResponse.getResponse();
		if (getResponse.isExists()) {
			resultJSON = getResponse.getSourceAsString();
		}
	}
	logger.info("--------------------------------:" + resultJSON);
}
 
Example #8
Source File: UKResultMapper.java    From youkefu with Apache License 2.0 5 votes vote down vote up
@Override
public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
	LinkedList<T> list = new LinkedList<T>();
	for (MultiGetItemResponse response : responses.getResponses()) {
		if (!response.isFailed() && response.getResponse().isExists()) {
			T result = mapEntity(response.getResponse().getSourceAsString(), clazz);
			setPersistentEntityId(result, response.getResponse().getId(), clazz);
			list.add(result);
		}
	}
	return list;
}
 
Example #9
Source File: TestTransportClient.java    From jframe with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiGet() {
    MultiGetResponse multiGetItemResponses = client.prepareMultiGet().add("twitter", "tweet", "1").add("twitter", "tweet", "2", "3", "4")
            .add("another", "type", "foo").get();

    for (MultiGetItemResponse itemResponse : multiGetItemResponses) {
        GetResponse response = itemResponse.getResponse();
        if (response.isExists()) {
            String json = response.getSourceAsString();
            System.out.println(json);
        }
    }
}
 
Example #10
Source File: IndexThread.java    From disthene with MIT License 5 votes vote down vote up
private void flush() {
    MultiGetResponse multiGetItemResponse = request.execute().actionGet();

    for(MultiGetItemResponse response : multiGetItemResponse.getResponses()) {
        if (response.isFailed()) {
            logger.error("Get failed: " + response.getFailure().getMessage());
        }

        Metric metric = request.metrics.get(response.getId());
        if (response.isFailed() || !response.getResponse().isExists()) {
            final String[] parts = metric.getPath().split("\\.");
            final StringBuilder sb = new StringBuilder();

            for (int i = 0; i < parts.length; i++) {
                if (sb.toString().length() > 0) {
                    sb.append(".");
                }
                sb.append(parts[i]);
                try {
                    bulkProcessor.add(new IndexRequest(index, type, metric.getTenant() + "_" + sb.toString()).source(
                            XContentFactory.jsonBuilder().startObject()
                                    .field("tenant", metric.getTenant())
                                    .field("path", sb.toString())
                                    .field("depth", (i + 1))
                                    .field("leaf", (i == parts.length - 1))
                                    .endObject()
                    ));
                } catch (IOException e) {
                    logger.error(e);
                }

            }

        }

    }

    request = new MetricMultiGetRequestBuilder(client, index, type);
}
 
Example #11
Source File: ElasticSearchRepository.java    From elastic-crud with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public List<T> findAll(final List<String> ids) {
  if (ids.isEmpty()) {
    return ImmutableList.of();
  }

  final Builder<T> builder = ImmutableList.builder();

  final MultiGetResponse response = client
    .prepareMultiGet()
    .add(index, type, ids)
    .execute()
    .actionGet();

  for(final MultiGetItemResponse item : response.getResponses()) {
    final GetResponse get = item.getResponse();
    if(get.isSourceEmpty()) {
      continue;
    }

    final String json = get.getSourceAsString();
    final T entity = deserializer.apply(json);
    builder.add((T) entity.withId(get.getId()));
  }

  return builder.build();
}
 
Example #12
Source File: MultiGetDemo.java    From elasticsearch-full with Apache License 2.0 5 votes vote down vote up
@Test
public void name() throws Exception {
    MultiGetResponse multiGetItemResponses = client.prepareMultiGet()
            .add("twitter", "tweet", "1")
            .add("twitter", "tweet", "2", "3", "4")
            .add("another", "type", "foo")
            .get();
    for (MultiGetItemResponse itemResponse : multiGetItemResponses) {
        GetResponse response = itemResponse.getResponse();
        if (response.isExists()) {
            String json = response.getSourceAsString();
        }
    }
}
 
Example #13
Source File: ResultUtils.java    From vind with Apache License 2.0 5 votes vote down vote up
public static GetResult buildRealTimeGetResult(MultiGetResponse response, RealTimeGet query, DocumentFactory factory, long elapsedTime) {

        final List<Document> docResults = new ArrayList<>();

        final List<MultiGetItemResponse> results = Arrays.asList(response.getResponses());
        if(CollectionUtils.isNotEmpty(results)){
            docResults.addAll(results.stream()
                    .map(MultiGetItemResponse::getResponse)
                    .map(GetResponse::getSourceAsMap)
                    .map(jsonMap -> DocumentUtil.buildVindDoc(jsonMap ,factory,null))
                    .collect(Collectors.toList()));
        }
        final long nResults = docResults.size();
        return new GetResult(nResults,docResults,query,factory,elapsedTime).setElapsedTime(elapsedTime);
    }
 
Example #14
Source File: ElasticSearch.java    From hsweb-learning with Apache License 2.0 5 votes vote down vote up
private static void MultiGet(Client client) {

        MultiGetResponse response = client.prepareMultiGet()
                .add("twitter","tweet","2","1")
                .get();
        for(MultiGetItemResponse itemResponse:response){
            GetResponse getResponse = itemResponse.getResponse();
            if(getResponse.isExists()){
                System.out.println(getResponse.getSourceAsString());
            }
        }
    }
 
Example #15
Source File: ElasticsearchClient.java    From yacy_grid_mcp with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Map<String, Map<String, Object>> readMapBulkInternal(final String indexName, final Collection<String> ids) {
    MultiGetRequestBuilder mgrb = elasticsearchClient.prepareMultiGet();
    ids.forEach(id -> mgrb.add(indexName, null, id).execute().actionGet());
    MultiGetResponse response = mgrb.execute().actionGet();
    Map<String, Map<String, Object>> bulkresponse = new HashMap<>();
    for (MultiGetItemResponse r: response.getResponses()) {
        GetResponse gr = r.getResponse();
        if (gr != null) {
            Map<String, Object> map = getMap(gr);
            bulkresponse.put(r.getId(), map);
        }
    }
    return bulkresponse;
}
 
Example #16
Source File: ElasticsearchClient.java    From yacy_grid_mcp with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Set<String> existBulkInternal(String indexName, final Collection<String> ids) {
    if (ids == null || ids.size() == 0) return new HashSet<>();
    MultiGetResponse multiGetItemResponses = elasticsearchClient.prepareMultiGet()
            .add(indexName, null, ids)
            .get();
    Set<String> er = new HashSet<>();
    for (MultiGetItemResponse itemResponse : multiGetItemResponses) { 
        GetResponse response = itemResponse.getResponse();
        if (response.isExists()) {
            er.add(response.getId());
        }
    }
    return er;
}
 
Example #17
Source File: XiaoEUKResultMapper.java    From youkefu with Apache License 2.0 5 votes vote down vote up
@Override
public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
	LinkedList<T> list = new LinkedList<T>();
	for (MultiGetItemResponse response : responses.getResponses()) {
		if (!response.isFailed() && response.getResponse().isExists()) {
			T result = mapEntity(response.getResponse().getSourceAsString(), clazz);
			setPersistentEntityId(result, response.getResponse().getId(), clazz);
			list.add(result);
		}
	}
	return list;
}
 
Example #18
Source File: ResultMapperExt.java    From roncoo-education with MIT License 5 votes vote down vote up
@Override
public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
	LinkedList<T> list = new LinkedList<>();
	for (MultiGetItemResponse response : responses.getResponses()) {
		if (!response.isFailed() && response.getResponse().isExists()) {
			T result = mapEntity(response.getResponse().getSourceAsString(), clazz);
			setPersistentEntityId(result, response.getResponse().getId(), clazz);
			setPersistentEntityVersion(result, response.getResponse().getVersion(), clazz);
			list.add(result);
		}
	}
	return list;
}
 
Example #19
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 #20
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 #21
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 #22
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 #23
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 #24
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 #25
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 #26
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 #27
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 #28
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 #29
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));
}