We can write a custom query method to get components by name & version.
private List<Component> getAllMatchingComponents(Repository repository, String name, Map<String, String> formatAttributes) {
Preconditions.checkNotNull(repository);
Preconditions.checkNotNull(name);
Preconditions.checkNotNull(formatAttributes);
List<Component> filteredComponents;
try (StorageTx storageTx = repository.facet(StorageFacet.class).txSupplier().get()) {
storageTx.begin();
Query.Builder query = Query.builder().where("name").eq(name).suffix("order by version desc");
Iterable<Component> unfilteredComponents = storageTx.findComponents(query.build(), Collections.singletonList(repository));
Stream<Component> filteredStream = StreamSupport.stream(unfilteredComponents.spliterator(), false).filter(
component -> formatAttributes.entrySet().stream().allMatch(
entry -> Objects.equals(entry.getValue(), component.version())));
filteredComponents = ImmutableList.copyOf(filteredStream.iterator());
}
return filteredComponents;
}