use of org.springframework.data.elasticsearch.core.query.Query in project spring-data-elasticsearch by spring-projects.
the class CriteriaQueryMappingUnitTests method shouldMapNamesInSourceFieldsAndSourceFilters.
// #1778
@Test
@DisplayName("should map names in source fields and SourceFilters")
void shouldMapNamesInSourceFieldsAndSourceFilters() {
Query query = Query.findAll();
// Note: we don't care if these filters make sense here, this test is only about name mapping
query.addFields("firstName", "lastName");
query.addSourceFilter(new FetchSourceFilterBuilder().withIncludes("firstName").withExcludes("lastName").build());
mappingElasticsearchConverter.updateQuery(query, Person.class);
SoftAssertions softly = new SoftAssertions();
softly.assertThat(query.getFields()).containsExactly("first-name", "last-name");
SourceFilter sourceFilter = query.getSourceFilter();
softly.assertThat(sourceFilter).isNotNull();
softly.assertThat(sourceFilter.getIncludes()).containsExactly("first-name");
softly.assertThat(sourceFilter.getExcludes()).containsExactly("last-name");
softly.assertAll();
}
use of org.springframework.data.elasticsearch.core.query.Query in project spring-data-elasticsearch by spring-projects.
the class CriteriaQueryMappingUnitTests method shouldMapNamesInSourceStoredFields.
@Test
@DisplayName("should map names in source stored fields")
void shouldMapNamesInSourceStoredFields() {
Query query = Query.findAll();
query.addStoredFields("firstName", "lastName");
mappingElasticsearchConverter.updateQuery(query, Person.class);
SoftAssertions softly = new SoftAssertions();
List<String> storedFields = query.getStoredFields();
softly.assertThat(storedFields).isNotNull();
softly.assertThat(storedFields).containsExactly("first-name", "last-name");
softly.assertAll();
}
use of org.springframework.data.elasticsearch.core.query.Query in project spring-data-elasticsearch by spring-projects.
the class SimpleElasticsearchRepository method deleteAll.
@Override
public void deleteAll() {
IndexCoordinates indexCoordinates = getIndexCoordinates();
Query query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).build();
executeAndRefresh((OperationsCallback<Void>) operations -> {
operations.delete(query, entityClass, indexCoordinates);
return null;
});
}
use of org.springframework.data.elasticsearch.core.query.Query in project spring-data-elasticsearch by spring-projects.
the class SimpleElasticsearchRepository method deleteAllById.
@Override
public void deleteAllById(Iterable<? extends ID> ids) {
Assert.notNull(ids, "Cannot delete 'null' list.");
List<String> idStrings = new ArrayList<>();
for (ID id : ids) {
idStrings.add(stringIdRepresentation(id));
}
if (idStrings.isEmpty()) {
return;
}
Query query = operations.idsQuery(idStrings);
executeAndRefresh((OperationsCallback<Void>) operations -> {
operations.delete(query, entityClass, getIndexCoordinates());
return null;
});
}
use of org.springframework.data.elasticsearch.core.query.Query in project spring-data-elasticsearch by spring-projects.
the class ReactiveSearchAfterIntegrationTests method shouldReadPagesWithSearchAfter.
// #1143
@Test
@DisplayName("should read pages with search_after")
void shouldReadPagesWithSearchAfter() {
List<Entity> entities = IntStream.rangeClosed(1, 10).mapToObj(i -> new Entity((long) i, "message " + i)).collect(Collectors.toList());
operations.saveAll(Mono.just(entities), Entity.class).blockLast();
Query query = Query.findAll();
query.setPageable(PageRequest.of(0, 3));
query.addSort(Sort.by(Sort.Direction.ASC, "id"));
List<Object> searchAfter = null;
List<Entity> foundEntities = new ArrayList<>();
int loop = 0;
do {
query.setSearchAfter(searchAfter);
List<SearchHit<Entity>> searchHits = operations.search(query, Entity.class).collectList().block();
if (searchHits.size() == 0) {
break;
}
foundEntities.addAll(searchHits.stream().map(searchHit -> searchHit.getContent()).collect(Collectors.toList()));
searchAfter = searchHits.get((int) (searchHits.size() - 1)).getSortValues();
if (++loop > 10) {
fail("loop not terminating");
}
} while (true);
assertThat(foundEntities).containsExactlyElementsOf(entities);
}
Aggregations