Await is a reserved word - Using ES API to load list of hits by index

Hello,

I am using Kibana to load list of hits by index name using ElasticSearch API.
The best way to do that, is using Scroll API and this is my routes.js code:

const elasticsearch = require('elasticsearch');

export default function (server) {

  const config = server.config();
  const client = new elasticsearch.Client({ host: config.get('elasticsearch.url') });
  
  server.route({
    path: '/api/indices_view/index/{name}',
    method: 'GET',
    handler(req, reply) {

      const allHits = [];
      const responseQueue = [];

      // start things off by searching, setting a scroll timeout, and pushing
      // our first response into the queue to be processed
      client.search({
        index: 'shakespeare',
        scroll: '30s', // keep the search results "scrollable" for 30 seconds
        q: 'type:act'
      })

      while (responseQueue.length) {
        const response = responseQueue.shift();

        // collect the titles from this response
        response.hits.hits.forEach(function (hit) {
          allHits .push(hit.fields.title);
        });

        // check to see if we have collected all of the titles
        if (response.hits.total === allHits .length) {
          console.log('every "test" title', allHits );
          break
        }

        // get the next response if there are more titles to fetch
        responseQueue.push(
          client.scroll({
            scrollId: response._scroll_id,
            scroll: '30s'
          })
        );
      }
    }
  });
}

I know.. the example uses Await operator. If I use it, I got this error in my console await is a reserved word, else I got nothing.

I'll appreciate any help :smiley:
Best regards.

it's because the handler function isn't marked as async. change to:

async handler(req, reply) {

1 Like

Thanks for your reply @Stacey_Gammon :smiley:

I added async before the handler function, but I got this error:

error [06:47:16.302] [warning][process] [illegal_state_exception] source and source_content_type parameters are required :: {"path":"/shakespeare/_search","qu
ery":{"scroll":"30s","source":["type"],"q":"type:act"},"statusCode":500,"response":"{"error":{"root_cause":[{"type":"illegal_state_exception","reason"
:"source and source_content_type parameters are required"}],"type":"illegal_state_exception","reason":"source and source_content_type parameters are re
quired"},"status":500}"}

Is this an issue too ? :frowning:

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