Bryansq2nt
Bryansq2nt

Reputation: 394

NodeJs + Postgresql + ElasticSearch

I have a project that has a lot of information in the database, I have several tables that can contain names of people and I need to do a search by name, but I need that name to search in all fields of all tables, That's why I decided to use ElasticSearch but I don't know how to integrate it with NodeJs and Postgresql, all the tutorials that I find on the internet do not use postgres as a database, they use ElasticSearch as the database as well.

this way i initialize elasticsearch :

var elasticsearch = require('elasticsearch');

var client = new elasticsearch.Client({
hosts: [ 'http://localhost:9200']
});

client.ping({
requestTimeout: 30000,
}, function(error) {
if (error) {
console.error('Cannot connect to Elasticsearch.');
} else {
console.log('Connected to Elasticsearch was successful!');
}
});

and this way is how to i search in my postgres database :

await db.query('SELECT * FROM people WHERE name like '%Jhon%' OR comments like '%Jhon%'', (err, results) => {
        if (err) {
            console.log(err.stack);
            return res.send('Sorry an error ocurred');
        }

        var people = results.rows;


        console.log(people);


    })

How can i use ElasticSearch to search in my postgres database?

Upvotes: 1

Views: 2855

Answers (1)

Ashish Modi
Ashish Modi

Reputation: 7770

You need to index all your data from Postgres to Elasticsearch to be able to search in elastic search. There are many ways to push data to elasticsearch. You can read some of the way here.

Upvotes: 1

Related Questions