public class Post {
private int id;
private String title;
private String content;
private Date createdAt;
}
content field is the text field. This field value actually is a Json object, I converted it to text.
How can search efficient way a text pattern match or not?
I want to implement using spring boot, JPA and postgresSQL
3
Answers
You can use FTS on postgres.
https://www.postgresql.org/docs/current/textsearch.html
fts will provide searching similar to search engines but you need to configure it as per your need. it will be much faster than like/ilike ‘%query%’.
If I understand your question correctly you have a JPA repository already.
All you need to do is add a method and add the JPQL as below.
Then in your service:
If
content
is JSON, why not use native Postgresjson
(orjsonb
) type for that field, and then search inside it using native JSON operators.This should allow you to request on the actual value, instead of doing clunky text search on a
String
(and potentially even selecting unexpected data).e.g.: if you want to request an ID in a
jsonb
field, you would do something like: