private static GetResult getResponseToGetResult(GetResponse response) { return new GetResult(response.getIndex(), response.getType(), response.getId(), response.getVersion(), response.isExists(), response.getSourceAsBytesRef(), response.getFields()); }
/** * The index name of the document. */ public String getIndex() { if (failure != null) { return failure.getIndex(); } return response.getIndex(); }
/** * 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 + "]"); } } }
@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 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; }
/** * The index name of the document. */ public String getIndex() { if (failure != null) { return failure.getIndex(); } return response.getIndex(); }
/** * The index name of the document. */ public String getIndex() { if (failure != null) { return failure.getIndex(); } return response.getIndex(); }
/** * The index name of the document. */ public String getIndex() { if (failure != null) { return failure.getIndex(); } return response.getIndex(); }
/** * The index name of the document. */ public String getIndex() { if (failure != null) { return failure.getIndex(); } return response.getIndex(); }
public static void assertExists(GetResponse response) { String message = String.format(Locale.ROOT, "Expected %s/%s/%s to exist, but does not", response.getIndex(), response.getType(), response.getId()); assertThat(message, response.isExists(), is(true)); }
@Override public void onResponse(GetResponse getResponse) { JsonObject source = (getResponse.isExists() ? new JsonObject(getResponse.getSourceAsString()) : null); JsonObject reply = new JsonObject() .put(CONST_INDEX, getResponse.getIndex()) .put(CONST_TYPE, getResponse.getType()) .put(CONST_ID, getResponse.getId()) .put(CONST_VERSION, getResponse.getVersion()) .put(CONST_SOURCE, source); resultHandler.handle(Future.succeededFuture(reply)); }
@SuppressWarnings("unchecked") protected <T extends ESEntity> T buildEntityFromGetResponse(Class<T> entityClass, MultiGetItemResponse item) { GetResponse response = item.getResponse(); if (!response.isExists()) throw new DaoException(String.format( "Entity %s does not exist in %s/%s", response.getId(), response.getIndex(), response.getType())); if (entityClass == null) throw new IllegalArgumentException("Provided Entity class is null"); else if (entityClass.equals(ESNode.class)) return (T) ESNode.Builder.buildFromGetReponse(response); else if (entityClass.equals(ESWay.class)) return (T) ESWay.Builder.buildFromGetReponse(response); else throw new IllegalArgumentException(entityClass.getSimpleName() + " is not a known Entity"); }
@Override public Metadata searchById(Optional<String> index, String id) throws NotFoundException { log.debug("Search in ElasticSearch by ID [index: {}, ID: {}]", index, id); // Validation if (StringUtils.isEmpty(id)) throw new IllegalArgumentException("id" + ERROR_NOT_NULL_OR_EMPTY); try { String indexFormatted = formatIndex(index); GetResponse response = client.prepareGet(indexFormatted, indexFormatted, id).get(); log.trace("Search one document in ElasticSearch [index: {}, ID: {}] : response= {}", indexFormatted, id, response); if (!response.isExists()) { throw new NotFoundException( "Document [index: " + indexFormatted + ", ID: " + id + "] not found"); } Metadata metadata = convert(response.getIndex(), response.getId(), response.getSourceAsMap()); log.debug("Search in ElasticSearch by ID [index: {}, ID: {}]: {}", index, id, metadata); return metadata; } catch (NotFoundException ex) { log.warn("Error while searching into ElasticSearch [index: {}, ID: {}]", index, id, ex); throw ex; } catch (Exception ex) { log.error("Error while searching into ElasticSearch [index: {}, ID: {}]", index, id, ex); throw new TechnicalException( "Error while searching into ElasticSearch: " + ex.getMessage()); } }
/** * Returns a Document representing the specified document ID (combination of resource and context), or * null when no such Document exists yet. */ @Override protected SearchDocument getDocument(String id) throws IOException { GetResponse response = client.prepareGet(indexName, documentType, id).execute().actionGet(); if (response.isExists()) { return new ElasticsearchDocument(response.getId(), response.getType(), response.getIndex(), response.getVersion(), response.getSource(), geoContextMapper); } // no such Document return null; }
@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; }