How different is GIN based full text search compared to BM25. We're using Postgres to…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
How different is GIN based full text search compared to BM25. We're using Postgres to store legal cases and want to allow users to fetch most relevant cases by explaining their case. which one should we use?
Assistant
Full-text search in PostgreSQL can be accomplished using GIN (Generalized Inverted Index) or with the BM25 (Best Match 2) algorithm. Both methods have their own strengths and are suitable for different use cases.
- User
So how can we use BM25 with our existing postgres database?
Assistant
To use BM25 with your existing PostgreSQL database, you can follow these steps:
- Install the pg_trgm extension:
sqlCREATE EXTENSION pg_trgm;- Create a GIN or BRIN index on the case text data field:
sqlCREATE INDEX idx_case_text ON legal_cases USING GIN (case_text gin_trgm_ops);Replace 'case_text' with the appropriate column name from your table.
- Use the ts_rank_cd function to rank the search results based on relevance.
sqlSELECT *, ts_rank_cd(to_tsvector('english', case_text), to_tsquery('english', 'case_text')) AS rank FROM legal_cases WHERE to_tsvector('english', case_text) @@ to_tsquery('english', 'case_text');