Models

models.py 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/settings.py:

# Application definition

INSTALLED_APPS = [
    'collection.apps.CollectionConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

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

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

and migrating (apply to the database):

(search) [user]@[machine]:~/search$ python manage.py 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/admin.py:

from django.contrib import admin

# Register your models here.

from .models import Document

admin.site.register(Document)
  • Last modified: 2020/03/27 20:19