Models for a simple search engine for searching across documents and ranking results according to relevancy to query.

from django.db import models

# Create your models here.

# Store document content
class Document(models.Model):
    content = models.TextField()
    def __str__(self):
        return self.document_text

# Store term frequency in a document
# Calculate score to rank the tfidf
class TermFrequency(models.Model):
    term = models.CharField(max_length=200)
    document = models.ForeignKey(Document, on_delete=models.CASCADE)
    frequency = models.IntegerField()
    score = models.FloatField()
    def __str__(self):
        return self.termfrequency_text

# Number of documents containing a term
class DocFrequency(models.Model):
    term = models.CharField(max_length=200)
    num_docs = models.IntegerField()
    def __str__(self):
        return self.docfrequency_text

Adding a reference to the collection configuration class in the INSTALLED_APPS setting in /home/[user]/search/search/

# Application definition


Storing the models as a migration (create the migrations):

(search) [user]@[machine]:~/search$ python makemigrations
Migrations for 'collection':
    - Create model DocFrequency
    - Create model Document
    - Create model TermFrequency

and migrating (apply to the database):

(search) [user]@[machine]:~/search$ python migrate
Operations to perform:
  Apply all migrations: admin, auth, collection, contenttypes, sessions
Running migrations:
  Applying collection.0001_initial... OK

Making Document modifiable in Admin by adding in /search/collection/

from django.contrib import admin

# Register your models here.

from .models import Document
  • Last modified: 2020/03/27 20:19