0
votes

I am stuck in a problem and can not figure out the solution. I want to use an elasticSearch query from my nodejs. The problem is, I can make it work from postman, but not from node.

http://user:psd@my_domain:9200/ra_autocomplete/search

And from my nodejs app :

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
  host: 'my_domain',
  port : 9200,
  protocol : 'http',
  auth : 'user:psd',
  maxRetries : 2
});

And then,

    client.search({
       index: "ra_autocomplete",
       body: {
         query: {
           m_prefix : {
           r_n : {
            query : my_var
          }
        }
      }
    }
  } , function(err, res) {
      console.log(err);
      console.log(res);
  });

I get this error :

Error: Not Found at respond (my_path\node_modules\elasticsearch\src\lib\transport.js:307:15) at checkRespForFailure (my_path\node_modules\elasticsearch\src\lib\transport.js:266:7) at HttpConnector. (my_path\node_modules\elasticsearch\src\lib\connectors\http.js:159:7) at IncomingMessage.bound (my_path\node_modules\lodash\dist\lodash.js:729:21) at emitNone (events.js:111:20) at IncomingMessage.emit (events.js:208:7) at endReadableNT (_stream_readable.js:1056:12) at _combinedTickCallback (internal/process/next_tick.js:138:11) at process._tickCallback (internal/process/next_tick.js:180:9) status: 404, displayName: 'NotFound', message: 'Not Found', path: '/roads_autocomplete/_search', query: {}, body: '{"query":{"m_prefix":{"r_n":{"query":"montexte a analyser"}}}}', statusCode: 404, response: '\r\n404 Not Found\r\n\r\n

404 Not Found

\r\n
nginx/1.10.3 (Ubuntu)\r\n\r\n\r\n', toString: [Function], toJSON: [Function] }

Any help would be appreciated, the problem is, when I try to make it with postman, it goes well.

Thank you.

1

1 Answers

1
votes

I think the problem is that you are not connecting to elastic search node.

Add your port and domain to into one line and then start by running a simple query.

var elasticsearch = require('elasticsearch');

var client = new elasticsearch.Client({
   host: 'localhost:9200',
   log: 'trace'
});

client.search({
    index: 'products',
    type: 'product',
    body: {
        query: {
            bool: {

            }
        }
    }
    }).then((body) => {
        return body;
    }, (error) => {
        console.trace(error.message);
    });