Get all documents by index name

Hello,

Which function should I use to get all documents by index name ?

route.js:

server.route({
    path: '/api/getAllDocuments/index/{name}',
    method: 'GET',
    handler(req, reply) {
      client.FUNCTION_NAME(req.params.name, function (err,response) {
        reply(response);
      });
    }
});

Thanks for advance.

Use a scroll query.

1 Like

Hello @Christian_Dahlqvist,
I don't need a filter :confused: This is the only function I can use ?

const allTitles = [];
const responseQueue = [];

// start things off by searching, setting a scroll timeout, and pushing
// our first response into the queue to be processed
await client.search({
  index: 'myindex',
  scroll: '30s', // keep the search results "scrollable" for 30 seconds
  source: ['title'], // filter the source to only include the title field
  q: 'title:test'
}) ........

Thanks for advance .

Scroll query is the best way to get large amounts of data with or without filters.

1 Like

Hello @Christian_Dahlqvist,

I used scroll query without filter and this is my code:

server.route({
    path: '/api/indices_view/index/{name}',
    method: 'GET',
    handler(req, reply) {

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

      client.search({
        index: req.params.name,
        scroll: '30s'
      })

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

        response.hits.hits.forEach(function (hit) {
          allHits.push(hit);
        });

        if (response.hits.total === allHits.length) {
          break
        }

        responseQueue.push(
          client.scroll({
            scrollId: response._scroll_id,
            scroll: '30s'
          })
        );
      }
    }
  });

I didn't get response in this case (like this route doesn't exist) :roll_eyes: (Did I miss something here ?)

But if I use only the client.search :

client.search({
  index: req.params.name
}, function (err,response) {
   reply(response);
});

I got just the 10 first hits of the index selected, despite the console displays the right total of hits!!

May you explain to me ?
Thanks for advance @Christian_Dahlqvist :wink:

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