Reputation: 1
I'm trying to integrate an existing legacy application to Elasticsearch using Spring Data Elasticsearch (version 5.3.0). I wrote a repository like this:
public interface IndexableUserRepository extends ElasticsearchRepository<IndexableUser, Integer> {
@Query(
"""
{
"bool": {
"filter": [
#{#user.hasNeverPrintedUserCard ? '' :
'{
"term": {
"hasPrintedUserCard": false
}
},'
}
{
"term": {
"isInactive": #{#user.isInactive}
}
}
]
}
}
""")
Page<IndexableUser> findByQueryWithName(
IndexableUserQueryParameters user, PageRequest pageRequest);
}
(I'll omit the definition of IndexableUserQueryParameters
and IndexableUser
for the sake of brevity.)
I want to include a term query conditionally on the hasPrintedUserCard
property of the record IndexableUserQueryParameters
being false
. In other words,
hasPrintedUserCard
is false
, include the term query to only include the documents that have the hasPrintedUserCard
field set to false
.hasPrintedUserCard
is true
don't include the term query, so include documents regardless of the value of hasPrintedUserCard
.However, the double quotes around "term"
and "hasPrintedUserCard"
are being escaped, which causes a syntax error for Elasticsearch.
I decoded the wrapped query value and got this
{
"bool": {
"filter": [
{
\"term\": {
\"hasPrintedUserCard\": false
}
},
{
"term": {
"isInactive": false
}
}
]
}
}
Is that the expected behavior for SpEl? Or is it an issue with how the @Query annotation works? Is there a way to implement that conditional inclusion of a term query using @Query + @SpEl?
I tried removing the single quotes from around the term query, but it then gave me an error detected by Spring itself about SpEl syntax.
Upvotes: 0
Views: 22