Last active
October 5, 2017 09:28
-
-
Save mwean/f6f3f062bff10d230ab2 to your computer and use it in GitHub Desktop.
Code Snippets for Adventures in Searching with Postgres - Part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE INDEX index_description_on_icd_codes ON icd_codes USING gin(to_tsvector(description)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE INDEX index_description_on_icd_codes ON icd_codes USING gin(to_tsvector('english', description)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ SELECT to_tsvector('The quick brown fox jumped over the lazy dog') | |
@@ to_tsquery('laziness'); | |
?column? | |
---------- | |
t | |
(1 row) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * FROM icd_codes | |
WHERE to_tsvector(description || ' ' || another_column) @@ to_tsquery('quick&brown') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
description_col = arel_table[:description] | |
description_vector = Arel::Nodes::NamedFunction.new('to_tsvector', ['english', description_col]) | |
quoted_query_str = Arel::Nodes.build_quoted(query_str) | |
query_vector = Arel::Nodes::NamedFunction.new('to_tsquery', ['english', quoted_query_str]) | |
Arel::Nodes::InfixOperation.new('@@', description_vector, query_vector) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * FROM icd_codes | |
WHERE to_tsvector(description) @@ to_tsquery('quick&brown') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ EXPLAIN SELECT * FROM icd_codes | |
WHERE to_tsvector('english', description) @@ to_tsquery('english', 'vailable'); | |
QUERY PLAN | |
------------------------------------------------------------------------------------------------ | |
Bitmap Heap Scan on icd_codes (cost=14.13..936.63 rows=275 width=334) | |
Recheck Cond: (to_tsvector('english'::regconfig, description) @@ '''vailabl'''::tsquery) | |
-> Bitmap Index Scan on index_description_on_icd_codes (cost=0.00..14.06 rows=275 width=0) | |
Index Cond: (to_tsvector('english'::regconfig, description) @@ '''vailabl'''::tsquery) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ts_rank_cd = Arel::Nodes::NamedFunction.new('ts_rank_cd', [description_vector, query_vector]) | |
ICDCode.where(···).order(ts_rank_cd.desc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ SELECT to_tsvector('The quick brown fox jumped over the lazy dog'); | |
to_tsvector | |
------------------------------------------------------- | |
'brown':3 'dog':9 'fox':4 'jump':5 'lazi':8 'quick':2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ EXPLAIN SELECT * FROM icd_codes | |
WHERE to_tsvector('english', description) @@ to_tsquery('english', 'vailable&benzene'); | |
QUERY PLAN | |
------------------------------------------------------------------------------------------------------------- | |
Bitmap Heap Scan on icd_codes (cost=20.02..27.94 rows=2 width=334) | |
Recheck Cond: (to_tsvector('english'::regconfig, description) @@ '''vailabl'' & ''benzen'''::tsquery) | |
-> Bitmap Index Scan on index_description_on_icd_codes (cost=0.00..20.01 rows=2 width=0) | |
Index Cond: (to_tsvector('english'::regconfig, description) @@ '''vailabl'' & ''benzen'''::tsquery) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment