public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) { try { final GetResponse response = client.prepareGet(indexKey, table, key).execute().actionGet(); if (response.isExists()) { if (fields != null) { for (String field : fields) { result.put(field, new StringByteIterator( (String) response.getSource().get(field))); for (String field : response.getSource().keySet()) { result.put(field, new StringByteIterator( (String) response.getSource().get(field)));
private static GetResult getResponseToGetResult(GetResponse response) { return new GetResult(response.getIndex(), response.getType(), response.getId(), response.getVersion(), response.isExists(), response.getSourceAsBytesRef(), response.getFields()); }
public static Status getRiverStatus(Client client, String riverName) { GetResponse statusResponse = client.prepareGet("_river", riverName, MongoDBRiver.STATUS_ID).get(); if (!statusResponse.isExists()) { return Status.UNKNOWN; } else { Object obj = XContentMapValues.extractValue(MongoDBRiver.TYPE + "." + MongoDBRiver.STATUS_FIELD, statusResponse.getSourceAsMap()); return Status.valueOf(obj.toString()); } }
private static Map<String, Object> getMap(GetResponse response) { Map<String, Object> map = null; if (response.isExists() && (map = response.getSourceAsMap()) != null) { map.put("$type", response.getType()); } return map; }
private Optional<String> getMetadata(String id) { GetRequestBuilder request = esClient.prepareGet(MetadataIndexDefinition.INDEX_TYPE_METADATA, id) .setStoredFields(MetadataIndexDefinition.FIELD_VALUE); GetResponse response = request.get(); if (response.isExists()) { GetField field = response.getField(MetadataIndexDefinition.FIELD_VALUE); String value = String.valueOf(field.getValue()); return Optional.of(value); } return Optional.empty(); }
@Override public Boolean execute(Client client) throws ElasticsearchException { if ((index != null) && (type != null) && (id != null)) { // Check if a document exists GetResponse response = client.prepareGet(index, type, id).setRefresh(true).execute().actionGet(); return response.isExists(); } else { // Check if index exists IndicesExistsResponse response = client.admin().indices().prepareExists(index).execute().actionGet(); return response.isExists(); } }
@Override public synchronized State getRiverState(final Folder folder) throws MessagingException { try { waitForCluster(); if (client.admin().indices().prepareExists(index()).execute().actionGet().isExists()) { final GetResponse response = client .prepareGet(index(), RIVERSTATE_TYPE, FOLDERSTATE_ID + "_" + folder.getURLName().toString().hashCode()).execute() .get(); if (!response.isSourceEmpty()) { return mapper.readValue(response.getSourceAsString(), new TypeReference<State>() { }); } } } catch (final Exception ex) { throw new MessagingException("Unable to get river state", ex); } final State rs = new State(); rs.setFolderUrl(folder.getURLName().toString()); // rs.setLastUid(1L); rs.setExists(true); return rs; }
private String getLastUpdatedTimestamp() { GetResponse lastUpdatedTimestampResponse = client.prepareGet().setIndex(riverIndexName).setType(riverName.name()).setId("lastUpdatedTimestamp").execute().actionGet(); if (lastUpdatedTimestampResponse.isExists() && lastUpdatedTimestampResponse.getSource().containsKey("lastUpdatedTimestamp")) { return lastUpdatedTimestampResponse.getSource().get("lastUpdatedTimestamp").toString(); } return null; } }
@SuppressWarnings("unchecked") @Override public Optional<T> findOne(final String id) { final GetResponse response = client .prepareGet(index, type, id) .setFetchSource(true) .execute() .actionGet(); final String json = response.getSourceAsString(); return ofNullable(json) .map(deserializer) .map(e -> (T) e.withId(id)); }
public NamedQuery get(String name) { GetResponse response = getConnection().getClient() .prepareGet(getIndex(), getType(), name.toLowerCase()) .setFetchSource(true) .get(); if (response.isExists()) { return mapToNamedQuery(response.getSource()); } return null; }
public ElasticsearchSample get(Object key, String... fields) { GetResponse getResponse = client.prepareGet(index, type, String.valueOf(key)).setFields(fields).execute() .actionGet(); Map<String, Object> map = new TreeMap<String, Object>(); map.put(ID, getResponse.getId()); for (String k : getResponse.getFields().keySet()) { map.put(k, getResponse.getField(k).getValue()); } return new ElasticsearchSample(this, map); }
/** * Checks if the requested document or source is missing. * * @param response a response * @throws ResourceNotFoundException if the document or source is missing */ private void checkResource(final GetResponse response) { final String index = response.getIndex(); final String type = response.getType(); final String id = response.getId(); if (response.isExists() == false) { throw new ResourceNotFoundException("Document not found [" + index + "]/[" + type + "]/[" + id + "]"); } else if (response.isSourceEmpty()) { throw new ResourceNotFoundException("Source not found [" + index + "]/[" + type + "]/[" + id + "]"); } } }
try (Writer writer = new FileWriter(filepath)) { final GetResponse response = tc.get(new GetRequest(index).type(type).id(id).refresh(true).realtime(false)).actionGet(); if (response.isExists()) { if(response.isSourceEmpty()) { System.out.println(" FAIL: Configuration for '"+_id+"' failed because of empty source"); return false; String yaml = convertToYaml(_id, response.getSourceAsBytesRef(), true); writer.write(yaml); System.out.println(" SUCC: Configuration for '"+_id+"' stored in "+filepath);
@Override @SneakyThrows({ IOException.class }) public <T> List<T> findByIds(Class<T> clazz, String... ids) { String indexName = getIndexForType(clazz); String typeName = MappingBuilder.indexTypeFromClass(clazz); MultiGetResponse response = getClient().prepareMultiGet().add(indexName, typeName, ids).execute().actionGet(); if (response == null || response.getResponses() == null || response.getResponses().length == 0) { ESIndexMapper.getLog().debug("Nothing found in index [ {} ], type [ {} ], for Ids [ {} ].", indexName, typeName, Arrays.toString(ids)); return null; } List<T> result = new ArrayList<>(); for (MultiGetItemResponse getItemResponse : response.getResponses()) { if (getItemResponse.getResponse().isExists()) { result.add(getJsonMapper().readValue(getItemResponse.getResponse().getSourceAsString(), clazz)); } } return result; }
@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(); }
private SearchGuardLicense createOrGetTrial(String msg) { long created = System.currentTimeMillis(); ThreadContext threadContext = threadPool.getThreadContext(); try(StoredContext ctx = threadContext.stashContext()) { threadContext.putHeader(ConfigConstants.SG_CONF_REQUEST_HEADER, "true"); GetResponse get = client.prepareGet(searchguardIndex, "sg", "tattr").get(); if(get.isExists()) { created = (long) get.getSource().get("val"); } else { try { client.index(new IndexRequest(searchguardIndex) .type("sg") .id("tattr") .setRefreshPolicy(RefreshPolicy.IMMEDIATE) .create(true) .source("{\"val\": "+System.currentTimeMillis()+"}", XContentType.JSON)).actionGet(); } catch (VersionConflictEngineException e) { //ignore } catch (Exception e) { LOGGER.error("Unable to index tattr", e); } } } return SearchGuardLicense.createTrialLicense(formatDate(created), clusterService, msg); } }
protected static Map<String, Object> getMap(GetResponse response) { Map<String, Object> map = null; if (response.isExists() && (map = response.getSourceAsMap()) != null) { if (!map.containsKey("id")) map.put("id", response.getId()); if (!map.containsKey("type")) map.put("type", response.getType()); } return map; }
@Override public ESSearchHit get(String index, String id) throws IOException { GetRequest request = new GetRequest(index, id); GetResponse response = client.get(request, RequestOptions.DEFAULT); ESSearchHit hit = new ESSearchHit(); hit.setIndex(response.getIndex()); hit.setId(response.getId()); hit.setVersion(response.getVersion()); hit.setSourceAsMap(response.getSourceAsMap()); return hit; }
.setOperationThreaded(false).execute().get(); Map<String, Object> results = get.getSource(); if (get.isExists())
@Override public String get(String workflowInstanceId, String fieldToGet) { GetRequest request = new GetRequest(indexName, WORKFLOW_DOC_TYPE, workflowInstanceId) .fetchSourceContext( new FetchSourceContext(true, new String[]{fieldToGet}, Strings.EMPTY_ARRAY)); GetResponse response = elasticSearchClient.get(request).actionGet(); if (response.isExists()) { Map<String, Object> sourceAsMap = response.getSourceAsMap(); if (sourceAsMap.containsKey(fieldToGet)) { return sourceAsMap.get(fieldToGet).toString(); } } logger.debug("Unable to find Workflow: {} in ElasticSearch index: {}.", workflowInstanceId, indexName); return null; }