Skip to content

Commit e5555ea

Browse files
committed
update existing code for FTS in PostgreSQL
1 parent f4d5f08 commit e5555ea

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

spring-data-postgres-fulltext-search/src/main/java/de/rieckpil/blog/CommentController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class CommentController {
2323
@GetMapping
2424
public List<Comment> getCommentsContainingText(@RequestParam("q") String queryText) {
2525

26-
List<Comment> list = em.createQuery("SELECT c FROM Comment c where fts(c.commentText, :commentText) = true", Comment.class)
26+
List<Comment> list = em.createQuery("SELECT c FROM Comment c where fts(english, c.commentText, :commentText) = true", Comment.class)
2727
.setParameter("commentText", queryText).getResultList();
2828

2929
return list;

spring-data-postgres-fulltext-search/src/main/java/de/rieckpil/blog/PostgreSQLFTSFunction.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ public boolean hasParenthesesIfNoArguments() {
2626
return false;
2727
}
2828

29-
@SuppressWarnings("rawtypes")
3029
@Override
3130
public String render(Type type, List args, SessionFactoryImplementor factory) throws QueryException {
3231

33-
if (args == null || args.size() != 2) {
32+
if (args == null || args.size() != 3) {
3433
throw new IllegalArgumentException("The function must be passed 2 arguments");
3534
}
3635

37-
String field = (String) args.get(0);
38-
String searchString = (String) args.get(1);
39-
return field + " @@ to_tsquery(" + searchString + ")";
36+
String language = (String) args.get(0);
37+
String field = (String) args.get(1);
38+
String searchString = (String) args.get(2);
39+
return field + " @@ to_tsquery('" + language + "', " + searchString + ")";
4040
}
4141
}

spring-data-postgres-fulltext-search/src/main/resources/application.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ spring:
44
password: postgres
55
url: jdbc:postgresql://localhost:5432/postgres jpa: hibernate: ddl-auto: validate
66
properties: hibernate:
7-
dialect: de.rieckpil.blog.ExtendedPostgreSQLDialect
7+
dialect: de.rieckpil.blog.ExtendedPostgreSQLDialect show-sql: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
INSERT INTO comment (id, comment_text, created_at) VALUES (1, to_tsvector('english', 'Out believe has request not how comfort evident. Up delight cousins we feeling minutes. Genius has looked end piqued spring. Down has rose feel find man. Learning day desirous informed expenses material returned six the. She enabled invited exposed him another. Reasonably conviction solicitude me mr at discretion reasonable. Age out full gate bed day lose.') ,NOW());

0 commit comments

Comments
 (0)