英文:
spring jdbc prepared statement with boolean fulltext search formatting issues
问题
我有一个使用布尔全文搜索的SQL查询:
SELECT
*,
MATCH (job_title) AGAINST ('"product manager"' in boolean mode) AS title_relevance_exact,
MATCH (job_description) AGAINST ('"product manager"' in boolean mode) AS description_relevance_exact,
MATCH (job_title) AGAINST ('product manager' in boolean mode) AS title_relevance_part,
MATCH (job_description) AGAINST ('product manager' in boolean mode) AS description_relevance_part
FROM
jobs
WHERE
MATCH(job_title, job_description) AGAINST ('product manager') AND
date_posted >= now() - INTERVAL 30 DAY
ORDER BY
job_title LIKE 'product manager' DESC,
title_relevance_exact DESC,
description_relevance_exact DESC,
title_relevance_part DESC,
description_relevance_part DESC
LIMIT 300;
即在`'"product manager"'`中的引号是查询中的重要部分。
当我将此查询作为Spring JDBC预准备语句运行时,我必须进行一些处理,以便正确格式化字符串,因为如果我使用`'?'`,JDBC不会将其识别为参数:
String queryPrepared =
"SELECT\n" +
" *,\n" +
" MATCH (job_title) AGAINST (? in boolean mode) AS title_relevance_exact,\n" +
" MATCH (job_description) AGAINST (? in boolean mode) AS description_relevance_exact,\n" +
" MATCH (job_title) AGAINST (? in boolean mode) AS title_relevance_part,\n" +
" MATCH (job_description) AGAINST (? in boolean mode) AS description_relevance_part\n" +
"FROM\n" +
" jobs\n" +
"WHERE\n" +
" MATCH(job_title, job_description) AGAINST (?) AND\n" +
" date_posted >= now() - INTERVAL 30 DAY\n" +
"ORDER BY\n" +
" job_title LIKE ? DESC,\n" +
" title_relevance_exact DESC,\n" +
" description_relevance_exact DESC,\n" +
" title_relevance_part DESC,\n" +
" description_relevance_part DESC\n" +
"LIMIT 500;";
String param1 = String.format("'%1$s'", searchValue);
String param2 = String.format("'%1$s'", searchValue);
List<Map<String, Object>> resultPrepared = jdbcTemplate.queryForList(queryPrepared,
new Object[] {param1, param1, param2, param2, param2, param2});
以上是您提供的内容的翻译部分。如果您需要更多的帮助或有其他问题,请随时问我。
英文:
I have an sql query that uses boolean fulltext search:
SELECT
*,
MATCH (job_title) AGAINST ('"product manager"' in boolean mode) AS title_relevance_exact,
MATCH (job_description) AGAINST ('"product manager"' in boolean mode) AS description_relevance_exact,
MATCH (job_title) AGAINST ('product manager' in boolean mode) AS title_relevance_part,
MATCH (job_description) AGAINST ('product manager' in boolean mode) AS description_relevance_part
FROM
jobs
WHERE
MATCH(job_title, job_description) AGAINST ('product manager') AND
date_posted >= now() - INTERVAL 30 DAY
ORDER BY
job_title LIKE 'product manager' DESC,
title_relevance_exact DESC,
description_relevance_exact DESC,
title_relevance_part DESC,
description_relevance_part DESC
LIMIT 300;
i.e. the quotation and speech marks in '"product manager"'
is important to the query.
When I run this query as a spring jdbc prepared statement, I have to do some work arounds to get the string formatted correctly since if I have '?'
, jdbc doesnt recognise this as a parameter:
String queryPrepared =
"SELECT\n" +
" *,\n" +
" MATCH (job_title) AGAINST (? in boolean mode) AS title_relevance_exact,\n" +
" MATCH (job_description) AGAINST (? in boolean mode) AS description_relevance_exact,\n" +
" MATCH (job_title) AGAINST (? in boolean mode) AS title_relevance_part,\n" +
" MATCH (job_description) AGAINST (? in boolean mode) AS description_relevance_part\n" +
"FROM\n" +
" jobs\n" +
"WHERE\n" +
" MATCH(job_title, job_description) AGAINST (?) AND\n" +
" date_posted >= now() - INTERVAL 30 DAY\n" +
"ORDER BY\n" +
" job_title LIKE ? DESC,\n" +
" title_relevance_exact DESC,\n" +
" description_relevance_exact DESC,\n" +
" title_relevance_part DESC,\n" +
" description_relevance_part DESC\n" +
"LIMIT 500;";
String param1 = String.format("'\"%1$s\"'", searchValue);
String param2 = String.format("'%1$s'", searchValue);
List<Map<String, Object>> resultPrepared = jdbcTemplate.queryForList(queryPrepared,
new Object[] {param1, param1, param2, param2, param2, param2});
But the correct result isnt produced from the query, the results are not in the expected order. I can only assume that this happens because of the formatting in the prepared statement parameters. But I can't see what the query looks like after adding the parameters, how can I debug this?
答案1
得分: 1
你需要从你的值中移除单引号。在SQL中,单引号用于界定查询字符串中的字符串字面量。当你设置参数值时,只需要设置字符串值而无需使用单引号。
因此,如果你想要用参数替换字面量 '"product manager"'
,你在查询中使用 ?
(因此不使用单引号),并设置值为 "product manager"
(同样,不使用单引号)。作为Java字符串字面量,它将是 "\"product manager\""
。
在作为参数传递的值中使用单引号,相当于使用SQL字符串字面量 '''"product manager"'''
。
英文:
You need to remove the single quotes from your value. Single quotes in SQL delimit a string literal in the query string. When you set a parameter value, you only need to set the string value without the single quotes.
So, if you want to replace the literal '"product manager"'
with a parameter, then you use ?
in your query (so, without single quotes), and set the value "product manager"
(again, without single quotes), which as a Java string literal is "\"product manager\""
.
Using single quotes in the value passed as a parameter would be the equivalent of using the SQL String literal '''"product manager"'''
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论