Skip to content

Custom Query with string parameter which contains double quotes #1790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mike1994s opened this issue Apr 27, 2021 · 1 comment · Fixed by #1793
Closed

Custom Query with string parameter which contains double quotes #1790

mike1994s opened this issue Apr 27, 2021 · 1 comment · Fixed by #1793
Assignees
Labels
type: bug A general bug

Comments

@mike1994s
Copy link

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!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 27, 2021
@sothawo sothawo added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 27, 2021
@sothawo
Copy link
Collaborator

sothawo commented Apr 27, 2021

Thanks for reporting this bug. Of course the quotes should be escaped properly in the created Elasticsearch query

@sothawo sothawo self-assigned this Apr 27, 2021
@sothawo sothawo linked a pull request Apr 28, 2021 that will close this issue
@sothawo sothawo added this to the 4.3 M1 (2021.1.0) milestone Apr 28, 2021
sothawo added a commit that referenced this issue Apr 29, 2021
Original Pull Request #1793
Closes #1790

(cherry picked from commit f8fbf77)
sothawo added a commit that referenced this issue Apr 29, 2021
Original Pull Request #1793
Closes #1790

(cherry picked from commit f8fbf77)
(cherry picked from commit 40972b2)
sothawo added a commit that referenced this issue Apr 29, 2021
Original Pull Request #1793
Closes #1790

(cherry picked from commit f8fbf77)
(cherry picked from commit 40972b2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants