Full-text Django model architecture for PostgreSQL

I am creating a website for social interaction on a specific topic, based on flows, I think about gmail, only public. There will also be some kind of static information in dictionaries, as well as in a blog, directories, knowledge base, etc. This is django + postgres.

One of the most important requirements is a full-text search of all information, regardless of the type of model. If an exact search phrase appears on a blog, and her distorted sister in the messages, than a fragment from a blog entry, must first appear in the search results, and a message fragment should appear behind it. So, I need a table with all indexed texts and links to _any_other_table_ in db.

My first idea is to create a separate model with a “free link”, for example:

class Content(models.Model): obj_id= CharField() # An id of the object of a given model. model= CharField(choices=("Message", "BlogEntry", "HowTo", "EntityProfile",)) content_type= CharField(choices=("subject", "body", "description", "tags",)) body= TextField() 

But it doesn’t feel right ... This promises an extra hassle around link integrity when creating and re-linking instances.

So the question is, is there an elegant solution that django will provide? What could be the most efficient architecture to solve the problem?

I do not ask for a direct answer, but rather a hint.

Thanks in advance!

+4
source share
2 answers

I had great success working with this snippet that uses PostgreSQL tsearch2. I modified it for various purposes in various ways, but basically it works very well and it is very easy to implement.

+3
source

Thanks so much for the tips!

while contenttypes are ideal for this kind of task, it's really tempting to be DB independent

from what I read so far, Solr is a reliable search engine, but I think I will go and try ElasticSearch through Haystack

Thanks again!

+1
source

Source: https://habr.com/ru/post/1447112/


All Articles