/** * Checks for the existence of a document. Emits {@literal true} if it exists, {@literal false} otherwise. * * @param consumer never {@literal null}. * @return the {@link Mono} emitting {@literal true} if it exists, {@literal false} otherwise. */ default Mono<Boolean> exists(Consumer<GetRequest> consumer) { GetRequest request = new GetRequest(); consumer.accept(request); return exists(request); }
/** * Execute a {@link GetRequest} against the {@literal get} API to retrieve a document by id. * * @param consumer never {@literal null}. * @see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html">Get API on * elastic.co</a> * @return the {@link Mono} emitting the {@link GetResult result}. */ default Mono<GetResult> get(Consumer<GetRequest> consumer) { GetRequest request = new GetRequest(); consumer.accept(request); return get(request); }
@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 Map<String, Object> get(String index, String type, String id) throws IOException { GetRequest get = new GetRequest(index, type, id); GetResponse resp = highLevelClient.get(get, new Header[]{}); return resp.getSource(); }
private Mono<GetResult> doFindById(String id, ElasticsearchPersistentEntity<?> entity, @Nullable String index, @Nullable String type) { return Mono.defer(() -> { IndexCoordinates indexCoordinates = operations.determineIndex(entity, index, type); return doFindById(new GetRequest(indexCoordinates.getIndexName(), indexCoordinates.getTypeName(), id)); }); }
private Mono<Boolean> doExists(String id, ElasticsearchPersistentEntity<?> entity, @Nullable String index, @Nullable String type) { return Mono.defer(() -> { IndexCoordinates indexCoordinates = operations.determineIndex(entity, index, type); return doExists(new GetRequest(indexCoordinates.getIndexName(), indexCoordinates.getTypeName(), id)); }); }
@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; }
try (Writer writer = new FileWriter(filepath)) { final GetResponse response = tc.get(new GetRequest(index).type(type).id(id).refresh(true).realtime(false)).actionGet();
/** * Verify the results in an Elasticsearch index. The results must first be produced into the index * using a {@link TestElasticsearchSinkFunction}; * * @param client The client to use to connect to Elasticsearch * @param index The index to check */ public static void verifyProducedSinkData(Client client, String index) { for (int i = 0; i < NUM_ELEMENTS; i++) { GetResponse response = client.get(new GetRequest(index, TYPE_NAME, Integer.toString(i))).actionGet(); Assert.assertEquals(DATA_PREFIX + i, response.getSource().get(DATA_FIELD_NAME)); } }
@Override public <T> T queryForObject(GetQuery query, Class<T> clazz, GetResultMapper mapper) { ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz); GetRequest request = new GetRequest(persistentEntity.getIndexName(), persistentEntity.getIndexType(), query.getId()); GetResponse response; try { response = client.get(request); T entity = mapper.mapResult(response, clazz); return entity; } catch (IOException e) { throw new ElasticsearchException("Error while getting for request: " + request.toString(), e); } }
/** * Creates a get request to get the JSON source from an index based on a type and id. Note, the * {@link GetRequest#type(String)} and {@link GetRequest#id(String)} must be set. * * @param index The index to get the JSON source from * @return The get request * @see org.elasticsearch.client.Client#get(org.elasticsearch.action.get.GetRequest) */ public static GetRequest getRequest(String index) { return new GetRequest(index); }
Assert.assertTrue(tc.get(new GetRequest("searchguard", "sg", "config")).actionGet().isExists()); Assert.assertTrue(tc.get(new GetRequest("searchguard","sg","internalusers")).actionGet().isExists()); Assert.assertTrue(tc.get(new GetRequest("searchguard","sg","roles")).actionGet().isExists()); Assert.assertTrue(tc.get(new GetRequest("searchguard","sg","rolesmapping")).actionGet().isExists()); Assert.assertTrue(tc.get(new GetRequest("searchguard","sg","actiongroups")).actionGet().isExists()); Assert.assertFalse(tc.get(new GetRequest("searchguard","sg","rolesmapping_xcvdnghtu165759i99465")).actionGet().isExists()); Assert.assertTrue(tc.get(new GetRequest("searchguard","sg","config")).actionGet().isExists());
public GetRequestBuilder(ElasticsearchClient client, GetAction action, @Nullable String index) { super(client, action, new GetRequest(index)); }
try { GetResponse response = testClient.get(new GetRequest(indexName, indexType, id1)); Assert.assertEquals(response.getId(), id1, "Response id matches request"); System.out.println(malformedDocPolicy + ":" + response.toString()); GetResponse response = testClient.get(new GetRequest(indexName, indexType, id2)); Assert.assertEquals(response.getId(), id2, "Response id matches request"); System.out.println(malformedDocPolicy + ":" + response.toString());
@Override public boolean exists(String index, String id) throws IOException { return client.exists(new GetRequest(index, getDefaultTypeName(), id)); }
@Override public boolean exists(String index, String id) throws IOException { return client.exists(new GetRequest(index, getDefaultTypeName(), id), RequestOptions.DEFAULT); }
GetResponse response = testClient.get(new GetRequest(indexName, indexType, id)); Assert.assertEquals(response.getId(), id, "Response id matches request"); Assert.assertEquals(response.isExists(), true, "Document not found");
@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; }