0

I'm using Django's builtin trigram_similar lookup and TrigramSimilarity class to create a fuzzy searching using a PostgreSQL database. This way, I filter titles of articles. The problem is, this filters based on the whole title. I want to filter it also on parts on title.

Example:

Title of Article: "We are the world | This is the best article ever".

My search: "we"

In this example, my function returns nothing, but I want it to return this article. How can I do that?

This is the code I use:

def search(qs, term, fields=["title"], treshold=.3):
    if len(fields) >= 2:
        return qs.annotate(
            similarity=Greatest(
                *[TrigramSimilarity(field, term) for field in fields]
            )
        ).filter(similarity__gte=treshold).order_by("-similarity")
    return qs.filter(
        **{"{}__trigram_similar".format(fields[0]): term}
    )
Myzel394
  • 1,155
  • 3
  • 16
  • 40
  • Recent versions of PosgreSQL offer word similarity functions (<->> or <->>>) . So it is just a matter of getting Django to expose it, which I don't know about. – jjanes Sep 10 '19 at 19:56

1 Answers1

-1

I think the problem is that you are using trigram similarity with a two letter word. If you try to do this with a word that is three letters, maybe it will work?

Alper
  • 3,424
  • 4
  • 39
  • 45