Flexible Search with Express Framework

I want to create an application and api that will basically fetch the resource. Now I have heard a lot about Nodejsand ElasticSearch, and I know a little Nodejs and Express framework. But I do not know how to integrate ElasticSearch with Express.

+4
source share
3 answers
  • 1 download ElasticSearch
  • Inside your project / Express directory framework download ElasticSearch express driver using npm install elasticsearch --save
  • Run an instance of ElasticSearch, by default it runs on port 9200
  • To use this module, just create a client instance

    var elasticsearch = require('elasticsearch');
    var client = elasticsearch.Client({
      host: 'localhost:9200'
    });
    
    client.search({
      index: 'books',
      type: 'book',
      body: {
        query: {
          multi_match: {
            query: 'express js',
            fields: ['title', 'description']
          }
        }
      }
    }).then(function(response) {
      var hits = response.hits.hits;
    }, function(error) {
      console.trace(error.message);
    });
    
  • Helpfull Link https://blog.raananweber.com/2015/11/24/simple-autocomplete-with-elasticsearch-and-node-js/

+3

-, , Elasticsearch.

Node.js: https://github.com/elastic/elasticsearch-js

,

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
  host: '<your_instance_ip>:9200',
  log: 'trace'
});

client.send client.search .

+1

OSx

1. elasticsearch

  • brew elasticsearch//

  • elasticsearch//

2.

2.1 ES

  • npm install elasticsearch --save

2.2

  • FOLDER : routers/elastic-search-router.js

    var express= require('express');
    var router= express.Router();
    var elasticsearch=require('elasticsearch');
    var client = new elasticsearch.Client( {  
                 hosts: [
                         'http://localhost:9200/',//DEFAULT URL
                        //'https://[username]:[password]@[server]:[port]/'
                        ]
     });
    
     /* This operation health of es */
     router.get('/health',function(req,res){
       client.cluster.health({},function(err,resp,status) {  
          if(err)
          {  console.log("-- Client Health ERROR--",err);
          }else{
             console.log("-- Client Health --",resp);
             res.send({resp});
          }
       });
     });
     /* This operation For Creating new Index of es */
     router.get('/createIndex',function(req,res){
       client.indices.create({  
            index: 'imageMap'
       },function(err,resp,status) {
       if(err) {
         console.log(err);
       }
       else {
          console.log("create",resp);
          res.send({resp});
       }
     });
    });
    module.exports =router;
    

2.2 Now inside the Express index.js file

    // Setup an Express app
    var express = require('express');
    var app = express();
    var bodyParser = require('body-parser');
    var path= require('path');
    //use routers
    app.use(require(path.join(__dirname,'routers/elastic-search-router')));
    ....

2.3 Launch Express Server:

IN TERMINAL : node index.js
  1. In the application url, you can access each function in ur router as:

    http: // localhost: 3000 / createIndex

    http: // localhost: 3000 / health

0
source

Source: https://habr.com/ru/post/1651895/


All Articles