How to create small search engine for your awesome project

How to create small search engine for your awesome project?

What do you think about Google or DuckDuckGo? Yes, I love it too, as millions of people.

But, if you create new project, you will need strong and very smart search engine? No, you will need very small and fast search engine!

Okay let's google it and what will we get? Finally you see ElasticSearch, awesome search engine for full text.

Suppose you create project on Nodejs + PostgreSQL and your database have 10,000 items. ElasticSearch to cope with this task.

But, you need copy your database, because ElasticSearch should generate its “search graph” and we have 2 “databases”, I dont like it, because we need more free space in my hard drive.

Can PostgreSQL help us for created custom search engine? Yes, he can.

Okay, to help you understand, i have programming language JavaScript, because i like him, but PostgreSQL main database in 2017. Very many people use NodeJS and PostgreSQL in your small and big projects.

Maybe start programming ?

Yeah, go to start and create new folder for our project.

Naming: sm-fs-search-engine (small fast search engine).

And we need init npm and install this packages.

{
  "name": "sm-fs-search-engine",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "MIT",
  "dependencies": {
    "body-parser": "^1.18.2",
    "express": "^4.16.2",
    "pg": "^7.4.0"
  }
}

Next, we create file server.js and write this:

And go to check this, node server.js, hmmm very nice work!

Okay, next we need set setting in PostgreSQL, go to open pgsql and create database.

Nice, we have our database. Next, create table university.

And now, we need university. I added the universities itself.

Now the most important thing, add another column, her name fts.

And update this.

Finish step for database, this create search index.

Awww right, now, we can start writing REST API for search engine.

Create API for search engine

If you notice we set the pack pg, special for him we need create folder liband create file db.js, this file current for connection on our database.

This file we include if we use query for database.

Okay, create folder functions this folder have all functions we need.

And update server.js

And go to check this, open your browser and write:

http://localhost:3000/search?text=Institutearrow-up-right

Example response:

Its work!

And we created small and fast engine!

Full codearrow-up-right

How to make it even better ?

We can write support response page for university, like this http://localhost:3000/search?text=Institute&page=2arrow-up-right, this will increase the upload speed results.

If you have troubles, write me twitterarrow-up-right or mail (gaserd123@gmail.comenvelope).

Thanks for read and may the force be with you!

Last updated

Was this helpful?