Full Text Search in Django

What is Full Text Search?

According to Wikipedia, In a full-text search, a search engine examines all of the words in every stored document as it tries to match search criteria.

Elasticsearch is a search engine based on the Lucene library. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents.

PostgreSQL has been supporting Full-Text Search since 2008. Internally it uses “tsvecor” and “tsquery” data type to process the data to search. It has some indexes that can be used to speed up the search: GIN and GiST. PostgreSQL added support for Phrase Search in 2016. Support for JSON[B] Full-Text Search was added in version 10.

Implementing Full Text Search in Django

We''ll be using PostgreSQL for our implementation.

Let's assume the following cases are valid for our implementation:

  • We have an Author model with name field
  • We have created instances as follows: 
    • Author.object.create(name='Timothy Olaleke')
    • Author.object.create(name='Mr Timothy Joseph')
    • Author.object.create(name='Dr. You Timothy')

Now Django allows us perform a full text search which returns a list of objects that matches the text been searched for.

Author.objects.filter(name__search='Timothy')

This returns a list of instances with 'Timothy' , irrespective of where it appears within the name.

Feel free to explore our more sample from here.

Thanks for reading!

Comments

Popular Posts