DEV Community

Sergey Gustun
Sergey Gustun

Posted on

2 3

How create small 'algorithm similar items'

Hi, very long time ago i made post small search engine.

And that post i use fts vector for search in your postgress database.

But in time my project needed similar items and i thought it is necessary to take some complex algorithm and implement it, but i can make small and easy for use algorithm search a similar items from my database.

I start search Google and ... oh my god, very much interest algorithms, but i dont see for NodeJS. Sorry, may be i not good use Google.

Hmmm Stop! I have fts vector and we have fast algorithm Levenstein. Yeah.

FTS vector - this is small matrix with very important word/words in your text. I have this structure in my database:

item
id | title | price | description | fts

fts vector generic sum of title and description.

We can compared this vectors in algorithm Levenstein or another. That is easy.

Okay, how start this ?

First i writed himself algorithm Levenstein, but he is sooo slooooowwwwllllyyyyy... and i found npm-package -> fast-levenstein

Okay, go write code.

    npm install fast-levenstein
Enter fullscreen mode Exit fullscreen mode

Next, i created this file.

    var levenshtein = require('fast-levenshtein');

    var getSimilarItems = function(id,func) {
        //....
        //code of get items, sort this items
        //....

        let levenshtein_number = levenshtein.get(data[0].fts, category_items[i].fts)

        //compare numbers and return necessary       

    }

    module.exports.getSimilarItems = getSimilarItems
Enter fullscreen mode Exit fullscreen mode

JUST DO IT

And, thats all. What do you think about this and what i do better ?

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay