Skip to content

Commit

Permalink
Add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
puppylpg committed Jan 13, 2024
1 parent 6d5e7e4 commit 0ffeaa3
Showing 1 changed file with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,159 @@ would make an https://www.elastic.co/guide/en/elasticsearch/reference/current/qu
}
----
====

[[elasticsearch.query-methods.at-query.spel]]
=== Using SpEL Expressions

.Declare query on the method using the `@Query` annotation with SpEL expression.
====
https://docs.spring.io/spring-framework/reference/core/expressions.html[SpEL expression] is also supported when defining query in `@Query`.
[source,java]
----
interface BookRepository extends ElasticsearchRepository<Book, String> {
@Query("""
{
"bool":{
"must":[
{
"term":{
"name":"#{#name}"
}
}
]
}
}
""")
Page<Book> findByName(String name,Pageable pageable);
}
----
If for example the function is called with the parameter _John_, it would produce the following query body:
[source,json]
----
{
"bool":{
"must":[
{
"term":{
"name":"John"
}
}
]
}
}
----
====

.accessing entity property.
====
Suppose we have the following entity class as parameter:
[source,java]
----
public record QueryParam(String q) {
}
----
Now we can access the parameter entity by `#` symbol, then reference the property `q` with a simple `.`:
[source,java]
----
interface BookRepository extends ElasticsearchRepository<Book, String> {
@Query("""
{
"bool":{
"must":[
{
"term":{
"name":"#{#entity.q}"
}
}
]
}
}
""")
Page<Book> findByName(QueryParam entity,Pageable pageable);
}
----
We can pass `new QueryParam("John")` as the parameter now, and it will produce the same query string as before.
====

.accessing bean property.
====
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 `#`:
[source,java]
----
interface BookRepository extends ElasticsearchRepository<Book, String> {
@Query("""
{
"bool":{
"must":[
{
"term":{
"name":"#{@queryParam.q}"
}
}
]
}
}
""")
Page<Book> findByName(QueryParam entity,Pageable pageable);
}
----
====

.SpEL and `Collection` param.
====
`Collection` parameter is also supported and is as easy to use as normal `String`, such as the following `terms` query:
[source,java]
----
interface BookRepository extends ElasticsearchRepository<Book, String> {
@Query("""
{
"bool":{
"must":[
{
"terms":{
"name": #{#names}
}
}
]
}
}
""")
Page<Book> findByName(Collection<String> names,Pageable pageable);
}
----
====

.access property in the `Collection` param.
====
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`:
[source,java]
----
interface BookRepository extends ElasticsearchRepository<Book, String> {
@Query("""
{
"bool":{
"must":[
{
"terms":{
"name": #{#entities.![q]}
}
}
]
}
}
""")
Page<Book> findByName(Collection<QueryParam> entities,Pageable pageable);
}
----
This will extract all the `q` property values as a new `Collection` from `QueryParam` `Collection`.
====

0 comments on commit 0ffeaa3

Please sign in to comment.