Skip to content

Commit d205d25

Browse files
committed
Add doc
1 parent 7105c29 commit d205d25

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

src/main/antora/modules/ROOT/pages/elasticsearch/repositories/elasticsearch-repository-queries.adoc

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,174 @@ would make an https://www.elastic.co/guide/en/elasticsearch/reference/current/qu
361361
}
362362
----
363363
====
364+
365+
[[elasticsearch.query-methods.at-query.spel]]
366+
=== Using SpEL Expressions
367+
368+
.Declare query on the method using the `@Query` annotation with SpEL expression.
369+
====
370+
https://docs.spring.io/spring-framework/reference/core/expressions.html[SpEL expression] is also supported when defining query in `@Query`.
371+
372+
373+
[source,java]
374+
----
375+
interface BookRepository extends ElasticsearchRepository<Book, String> {
376+
@Query("""
377+
{
378+
"bool":{
379+
"must":[
380+
{
381+
"term":{
382+
"name":"#{#name}"
383+
}
384+
}
385+
]
386+
}
387+
}
388+
""")
389+
Page<Book> findByName(String name,Pageable pageable);
390+
}
391+
----
392+
393+
If for example the function is called with the parameter _John_, it would produce the following query body:
394+
395+
[source,json]
396+
----
397+
{
398+
"bool":{
399+
"must":[
400+
{
401+
"term":{
402+
"name":"John"
403+
}
404+
}
405+
]
406+
}
407+
}
408+
----
409+
====
410+
411+
.accessing entity property.
412+
====
413+
Supposing that we have the following entity class as query parameter type:
414+
[source,java]
415+
----
416+
public record QueryParam(String q) {
417+
}
418+
----
419+
420+
It's easy to access the parameter entity by `#` symbol, then reference the property `q` with a simple `.`:
421+
422+
[source,java]
423+
----
424+
interface BookRepository extends ElasticsearchRepository<Book, String> {
425+
@Query("""
426+
{
427+
"bool":{
428+
"must":[
429+
{
430+
"term":{
431+
"name":"#{#entity.q}"
432+
}
433+
}
434+
]
435+
}
436+
}
437+
""")
438+
Page<Book> findByName(QueryParam entity,Pageable pageable);
439+
}
440+
----
441+
442+
We can pass `new QueryParam("John")` as the parameter now, and it will produce the same query string as above.
443+
====
444+
445+
.accessing bean property.
446+
====
447+
https://docs.spring.io/spring-framework/reference/core/expressions/language-ref/bean-references.html[Bean property] is also supported as entity property. Given that there is a bean named `queryParam` of type `QueryParam`, we can access the bean with symbol `@` rather than `#`:
448+
[source,java]
449+
----
450+
interface BookRepository extends ElasticsearchRepository<Book, String> {
451+
@Query("""
452+
{
453+
"bool":{
454+
"must":[
455+
{
456+
"term":{
457+
"name":"#{@queryParam.q}"
458+
}
459+
}
460+
]
461+
}
462+
}
463+
""")
464+
Page<Book> findByName(QueryParam entity,Pageable pageable);
465+
}
466+
----
467+
====
468+
469+
.SpEL and `Collection` param.
470+
====
471+
`Collection` parameter is also supported and is as easy to use as normal `String`, such as the following `terms` query:
472+
473+
[source,java]
474+
----
475+
interface BookRepository extends ElasticsearchRepository<Book, String> {
476+
@Query("""
477+
{
478+
"bool":{
479+
"must":[
480+
{
481+
"terms":{
482+
"name": #{#names}
483+
}
484+
}
485+
]
486+
}
487+
}
488+
""")
489+
Page<Book> findByName(Collection<String> names,Pageable pageable);
490+
}
491+
----
492+
A collection of `names` like `List.of("name1", "name2")` will produce the following terms query:
493+
[source,json]
494+
----
495+
{
496+
"bool":{
497+
"must":[
498+
{
499+
"terms":{
500+
"name": ["name1", "name2"]
501+
}
502+
}
503+
]
504+
}
505+
}
506+
----
507+
====
508+
509+
.access property in the `Collection` param.
510+
====
511+
https://docs.spring.io/spring-framework/reference/core/expressions/language-ref/collection-projection.html[SpEL Collection Projection] is convenient to use when entities in the `Collection` parameter is not plain `String`:
512+
513+
[source,java]
514+
----
515+
interface BookRepository extends ElasticsearchRepository<Book, String> {
516+
@Query("""
517+
{
518+
"bool":{
519+
"must":[
520+
{
521+
"terms":{
522+
"name": #{#entities.![q]}
523+
}
524+
}
525+
]
526+
}
527+
}
528+
""")
529+
Page<Book> findByName(Collection<QueryParam> entities,Pageable pageable);
530+
}
531+
----
532+
This will extract all the `q` property values as a new `Collection` from `QueryParam` collection, thus takes the same effect as above.
533+
====
534+

0 commit comments

Comments
 (0)