Elasticsearch.js _ search API error body is not defined

Hello Team,

I am have created a custom plugin and i am trying to call elasticsearch _ search API using elasticsearch.js
I have created custom route

server.route({
    path: '/api/HelloWorld/search_test',
    method: 'POST',
    handler(req, reply) {
      const query =  req.payload.searchterm;
	  client.search({
		index: 'inspections',
		body: {
			query: {
				match: {
					"name": query
				}
			}
		}
	})

when the function executes , it hits elastic search API and following API is shown in logs.

starting request {
  "method": "POST",
  "path": "/inspections/_search",
  "body": {
    "query": {
      "match": {
        "name": "1"
      }
    }
  },
  "query": {}
} 

and in response it gives correct response

However, logs shows following error

error [14:57:39.268] [warning][process] UnhandledPromiseRejectionWarning: ReferenceError: body is not defined

Could you please let me know if i am missing something.

Thank you very much for your help and support.

Thank you,
Aditya

You will want to access the elasticsearch cluster in this way to ensure elasticsearch is called with the correct permissions

const server = request.server;
const { callWithRequest } = server.plugins.elasticsearch.getCluster('data');
const searchPayload = {
  index: 'myIndex',
  body: {}
}
const response = await callWithRequest(request, 'search', searchPayload);

callWithRequest is a wrapper around javascript elasticsearch client

Your sample code is missing how reply is called so I am not sure where the promise error is coming from

1 Like

Thank you very much Nathan_Reese.

I am able to use callWithRequest and get the data from the elasticsearch.

handler(req, reply) {
const query_param =  req.payload.searchterm;	
const { callWithRequest } = server.plugins.elasticsearch.getCluster('data');
const searchPayload = {
	index: 'inspections',
body: {
	query: {
			match: {
				"name": query_param
			}
		}
	
	}
}
callWithRequest(req, 'search', searchPayload).then(response => {
	console.log(response.status);
	console.log({response});
	console.log(response);
	})

}

Just one query , One point i did not understand, when i hit api 'localhost:9200' from browser i get cluster name as 'elasticsearch' but when i use that name to get callWithRequest

{
  "name" : "Z3QtD8T",
  "cluster_name" : "**elasticsearch**",
  "cluster_uuid" : "1Z3o3i2WQ6eiH6Ayi2Rhiw",
  "version" : {
    "number" : "6.5.1",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "8c58350",
    "build_date" : "2018-11-16T02:22:42.182257Z",
    "build_snapshot" : false,
    "lucene_version" : "7.5.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

server.plugins.elasticsearch.getCluster('elasticsearch');
It gives a Type Error but when i changed that name to 'data' , it worked.

Thanks agian for your help and support.

Thank you,
Aditya

getCluster accepts either data or admin. The value is not related to the name of your cluster. Its kibana's way of differentiating between the elasticsearch instance holding data and the elasticsearch instance holding admin stuff like saved objects

Ok.. Thank you very much Nathan_ Riese.
Really appreciate your help.

Thank you
Aditya

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.