Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add slice query support in string based cosmos query #25011

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Expand Down Expand Up @@ -150,6 +151,21 @@ public void testAnnotatedQueryWithJsonNodeAsPage() {
assertAddressPostalCodes(actualPostalCodes, addresses);
}

@Test
public void testAnnotatedQueryWithJsonNodeAsSlice() {
final List<Address> addresses = Arrays.asList(Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS2_PARTITION1);
addressRepository.saveAll(addresses);

final PageRequest cosmosPageRequest = CosmosPageRequest.of(0, 10);
final Slice<JsonNode> postalCodes = addressRepository.annotatedFindPostalCodesByCityAsSlice(TestConstants.CITY,
cosmosPageRequest);
final List<String> actualPostalCodes = postalCodes.getContent()
.stream()
.map(jsonNode -> jsonNode.get("postalCode").asText())
.collect(Collectors.toList());
assertAddressPostalCodes(actualPostalCodes, addresses);
}

private void assertAddressPostalCodes(List<String> postalCodes, List<Address> expectedResults) {
List<String> expectedPostalCodes = expectedResults.stream()
.map(Address::getPostalCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
Expand Down Expand Up @@ -47,6 +48,9 @@ public interface AddressRepository extends CosmosRepository<Address, String> {
@Query("select DISTINCT a.postalCode from a where a.city = @city")
Page<JsonNode> annotatedFindPostalCodesByCity(@Param("city") String city, Pageable pageable);

@Query("select DISTINCT a.postalCode from a where a.city = @city")
Slice<JsonNode> annotatedFindPostalCodesByCityAsSlice(@Param("city") String city, Pageable pageable);

@Query("select DISTINCT value a.postalCode from a where a.city = @city")
List<String> annotatedFindPostalCodeValuesByCity(@Param("city") String city);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public Object execute(final Object[] parameters) {
if (isPageQuery()) {
return this.operations.runPaginationQuery(querySpec, accessor.getPageable(), processor.getReturnedType().getDomainType(),
processor.getReturnedType().getReturnedType());
} else if (isSliceQuery()) {
return this.operations.runSliceQuery(
querySpec,
accessor.getPageable(),
processor.getReturnedType().getDomainType(),
processor.getReturnedType().getReturnedType());
} else if (isCountQuery()) {
final String container = ((CosmosEntityMetadata<?>) getQueryMethod().getEntityInformation()).getContainerName();
return this.operations.count(querySpec, container);
Expand Down