Skip to content

400 Bad request for indices.exists #1069

Closed
@tomaskallup

Description

@tomaskallup

🐛 Bug Report

I'm trying to create indices via the node js API, to make sure everything goes well, I fist do a check if the given index exists and send a delete to make sure it's not there. How ever for one index (not sure why only the one) the exists api throws 400 error with true in the body, this happens only if the index is already created.

Response example:

error ResponseError: Response Error
    at IncomingMessage.<anonymous> (/home/armeeh/Projects/Reas/reas-services/node_modules/@elastic/elasticsearch/lib/Transport.js:294:25)
    at IncomingMessage.emit (events.js:215:7)
    at endReadableNT (_stream_readable.js:1184:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  name: 'ResponseError',
  meta: {
    body: true,
    statusCode: 400,
    headers: {
      'content-type': 'text/plain; charset=utf-8',
      connection: 'close'
    },
    warnings: null,
    meta: {
      context: null,
      request: [Object],
      name: 'elasticsearch-js',
      connection: [Object],
      attempts: 0,
      aborted: false
    }
  }
}

Now I'm not really sure, if this is a error on the js part, or elastic itself, so please tell me and I'll recreate the issue accordingly.
Also a thing to add, using something like Postman or just simple curl and sending HEAD request to /streets (the one where it fails) I get the expected response (200 or 404).

To Reproduce

Steps to reproduce the behavior:

import { Client } from '@elastic/elasticsearch';

(async () => {
  // Elastic
  const cloudId = 'cloudId';
  const auth = {
    username: 'elastic',
    password: 'password',
  };

  const client = new Client({
    cloud: cloudId ? { id: cloudId, ...auth } : undefined,
    node:
      'url to cloud node',
    auth: cloudId ? undefined : auth,
  });

  for (const index of ['cities', 'streets']) {
    let exists = false;
    try {
      console.log('checking', index);
      // This throws the exception - only for 'streets' and only if they exist
      const existsResponse = await client.indices.exists({ index });
      exists = existsResponse.body;
    } catch (e) {
      console.log('error', e);
      debugger;
    }

    try {
      if (exists) {
        console.log('deleting', index);
        await client.indices.delete({
          method: 'DELETE',
          index,
        });
      }
    } catch (e) {
      console.log('error', e);
      debugger;
    }
  }

  try {
    await client.indices.create({
      index: 'streets',
      body: {
        settings: {
          analysis: {
            analyzer: {
              'my-asciifolding': {
                type: 'custom',
                tokenizer: 'standard',
                filter: ['standard', 'lowercase', 'asciifolding'],
              },
            },
          },
        },
        mappings: {
          properties: {
            city: {
              type: 'text',
              analyzer: 'my-asciifolding',
            },
            name: {
              type: 'text',
              analyzer: 'my-asciifolding',
            },
          },
        },
      },
    });
  } catch (e) {
    console.log('error', e);
    debugger;
  }

  try {
    await client.indices.create({
      index: 'cities',
      body: {
        settings: {
          analysis: {
            analyzer: {
              'my-asciifolding': {
                type: 'custom',
                tokenizer: 'standard',
                filter: ['standard', 'lowercase', 'asciifolding'],
              },
            },
          },
        },
        mappings: {
          properties: {
            name: {
              type: 'text',
              analyzer: 'my-asciifolding',
            },
          },
        },
      },
    });
  } catch (e) {
    console.log('error', e);
    debugger;
  }
})();

Expected behavior

I expect the method to return the correct response and not throw an exception.

Your Environment

  • node version: v12.13.1
  • @elastic/elasticsearch version: 7.5.0
  • os: Linux
  • Using the free trial of elasic cloud

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions