Refine search
.prepareGet(indexName, typeName, id) .setOperationThreaded(false) .setFields(new String[]{}) .execute() .actionGet(); return getResponse.isExists();
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 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(); } }
private void createUserID(final long userID) { final GetResponse getResponse = client .prepareGet(userIndex, userType, Long.toString(userID)) .setRefresh(true).execute().actionGet(); if (!getResponse.isExists()) { final Map<String, Object> source = new HashMap<>(); source.put("system_id", Long.toString(userID)); source.put(userIdField, userID); source.put(timestampField, new Date()); final IndexResponse response = client .prepareIndex(userIndex, userType, Long.toString(userID)) .setSource(source).setRefresh(true).execute().actionGet(); if (!response.isCreated()) { throw new TasteException("Failed to create " + source); } } }
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; } }
@Override public int getFlaghashcode(final String id) throws IOException, MessagingException { createIndexIfNotExists(); client.admin().indices().refresh(new RefreshRequest()).actionGet(); final GetResponse getResponse = client.prepareGet().setIndex(index).setType(type).setId(id) .setFields(new String[] { "flaghashcode" }).execute().actionGet(); if (getResponse == null || !getResponse.isExists()) { return -1; } final GetField flaghashcodeField = getResponse.getField("flaghashcode"); if (flaghashcodeField == null || flaghashcodeField.getValue() == null || !(flaghashcodeField.getValue() instanceof Number)) { throw new IOException("No flaghashcode field for id " + id+ " ("+(flaghashcodeField==null?"null":"Val: "+flaghashcodeField.getValue())+")"); } return ((Number) flaghashcodeField.getValue()).intValue(); }
@Override public GetResponse get() { Profiler profiler = Profiler.createIfTrace(EsClient.LOGGER).start(); try { return super.execute().actionGet(); } catch (Exception e) { throw new IllegalStateException(String.format("Fail to execute %s", toString()), e); } finally { if (profiler.isTraceEnabled()) { profiler.stopTrace(toString()); } } }
@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)); }
/** * Read a json document from the search index for a given id. * Elasticsearch reads the '_source' field and parses the content as json. * * @param id * the unique identifier of a document * @return the document as json, matched on a Map<String, Object> object instance */ public Map<String, Object> readMap(String indexName, final String id) { GetResponse response = elasticsearchClient.prepareGet(indexName, null, id).execute().actionGet(); Map<String, Object> map = getMap(response); return map; }
.setOperationThreaded(false).execute().get(); Map<String, Object> results = get.getSource(); if (get.isExists())
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); }
/** * Read a document from the search index for a given id. * This is the cheapest document retrieval from the '_source' field because * elasticsearch does not do any json transformation or parsing. We * get simply the text from the '_source' field. This might be useful to * make a dump from the index content. * * @param id * the unique identifier of a document * @return the document as source text */ public byte[] readSource(String indexName, final String id) { GetResponse response = elasticsearchClient.prepareGet(indexName, null, id).execute().actionGet(); return response.getSourceAsBytes(); }
public Product getProduct(ElasticSearchIndexConfig config, Long productId) GetResponse getResponse = searchClientService.getClient().prepareGet(config.getIndexAliasName(), config.getDocumentType(), String.valueOf(productId)) .setFields(SearchDocumentFieldName.productDocumentFields) .get(); if(getResponse.isExists()) product.setId(Long.valueOf(getResponse.getId())); product.setTitle(getResponse.getField(SearchDocumentFieldName.TITLE.getFieldName()).getValue().toString()); product.setDescription(getResponse.getField(SearchDocumentFieldName.DESCRIPTION.getFieldName()).getValue().toString()); product.setSoldOut(Boolean.valueOf(getResponse.getField(SearchDocumentFieldName.SOLD_OUT.getFieldName()).getValue().toString()));
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); } }
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(); }
@Override public List<StreamsDatum> process(StreamsDatum entry) { List<StreamsDatum> result = new ArrayList<>(); if (entry == null || entry.getMetadata() == null) { return result; } Map<String, Object> metadata = entry.getMetadata(); String index = ElasticsearchMetadataUtil.getIndex(metadata, config); String type = ElasticsearchMetadataUtil.getType(metadata, config); String id = ElasticsearchMetadataUtil.getId(entry); GetRequestBuilder getRequestBuilder = elasticsearchClientManager.client().prepareGet(index, type, id); getRequestBuilder.setFields("*", "_timestamp"); getRequestBuilder.setFetchSource(true); GetResponse getResponse = getRequestBuilder.get(); if ( getResponse == null || !getResponse.isExists() || getResponse.isSourceEmpty() ) { return result; } entry.setDocument(getResponse.getSource()); if ( getResponse.getField("_timestamp") != null) { DateTime timestamp = new DateTime(((Long) getResponse.getField("_timestamp").getValue()).longValue()); entry.setTimestamp(timestamp); } result.add(entry); return result; }