Skip to content

feature: Provide a toEntityList from a set of loaded IDs  #539

Open
@SylvainAssemat

Description

@SylvainAssemat

Hi all

I have a question / use case about the load() query usign some MetamodelField

My goal is to have a model (RDUser in my case) with limited attribute (ID only as shown below)

Is this somtehing possible to have a kind of conversion based on MetamodelField in entry, and a List at the end ?

This work

            SearchStream<RDUser> searchStream = entityStream.of(RDUser.class);
            List<Single<String>> list = searchStream
                    //.filter(predicate)
                    //.sorted(comparator)
                    //.skip(pageable.getPageNumber() * pageable.getPageSize())
                    //.limit(pageable.getPageSize())
                    .load(RDUser$.ID)
                    .toList(String.class);

This is not working

  SearchStream<RDUser> searchStream = entityStream.of(RDUser.class);
            Page<RDUser> list = searchStream
                    //.filter(predicate)
                    //.sorted(comparator)
                    //.skip(pageable.getPageNumber() * pageable.getPageSize())
                    //.limit(pageable.getPageSize())
                    .load(RDUser$.ID)
                    .toList(PageRequest.ofSize(50000), RDUser.class);

Exception :

log.info("-> " + list.getContent());

java.lang.NullPointerException: Cannot invoke "Object.toString()" because the return value of "java.util.Map.get(Object)" is null
 AggregationStream<RDUser> aggregationStream = searchStream
                    //.filter(predicate)
                    //.sorted(comparator)
                    //.skip(pageable.getPageNumber() * pageable.getPageSize())
                    //.limit(pageable.getPageSize())
                    .load(RDUser$.ID);

            aggregationStream.aggregate().getResults().stream().forEach(System.out::println);

{id=020cc931-954b-4b42-83c3-49489945ca72}
{id=031dd63d-87c1-4c17-9552-af8d38a4630e}
{id=1780534c-d47b-473c-be92-37dd1342c001}
{id=1f0d13d7-452d-442e-8a7e-5bf384dde960}
{id=2e833aff-0734-4b69-9000-d0e3400ebedd}
{id=46bc80af-ef55-46dd-b890-4e78ae9e0c46}
{id=48edcd36-df7e-44c2-9521-6824104f4e51}
{id=52837167-894a-4542-8468-2f1f97aa857f}
{id=71b478c8-044a-4eb7-8445-0f24d175e209}
{id=83e5e31d-234b-4fb9-b17f-d58f28c5b22f}
{id=9962907a-ec4c-41cc-9902-810d94f3bd32}
{id=ae8e15a2-5940-4dcc-8815-5695d4f8e77f}

This function seems to be the culprit

 @SuppressWarnings("unchecked")
  List<E> toEntityList(AggregationResult aggregationResult) {
    if (isDocument) {
      return aggregationResult.getResults().stream().map(d -> gson.fromJson(d.get("$").toString(), entityClass))
          .toList();
    } else {
      return aggregationResult.getResults().stream()
          .map(h -> (E) ObjectUtils.mapToObject(h, entityClass, mappingConverter)).toList();
    }
  }

Workaround :

//https://github.com/redis/redis-om-spring/issues/474
// @Autowired
// @Qualifier("omGsonBuilder")
// private GsonBuilder gsonBuilder;
Gson gson = gsonBuilder.create(); //Not use jackson

List<RDUser> listRDUser = aggregationStream.aggregate().getResults().stream().map(d -> gson.fromJson(d.toString(), RDUser.class)).toList();

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions