Skip to content

Commit 0ffeaa3

Browse files
committed
Add doc
1 parent 6d5e7e4 commit 0ffeaa3

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

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

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,159 @@ 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+
Suppose we have the following entity class as parameter:
414+
[source,java]
415+
----
416+
public record QueryParam(String q) {
417+
}
418+
----
419+
420+
Now we can 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 before.
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+
====
493+
494+
.access property in the `Collection` param.
495+
====
496+
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`:
497+
498+
[source,java]
499+
----
500+
interface BookRepository extends ElasticsearchRepository<Book, String> {
501+
@Query("""
502+
{
503+
"bool":{
504+
"must":[
505+
{
506+
"terms":{
507+
"name": #{#entities.![q]}
508+
}
509+
}
510+
]
511+
}
512+
}
513+
""")
514+
Page<Book> findByName(Collection<QueryParam> entities,Pageable pageable);
515+
}
516+
----
517+
This will extract all the `q` property values as a new `Collection` from `QueryParam` `Collection`.
518+
====
519+

0 commit comments

Comments
 (0)