Skip to content

Add Query by Example feature #2418

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

Closed
ezequielantunez opened this issue Jan 2, 2023 · 2 comments · Fixed by #2422
Closed

Add Query by Example feature #2418

ezequielantunez opened this issue Jan 2, 2023 · 2 comments · Fixed by #2422
Labels
type: enhancement A general enhancement

Comments

@ezequielantunez
Copy link
Contributor

Hi spring-data-elasticsearch team.

I'd like to contribute to this project implementing Query by Example feature from Spring Data.

I found this feature extremely useful in the past. The use case is to avoid custom repositories method explosion for queries based on nullable fields filter models.

My initial approach is to create the following implementations:

  • public class QueryByExampleElasticsearchExecutor<T> implements QueryByExampleExecutor<T>
  • public class ReactiveQueryByExampleElasticsearchExecutor<T> implements ReactiveQueryByExampleExecutor<T>

and use them as fragments in repository factories.

I have an initial working version based on CriteriaQuery, for simplicity, but missing of the following features:

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 2, 2023
@sothawo
Copy link
Collaborator

sothawo commented Jan 4, 2023

Interesting topic, I never used or needed that so I never had a look at it, but it would be a nice addition.

I just had a look how Spring Data Mongodb uses this, but they have the luck that the mongo db itself supports an example query. For Spring Data Elasticsearch we'd need to build a query from the example, and I think that using a CriteriaQuery as you did would be the right way.

As for the regex match: You are right, this is not yet supported. This could be added either in this issue or as a separate issue to be done before that. Elasticsearch has a regexp term query that should be used for that.

Fluent query - just had a first look at it myself - is not directly related or a prerequisite to the query by example topic, rather something that can be used for that as well. Adding this would probably be a separate issue.

One thing I noticed about the QueryByExampleExecutor: This returns the simple entity types, in Spring Data Elasticsearch we mostly return the SearchHit(s)<T>, but that should be no big problem.

If you have code which you'd like to add, you're welcome to provide this as a PR, so I can have a look at that to see how it fits into the existing codebase.

@sothawo sothawo added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 4, 2023
ezequielantunez added a commit to ezequielantunez/spring-data-elasticsearch that referenced this issue Jan 5, 2023
Added Query by Example repositories fragments
Added Criteria.regexp
ezequielantunez added a commit to ezequielantunez/spring-data-elasticsearch that referenced this issue Jan 11, 2023
Fixed Criteria.regexp
Minor fixes in QueryByExample fragments and mapper
Improved test coverage
sothawo pushed a commit that referenced this issue Jan 11, 2023
Original Pull Request #2422
Closes #2418
@sothawo sothawo added this to the 5.1 M1 (2023.0.0) milestone Jan 11, 2023
@sothawo
Copy link
Collaborator

sothawo commented Jan 11, 2023

Thanks again, I rarely get such such nice written contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants