Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7822405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:55:00+00:00 2026-06-02T07:55:00+00:00

I’m building a small search system for a Django project (yes I know, there’s

  • 0

I’m building a small search system for a Django project (yes I know, there’s already lots of products doing this but I’d like to try it, just for fun).
I basically have the following models:

class Word(models.Model):
    """ A searchable word.
    We only store the slugified value
    """
    slug = models.SlugField(unique = True)

class Searchable(models.Model):
    """ Superclass for Searchable objects.
    """
    words = models.ManyToManyField(
        Word,
        through='WordCount')

class WordCount(models.Model):
   """ Occurences of a word in a Searchable object.
    """
    word = models.ForeignKey(Word)
    item = models.ForeignKey(Searchable)
    count = models.IntegerField()

So for example, I create an object Page (subclassing Searchable) with the text “Hello StackOverflow, I have a Django question”. The system will create a Word instance for each words in this sentence, and for each a WordCount instance saying that each word appears once in the text.

Making a query to get all Searchable instances containing one a more word works fine (searchable_text extract the words and makes a list from it):

def search(query)
    tokens = searchable_text(query)
    words = Word.objects.filter(
                        reduce(operator.or_,
                               [models.Q(slug__contains = t)
                                for t in tokens]))

    return Searchable.objects.filter(words__in = words)

Now what I’d like to do is to use the intermediate relation to order the results. I’d like to keep a QuerySet so the following code will not work, but gives the idea of what I want to do (with ugly patching to make annotations):

def search(query)
    tokens = searchable_text(query)
    words = Word.objects.filter(
                        reduce(operator.or_,
                               [models.Q(slug__contains = t)
                                for t in tokens]))
    results = []
    for obj in Searchable.objects.filter(words__in = words):
        matching_words = obj.wordcount_set.filter(word__in = words)
        obj.weight = sum([w.count for w in matching_words])
        results.append(obj)

    return sorted(results,
                  reverse = True,
                  key = lambda x: x.weight)

So basically:
– I get all Word objects contained in the query (or partially matching, if I search for “Stack”, the Word “StackOverflow” will be taken into account)
– I get all objects that have a relation with any of those words
– for each objects, I select all related WordCount objects that are related with a Word in the list of Word previously computed, then do the sum of the ‘count’ attribute and store it as an annotation ‘weight’
– I sort my objects on the ‘weight’

I don’t know if that’s doable with QuerySet but I’d like to keep the format for some extra actions after (like filtering out some results for example).

I know there lots of improvements possible but that would be a good start.

Thanks for the answers,
Vincent

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:55:01+00:00Added an answer on June 2, 2026 at 7:55 am

    Try

    Searchable.objects.filter(words__in=words).annotate(
        weight=models.Sum('wordcount__count')).order_by('-weight')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.