org.elasticsearch.action.get.MultiGetItemResponse Java Examples

The following examples show how to use org.elasticsearch.action.get.MultiGetItemResponse. 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: 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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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("没有找到该文档!");
			}

		}

	}