0

I've got a NodeJS app that uses @elastic/elasticstack's client. I'm trying to get a list of indexes (or indices) from my elastic database. I'm new to elastic, but when this REST endpoint is called I get back 695 results and I believe I've only created a couple indexes.

The code calling elastic to find the indexes is:

const express = require('express')
const app = express()
const { Client } = require('@elastic/elasticsearch')
const clientOpts = {};  // connect info not shown
const client = new Client(clientOpts)

app.get('/es/indices', async (req, res) => {
    let queryInfo = {
        index: '_all'
    };
    client.indices.stats(queryInfo).then(result => {
        console.log('/es/index got =', JSON.stringify(result.body, null,2));
        res.send(result.body);
    }).catch(ex => {
        console.error('/es/index failed ', ex);
        res.status(500).send({ error: { message: ex.message, stack: ex.stack } });
    })
})

Below is a short snippet showing the results and one of my index.

  "data": {
    "_shards": {
      "total": 695,
      "successful": 695,
      "failed": 0
    },

      "myindex": {
        "uuid": "xuuoc19mTJyfvjZlyMZVxB",
        "primaries": {
          "docs": {
            "count": 303375,
            "deleted": 0
          },

(Q) What's the way to get a list of the indexes that contain user data on an Elastic Database?

Searching for solution

Elastic docs

I found the Elastic documentation but it has very little useful information other than repeating the names of the parameters. For example, URL indices.stats defines the function as:

client.indices.stats({
  metric: string | string[],
  index: string | string[],
  completion_fields: string | string[],
  fielddata_fields: string | string[],
  fields: string | string[],
  groups: string | string[],
  level: 'cluster' | 'indices' | 'shards',
  types: string | string[],
  include_segment_file_sizes: boolean,
  include_unloaded_segments: boolean,
  expand_wildcards: 'open' | 'closed' | 'hidden' | 'none' | 'all',
  forbid_closed_indices: boolean
})

But does little to explain what the parameter values mean/do.

metric string | string[] - Limit the information returned the specific metrics.

I'm using ElasticSearch version 7.17 which is why the URL points to that version.

Similar SO Question

I found the SO Question/answer: https://stackoverflow.com/a/17429465/3281336 which I tried but the aliases API returns way more than what I wanted.

PatS
  • 8,833
  • 12
  • 57
  • 100

1 Answers1

0

I found the client.cat() API that worked better for what I wanted. Below is the code:

app.get('/es/indices', async (req, res) => {
    let paramInfo = {
        index: '_all',
        format: 'json',
        bytes: 'g',
        pri: true
    };
    client.cat.indices(paramInfo).then(result => {
//         console.log('/es/index got =', JSON.stringify(result.body, null,2));
        let f1 = _.filter(result.body, (item) => Number(item["docs.count"]) > 0);
        let f2 = _.map(f1, (item) => ({name: item.index, count: Number(item["docs.count"]) }));
        let f3 = _.sortBy(f2, item => item.name);
        res.send(f3);
    }).catch(ex => {
        console.error('/es/index failed ', ex);
        res.status(500).send({ error: { message: ex.message, stack: ex.stack } });
    })
})

The resulting JSON shape looks like this:

/es/index got = [
  {
    "health": "green",
    "status": "open",
    "index": "acme-my-cool-data",
    "uuid": "ciPwz2fUTeWPaDLxiR48nA",
    "pri": "1",
    "rep": "1",
    "docs.count": "180",
    "docs.deleted": "0",
    "store.size": "0",
    "pri.store.size": "0"
  },
  {
    "health": "green",
    "status": "open",
    "index": "acme-workspace-3be0c9a988b441b693f84d12791388c2",
    "uuid": "ciPwz2fUTeWPaDLxiR48nB",
    "pri": "1",
    "rep": "1",
    "docs.count": "0",
    "docs.deleted": "0",
    "store.size": "0",
    "pri.store.size": "0"
  },

So the code at let f1= filters out all results that have "doc.count": 0. In my case numerous indexes with zero documents exist (not sure why).

Code at let f2 transforms the shape to a result my UI wants which is a list of indexes and their counts, so {name, count}.

Code at let f3 sorts the results so they come back in a meaningful order.

PatS
  • 8,833
  • 12
  • 57
  • 100