micronaut-projects / micronaut-data

Ahead of Time Data Repositories
Apache License 2.0
467 stars 197 forks source link

Criteria Builder Like operator do not work on MongoDb. #2411

Closed prashantcodecraft closed 1 year ago

prashantcodecraft commented 1 year ago

Expected Behavior

As per documented on : micronaut pages , I have created an implementation of PredicateSpecification. The purpose of the implementation is to add criteria to query dynamically.

public Predicate toPredicate(Root<Articles> root, CriteriaBuilder criteriaBuilder) {
        List<Predicate> predicates = new ArrayList<>();
        if (searchText != null && !searchText.isEmpty()) {
            String searchParameter = ".*" + searchText.trim() + "*.";
            Predicate predicateForBody = criteriaBuilder.like((root.get("documentText")), searchParameter);
            Predicate predicateForTitle = criteriaBuilder.like((root.get("title")), searchParameter);
            predicates.add(criteriaBuilder.or(predicateForBody, predicateForTitle));
        }
        if (status != null && !status.isEmpty()) {
            predicates.add(criteriaBuilder.equal(root.get("status"), StatusEnum.fromValue(status)));
        }
        return criteriaBuilder.and(predicates.toArray(new Predicate[]{}));
    }

I expect the query to work like contain, just the way it works if I use direct JPA query.

findAllByTenantIdAndParentIdIsNullAndTitleContainsOrDocumentTextContains

Actual Behaviour

Mongo throws an error :

02:19:14.325 [default-nioEventLoopGroup-1-3] ERROR i.m.http.server.RouteExecutor - Unexpected error occurred: Queries of type Like are not supported by this implementation
java.lang.IllegalArgumentException: Queries of type Like are not supported by this implementation
    at io.micronaut.data.document.model.query.builder.MongoQueryBuilder.handleCriterion(MongoQueryBuilder.java:909)

The query generated is have following :

{"documentText": {"$eq": ".*Demo*."}}

Where as I want it to be

{title : {$regex : "Demo"}}

Steps To Reproduce

  1. Create a mongo db micronaut project from micronaut lancher.
  2. Connect to mongo database.
  3. Implement the interface as follows
import static jakarta.persistence.criteria.*;

public interface PredicateSpecification<T> {

    (1)
    @Nullable
    Predicate toPredicate(@NonNull Root<T> root, (2)
                          @NonNull CriteriaBuilder criteriaBuilder (3)
    );

}

Environment Information

Example Application

No response

Version

3.9.4

radovanradic commented 1 year ago

Should be doable in next micronaut-data patch release

prashantcodecraft commented 1 year ago

Thank you 👍 💯

prashantcodecraft commented 1 year ago

@radovanradic : I am trying to upgrade my micronaut application from 3.9.4 to 4.x due to above issue, but facing another issue : https://github.com/micronaut-projects/micronaut-maven-plugin/issues/805 . Can you please suggest what wrong with this ?

radovanradic commented 1 year ago

Sorry, not very familiar with that but I believe team working on it will handle it.

prashantcodecraft commented 1 year ago

@radovanradic : It looks the fix for like query is in master and not in 4.0.2 . So probably will be coming in 4.0.3.

image

radovanradic commented 1 year ago

Right, the fix is not released yet and should be in 4.0.3.

prashantcodecraft commented 1 year ago

@radovanradic : Is there any possibility to add this fix to Micronaut 3.10.x /3.9.x version ? basically we are still at 3.9.4 and upgrading to 4.0.x is giving us issue mainly around build custom AWS labda, hence to keep us going this would help.