DEV Community

Sergey Gustun
Sergey Gustun

Posted on

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 ?

Top comments (0)