SlideShare a Scribd company logo
1 of 19
Download to read offline
PostgreSQL Full-text Search
in Django
Paweł Kowalski
● What is full-text search
● How it works in PostgreSQL
○ search
○ ranking
● How to use it in Django
● Questions
Agenda
Full text search refers to techniques for searching a
single computer-stored document or a collection in a
full text database.
https://en.wikipedia.org/wiki/Full_text_search
WHAT IS FULL TEXT SEARCH
SELECT *
FROM table
WHERE Col1 LIKE '%query%';
WHAT IS FULL TEXT SEARCH
SELECT *
FROM table
WHERE Col1 LIKE '%query%';
WHAT IS FULL TEXT SEARCH
SLOW, EXPENSIVE,
NO ORDERING BY RELEVANCE
● LIKE ‘%query’ can’t use index
● Col1 can be very long (eg. entire book)
SELECT to_tsvector(
'english',
'Try not to become a man of success, but rather try to become a man of
value'
);
to_tsvector
----------------------------------------------------------------------
'becom':4,13 'man':6,15 'rather':10 'success':8 'tri':1,11 'valu':17
(1 row)
HOW IT WORKS IN POSTGRESQL
PostgreSQL, please help!
TSVECTOR
Since PostgreSQL 8.3
select to_tsvector('If you can dream it, you can do it') @@ 'dream';
?column?
----------
t
(1 row)
select to_tsvector('It''s kind of fun to do the impossible') @@ 'impossible';
?column?
----------
f
(1 row)
HOW IT WORKS IN POSTGRESQL
Search Operator: @@
SELECT 'dream'::tsquery, to_tsquery('dream');
tsquery | to_tsquery
--------------+------------
'dream' | 'dream'
(1 row)
SELECT 'impossible'::tsquery, to_tsquery('impossible');
tsquery | to_tsquery
--------------+------------
'impossible' | 'imposs'
(1 row)
HOW IT WORKS IN POSTGRESQL
TO_TSQUERY function
SELECT to_tsvector('It''s kind of fun to do the impossible') @@ to_tsquery
('impossible');
?column?
----------
t
(1 row)
HOW IT WORKS IN POSTGRESQL
TO_TSQUERY function
SELECT to_tsvector('If the facts don't fit the theory, change the facts') @@
to_tsquery('! fact');
SELECT to_tsvector('If the facts don''t fit the theory, change the facts') @@
to_tsquery('theory & !fact');
SELECT to_tsvector('If the facts don''t fit the theory, change the facts.') @@
to_tsquery('fiction | theory');
HOW IT WORKS IN POSTGRESQL
Query Operators: ! & |
SELECT COUNT(*) FROM ticketing_event WHERE name ILIKE '%madonna%rebel%heart%
tour%';
Time: 78,083 ms
HOW IT WORKS IN POSTGRESQL
Some numbers
SELECT COUNT(*) FROM ticketing_event WHERE search_vector @@ 'madonna & rebel &
heart & tour'::tsquery;
Time: 30,065 ms
SELECT COUNT(*) FROM ticketing_event;
count
-------
68889
Time: 11,440 ms
SELECT post.id, setweight(to_tsvector(post.title), ‘A’) ||
setweight(to_tsvector(post.content), ‘B’) AS vector1
FROM post
WHERE vector1 @@ to_tsquery(‘Michael & Jackson’)
ORDER BY ts_rank(vector1, to_tsquery(‘Michael & Jackson’));
HOW IT WORKS IN POSTGRESQL
Ranking:
SETWEIGHT, TS_RANK functions
SELECT ts_rank(to_tsvector('This is an example of document'),
to_tsquery('example')) as relevancy;
relevancy
-----------
0.0607927
(1 row)
SELECT ts_rank(to_tsvector('This is an example of document'),
to_tsquery('example | unknown')) as relevancy;
relevancy
-----------
0.0303964
(1 row)
HOW IT WORKS IN POSTGRESQL
Ranking:
SETWEIGHT, TS_RANK functions
HOW TO USE IT IN DJANGO
● django-pg-fts
● djorm-ext-pgfulltext
HOW TO USE IT IN DJANGO
● django-pg-fts
● djorm-ext-pgfulltext
[WIP] Refs #3254 -- Add Full Text Search to contrib.postgres
Post.objects.annotate(
search=SearchVector('title')
+ SearchVector('content'),
).filter(search='Michael Jackson')
HOW IT WORKS IN POSTGRESQL
SearchVector model field
Post.objects.filter(title__search='Michael Jackson')
HOW IT WORKS IN POSTGRESQL
SearchVector model field (stored)
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
search_vector = SearchVectorField()
Post.objects.filter(search_vector='Michael Jackson')
vector = SearchVector('title', weight=’A’) + SearchVector('content', weight=’B’)
post.search_vector = vector
post.save()
Update SearchVector field in post_save signal
HOW IT WORKS IN POSTGRESQL
django.contrib.postgres.search.SearchRank
queryset = Post.objects.annotate(
rank=SearchRank(
models.F('search_vector'),
SearchQuery('Michael Jackson')
),
)
queryset.filter(rank__gt=0.5).order_by('-rank')
QuestionTime

More Related Content

More from STX Next

Scrum Master - Breakout session.
Scrum Master - Breakout session.Scrum Master - Breakout session.
Scrum Master - Breakout session.STX Next
 
Transformation to agile.
Transformation to agile.Transformation to agile.
Transformation to agile.STX Next
 
What scrum masters and product owners should know about software quality and ...
What scrum masters and product owners should know about software quality and ...What scrum masters and product owners should know about software quality and ...
What scrum masters and product owners should know about software quality and ...STX Next
 
The essence hidden from the eye.
The essence hidden from the eye. The essence hidden from the eye.
The essence hidden from the eye. STX Next
 
Why to nearshore in Central Europe?
Why to nearshore in Central Europe?Why to nearshore in Central Europe?
Why to nearshore in Central Europe?STX Next
 
Is there a common pattern in fixing projects?
Is there a common pattern in fixing projects?Is there a common pattern in fixing projects?
Is there a common pattern in fixing projects?STX Next
 
Behave automatically: (Almost) Effortless feature testing
Behave automatically: (Almost) Effortless feature testingBehave automatically: (Almost) Effortless feature testing
Behave automatically: (Almost) Effortless feature testingSTX Next
 
Time to React!
Time to React!Time to React!
Time to React!STX Next
 
Salary Formula - bumpy road to transparency.
Salary Formula - bumpy road to transparency.Salary Formula - bumpy road to transparency.
Salary Formula - bumpy road to transparency.STX Next
 
Software Quality Visualization
Software Quality Visualization Software Quality Visualization
Software Quality Visualization STX Next
 
Kotlin Advanced - language reference for Android developers
Kotlin Advanced - language reference for Android developers Kotlin Advanced - language reference for Android developers
Kotlin Advanced - language reference for Android developers STX Next
 
Discover, Define, Deliver - a workflow to create successful digital products.
Discover, Define, Deliver - a workflow to create successful digital products. Discover, Define, Deliver - a workflow to create successful digital products.
Discover, Define, Deliver - a workflow to create successful digital products. STX Next
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016STX Next
 
Zwinność procesu rekrutacyjnego w branży IT
Zwinność procesu rekrutacyjnego w branży ITZwinność procesu rekrutacyjnego w branży IT
Zwinność procesu rekrutacyjnego w branży ITSTX Next
 
STX Next - Scrum Development Process Overview
STX Next - Scrum Development Process OverviewSTX Next - Scrum Development Process Overview
STX Next - Scrum Development Process OverviewSTX Next
 
STX Next - Meet Us
STX Next - Meet UsSTX Next - Meet Us
STX Next - Meet UsSTX Next
 
Group Process by Example - a PO’s and SM’s perspective
Group Process by Example - a PO’s and SM’s perspectiveGroup Process by Example - a PO’s and SM’s perspective
Group Process by Example - a PO’s and SM’s perspectiveSTX Next
 

More from STX Next (17)

Scrum Master - Breakout session.
Scrum Master - Breakout session.Scrum Master - Breakout session.
Scrum Master - Breakout session.
 
Transformation to agile.
Transformation to agile.Transformation to agile.
Transformation to agile.
 
What scrum masters and product owners should know about software quality and ...
What scrum masters and product owners should know about software quality and ...What scrum masters and product owners should know about software quality and ...
What scrum masters and product owners should know about software quality and ...
 
The essence hidden from the eye.
The essence hidden from the eye. The essence hidden from the eye.
The essence hidden from the eye.
 
Why to nearshore in Central Europe?
Why to nearshore in Central Europe?Why to nearshore in Central Europe?
Why to nearshore in Central Europe?
 
Is there a common pattern in fixing projects?
Is there a common pattern in fixing projects?Is there a common pattern in fixing projects?
Is there a common pattern in fixing projects?
 
Behave automatically: (Almost) Effortless feature testing
Behave automatically: (Almost) Effortless feature testingBehave automatically: (Almost) Effortless feature testing
Behave automatically: (Almost) Effortless feature testing
 
Time to React!
Time to React!Time to React!
Time to React!
 
Salary Formula - bumpy road to transparency.
Salary Formula - bumpy road to transparency.Salary Formula - bumpy road to transparency.
Salary Formula - bumpy road to transparency.
 
Software Quality Visualization
Software Quality Visualization Software Quality Visualization
Software Quality Visualization
 
Kotlin Advanced - language reference for Android developers
Kotlin Advanced - language reference for Android developers Kotlin Advanced - language reference for Android developers
Kotlin Advanced - language reference for Android developers
 
Discover, Define, Deliver - a workflow to create successful digital products.
Discover, Define, Deliver - a workflow to create successful digital products. Discover, Define, Deliver - a workflow to create successful digital products.
Discover, Define, Deliver - a workflow to create successful digital products.
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
 
Zwinność procesu rekrutacyjnego w branży IT
Zwinność procesu rekrutacyjnego w branży ITZwinność procesu rekrutacyjnego w branży IT
Zwinność procesu rekrutacyjnego w branży IT
 
STX Next - Scrum Development Process Overview
STX Next - Scrum Development Process OverviewSTX Next - Scrum Development Process Overview
STX Next - Scrum Development Process Overview
 
STX Next - Meet Us
STX Next - Meet UsSTX Next - Meet Us
STX Next - Meet Us
 
Group Process by Example - a PO’s and SM’s perspective
Group Process by Example - a PO’s and SM’s perspectiveGroup Process by Example - a PO’s and SM’s perspective
Group Process by Example - a PO’s and SM’s perspective
 

Recently uploaded

complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniquesugginaramesh
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 

Recently uploaded (20)

complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniques
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 

PostgreSQL Full-text Search in Django

  • 1. PostgreSQL Full-text Search in Django Paweł Kowalski
  • 2. ● What is full-text search ● How it works in PostgreSQL ○ search ○ ranking ● How to use it in Django ● Questions Agenda
  • 3. Full text search refers to techniques for searching a single computer-stored document or a collection in a full text database. https://en.wikipedia.org/wiki/Full_text_search WHAT IS FULL TEXT SEARCH
  • 4. SELECT * FROM table WHERE Col1 LIKE '%query%'; WHAT IS FULL TEXT SEARCH
  • 5. SELECT * FROM table WHERE Col1 LIKE '%query%'; WHAT IS FULL TEXT SEARCH SLOW, EXPENSIVE, NO ORDERING BY RELEVANCE ● LIKE ‘%query’ can’t use index ● Col1 can be very long (eg. entire book)
  • 6. SELECT to_tsvector( 'english', 'Try not to become a man of success, but rather try to become a man of value' ); to_tsvector ---------------------------------------------------------------------- 'becom':4,13 'man':6,15 'rather':10 'success':8 'tri':1,11 'valu':17 (1 row) HOW IT WORKS IN POSTGRESQL PostgreSQL, please help! TSVECTOR Since PostgreSQL 8.3
  • 7. select to_tsvector('If you can dream it, you can do it') @@ 'dream'; ?column? ---------- t (1 row) select to_tsvector('It''s kind of fun to do the impossible') @@ 'impossible'; ?column? ---------- f (1 row) HOW IT WORKS IN POSTGRESQL Search Operator: @@
  • 8. SELECT 'dream'::tsquery, to_tsquery('dream'); tsquery | to_tsquery --------------+------------ 'dream' | 'dream' (1 row) SELECT 'impossible'::tsquery, to_tsquery('impossible'); tsquery | to_tsquery --------------+------------ 'impossible' | 'imposs' (1 row) HOW IT WORKS IN POSTGRESQL TO_TSQUERY function
  • 9. SELECT to_tsvector('It''s kind of fun to do the impossible') @@ to_tsquery ('impossible'); ?column? ---------- t (1 row) HOW IT WORKS IN POSTGRESQL TO_TSQUERY function
  • 10. SELECT to_tsvector('If the facts don't fit the theory, change the facts') @@ to_tsquery('! fact'); SELECT to_tsvector('If the facts don''t fit the theory, change the facts') @@ to_tsquery('theory & !fact'); SELECT to_tsvector('If the facts don''t fit the theory, change the facts.') @@ to_tsquery('fiction | theory'); HOW IT WORKS IN POSTGRESQL Query Operators: ! & |
  • 11. SELECT COUNT(*) FROM ticketing_event WHERE name ILIKE '%madonna%rebel%heart% tour%'; Time: 78,083 ms HOW IT WORKS IN POSTGRESQL Some numbers SELECT COUNT(*) FROM ticketing_event WHERE search_vector @@ 'madonna & rebel & heart & tour'::tsquery; Time: 30,065 ms SELECT COUNT(*) FROM ticketing_event; count ------- 68889 Time: 11,440 ms
  • 12. SELECT post.id, setweight(to_tsvector(post.title), ‘A’) || setweight(to_tsvector(post.content), ‘B’) AS vector1 FROM post WHERE vector1 @@ to_tsquery(‘Michael & Jackson’) ORDER BY ts_rank(vector1, to_tsquery(‘Michael & Jackson’)); HOW IT WORKS IN POSTGRESQL Ranking: SETWEIGHT, TS_RANK functions
  • 13. SELECT ts_rank(to_tsvector('This is an example of document'), to_tsquery('example')) as relevancy; relevancy ----------- 0.0607927 (1 row) SELECT ts_rank(to_tsvector('This is an example of document'), to_tsquery('example | unknown')) as relevancy; relevancy ----------- 0.0303964 (1 row) HOW IT WORKS IN POSTGRESQL Ranking: SETWEIGHT, TS_RANK functions
  • 14. HOW TO USE IT IN DJANGO ● django-pg-fts ● djorm-ext-pgfulltext
  • 15. HOW TO USE IT IN DJANGO ● django-pg-fts ● djorm-ext-pgfulltext [WIP] Refs #3254 -- Add Full Text Search to contrib.postgres
  • 16. Post.objects.annotate( search=SearchVector('title') + SearchVector('content'), ).filter(search='Michael Jackson') HOW IT WORKS IN POSTGRESQL SearchVector model field Post.objects.filter(title__search='Michael Jackson')
  • 17. HOW IT WORKS IN POSTGRESQL SearchVector model field (stored) class Post(models.Model): title = models.CharField(max_length=100) content = models.TextField() search_vector = SearchVectorField() Post.objects.filter(search_vector='Michael Jackson') vector = SearchVector('title', weight=’A’) + SearchVector('content', weight=’B’) post.search_vector = vector post.save() Update SearchVector field in post_save signal
  • 18. HOW IT WORKS IN POSTGRESQL django.contrib.postgres.search.SearchRank queryset = Post.objects.annotate( rank=SearchRank( models.F('search_vector'), SearchQuery('Michael Jackson') ), ) queryset.filter(rank__gt=0.5).order_by('-rank')