Skip to content

Custom Query with string parameter which contains double quotes #1790

Closed
@mike1994s

Description

@mike1994s

Hello!
I have trouble with custom query in my repository, in order to reproduce my problem let's imagine that we have repository with books,

@Repository public interface BookRepository extends ElasticsearchRepository<BookDto, String> { @Query("{\"bool\":{\"must\": [{\"match\": {\"year\" : ?0}},{\"prefix\": {\"title\" : \"?1\"}}]}}") List<BookDto> findBookByYearAndPrefixTitle(final String year, final String title); }

So, this request speak for itself: I want to get all books published in defined year, by prefix.
And it works fine until title does not contain double quotes. For example if I want to get book published in 2000 year and with title : Name "name in quote" name .
Then I have got error
[type=x_content_parse_exception, reason=[1:81] [bool] failed to parse field [must]]; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=x_content_parse_exception, reason=[1:81] [bool] failed to parse field [must]]]; nested: ElasticsearchException[Elasticsearch exception [type=json_parse_exception, reason=Unexpected character ('1' (code 49)): was expecting comma to separate Object entries

As workaround solution for problem, before request I have prepared string like
return StringUtils.replace(value, "\"", "\\\\\"");

It doesn't seem like a good solution. I wish the framework would do this kind of thing, like jdbc does.
And I am not alone with a such problem, for example - Question on SO

Also, maybe useful link - Elastic forum
Dependency in my project -
org.springframework.boot:spring-boot-starter-actuator:jar:2.4.3:compile org.springframework.boot:spring-boot-starter-data-elasticsearch:jar:2.4.3:compile

Thank You for attention!
Have a nice day!

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions