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; }
@Override public String get(String workflowInstanceId, String fieldToGet) { GetRequest request = new GetRequest(indexName, WORKFLOW_DOC_TYPE, workflowInstanceId); GetResponse response; try { response = elasticSearchClient.get(request); } catch (IOException e) { logger.error("Unable to get Workflow: {} from ElasticSearch index: {}", workflowInstanceId, indexName, e); return null; } 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; }
@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; }
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()); } }
/** * Get the latest timestamp for a given namespace. */ @SuppressWarnings("unchecked") public static Timestamp<?> getLastTimestamp(Client client, MongoDBRiverDefinition definition) { client.admin().indices().prepareRefresh(definition.getRiverIndexName()).get(); GetResponse lastTimestampResponse = client.prepareGet(definition.getRiverIndexName(), definition.getRiverName(), definition.getMongoOplogNamespace()).get(); if (lastTimestampResponse.isExists()) { Map<String, Object> mongodbState = (Map<String, Object>) lastTimestampResponse.getSourceAsMap().get(TYPE); if (mongodbState != null) { Timestamp<?> lastTimestamp = Timestamp.on(mongodbState); if (lastTimestamp != null) { return lastTimestamp; } } } else { if (definition.getInitialTimestamp() != null) { return definition.getInitialTimestamp(); } } return null; }
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 void onResponse(GetResponse getResponse) { List<Object> terms = new ArrayList<>(); if (getResponse.isSourceEmpty() == false) { // extract terms only if the doc source exists List<Object> extractedValues = XContentMapValues.extractRawValues(termsLookup.path(), getResponse.getSourceAsMap()); terms.addAll(extractedValues); } actionListener.onResponse(terms); }
@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; }
@Override public Map<String, Object> sourceAsMap() { return this.getResponse.getSourceAsMap(); } }
@Override public ESSearchHit get(String index, String id) throws IOException { GetRequest request = new GetRequest(index, getDefaultTypeName(), id); GetResponse response = client.get(request); ESSearchHit hit = new ESSearchHit(); hit.setIndex(response.getIndex()); hit.setId(response.getId()); hit.setVersion(response.getVersion()); hit.setSourceAsMap(response.getSourceAsMap()); return hit; }
@Override public ESSearchHit get(String index, String id) throws IOException { GetRequest request = new GetRequest(index, getDefaultTypeName(), 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; }
protected Map<String, Object> getItemMap(final long itemID) { if (cache != null) { final Map<String, Object> map = cache.getIfPresent(itemID); if (map != null) { return map; } } final GetResponse response = client .prepareGet(itemIndex, itemType, Long.toString(itemID)) .execute().actionGet(); if (response.isExists()) { final Map<String, Object> map = response.getSourceAsMap(); map.remove(itemIdField); map.remove(valueField); if (cache != null) { cache.put(itemID, map); } return map; } return null; }
protected Map<String, Object> getUserMap(final long userID) { if (cache != null) { final Map<String, Object> map = cache.getIfPresent(userID); if (map != null) { return map; } } final GetResponse response = client .prepareGet(userIndex, userType, Long.toString(userID)) .execute().actionGet(); if (response.isExists()) { final Map<String, Object> map = response.getSourceAsMap(); map.remove(userIdField); map.remove(valueField); if (cache != null) { cache.put(userID, map); } return map; } return null; }
@Override public void onResponse(GetResponse getResponse) { List<Object> terms = new ArrayList<>(); if (getResponse.isSourceEmpty() == false) { // extract terms only if the doc source exists List<Object> extractedValues = XContentMapValues.extractRawValues(termsLookup.path(), getResponse.getSourceAsMap()); terms.addAll(extractedValues); } actionListener.onResponse(terms); }
@Override public void onResponse(GetResponse getResponse) { List<Object> terms = new ArrayList<>(); if (getResponse.isSourceEmpty() == false) { // extract terms only if the doc source exists List<Object> extractedValues = XContentMapValues.extractRawValues(termsLookup.path(), getResponse.getSourceAsMap()); terms.addAll(extractedValues); } actionListener.onResponse(terms); }
public Object getField(String id, String field){ GetResponse response = client.prepareGet(this.indexName, this.documentType, id) .setFetchSource(field,null) // .setStoredFields(field) .execute() .actionGet(); Object res = null; if (response==null){ if (logger.isWarnEnabled()){ logger.warn("no response from document "+id+" when fetching field "+field+"!"); } return null; }else { res = response.getSourceAsMap().get(field); if (res==null){ if (logger.isWarnEnabled()) { logger.warn("document " + id + " has no field " + field + "!"); } } } return res; // return response.getField(field).getValue(); }
private ElasticSearchDoc parse(final GetResponse response) { ElasticSearchDoc result = null; if (response.isExists()) { final Map<String, Object> responseMap = response.getSourceAsMap(); final String user = (String) responseMap.get(ElasticSearchDoc.Field.USER); final boolean deleted = (boolean) responseMap.get(ElasticSearchDoc.Field.DELETED); final long timestamp = (long) responseMap.get(ElasticSearchDoc.Field.TIMESTAMP); @SuppressWarnings("unchecked") final Object dto = metacatJson.parseJsonValue( response.getSourceAsBytes(), getClass(response.getType()) ); result = new ElasticSearchDoc(response.getId(), dto, user, deleted, timestamp); } return result; }
private Integer getCurrentVersion() { GetResponse resp = client.prepareGet(settings.getIndexId(), MetadataDataMapping.METADATA_TYPE_NAME, MetadataDataMapping.METADATA_ROW_ID) .setOperationThreaded(false).get(); if (resp.isExists()) { Object versionString = resp.getSourceAsMap().get(MetadataDataMapping.METADATA_VERSION_FIELD.getName()); if (versionString == null) { throw new ElasticsearchException(String .format("Database inconsistency. Version can't be found in row %s/%s/%s", settings.getIndexId(), MetadataDataMapping.METADATA_TYPE_NAME, MetadataDataMapping.METADATA_ROW_ID)); } return Integer.valueOf(versionString.toString()); } else { return null; } }
private List<Object> fetch(TermsLookup termsLookup, Client client) { List<Object> terms = new ArrayList<>(); GetRequest getRequest = new GetRequest(termsLookup.index(), termsLookup.type(), termsLookup.id()) .preference("_local").routing(termsLookup.routing()); final GetResponse getResponse = client.get(getRequest).actionGet(); if (getResponse.isSourceEmpty() == false) { // extract terms only if the doc source exists List<Object> extractedValues = XContentMapValues.extractRawValues(termsLookup.path(), getResponse.getSourceAsMap()); terms.addAll(extractedValues); } return terms; }
@Override public ESSearchHit get(String index, String type, String id) throws IOException { GetRequest request = new GetRequest(index, type, id); GetResponse response = client.get(request); ESSearchHit hit = new ESSearchHit(); hit.setIndex(response.getIndex()); hit.setId(response.getId()); hit.setVersion(response.getVersion()); hit.setSourceAsMap(response.getSourceAsMap()); return hit; }