@Test public void shouldDeleteDocumentBySpecifiedTypeUsingDeleteQuery() { // given String documentId = randomNumeric(5); SampleEntity sampleEntity = new SampleEntity(); sampleEntity.setId(documentId); sampleEntity.setMessage("some message"); sampleEntity.setVersion(System.currentTimeMillis()); IndexQuery indexQuery = new IndexQuery(); indexQuery.setId(documentId); indexQuery.setObject(sampleEntity); elasticsearchTemplate.index(indexQuery); // when DeleteQuery deleteQuery = new DeleteQuery(); deleteQuery.setQuery(fieldQuery("id", documentId)); deleteQuery.setIndex("test-index"); deleteQuery.setType("test-type"); elasticsearchTemplate.delete(deleteQuery); // then SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(fieldQuery("id", documentId)).build(); Page<SampleEntity> sampleEntities = elasticsearchTemplate.queryForPage(searchQuery, SampleEntity.class); assertThat(sampleEntities.getTotalElements(), equalTo(0L)); }
@Override public void delete(DeleteQuery deleteQuery) { Assert.notNull(deleteQuery.getIndex(), "No index defined for Query"); Assert.notNull(deleteQuery.getType(), "No type define for Query"); client .prepareDeleteByQuery(deleteQuery.getIndex()) .setTypes(deleteQuery.getType()) .setQuery(deleteQuery.getQuery()) .execute() .actionGet(); }
@Override public <T> void delete(DeleteQuery deleteQuery, Class<T> clazz) { ElasticsearchPersistentEntity persistentEntity = getPersistentEntityFor(clazz); client .prepareDeleteByQuery(persistentEntity.getIndexName()) .setTypes(persistentEntity.getIndexType()) .setQuery(deleteQuery.getQuery()) .execute() .actionGet(); }