|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +process.on('unhandledRejection', function (err) { |
| 21 | + console.error(err) |
| 22 | + process.exit(1) |
| 23 | +}) |
| 24 | + |
| 25 | +import { Client } from '../../' |
| 26 | +import assert from 'node:assert' |
| 27 | + |
| 28 | +async function main () { |
| 29 | + // a simple manual smoke test that can be run against a fresh serverless instance |
| 30 | + const { ELASTICSEARCH_URL, ES_API_SECRET_KEY } = process.env |
| 31 | + |
| 32 | + assert(Boolean(ELASTICSEARCH_URL), "ELASTICSEARCH_URL is required") |
| 33 | + assert(Boolean(ES_API_SECRET_KEY), "ES_API_SECRET_KEY is required") |
| 34 | + |
| 35 | + const client = new Client({ |
| 36 | + node: ELASTICSEARCH_URL, |
| 37 | + auth: { apiKey: ES_API_SECRET_KEY }, |
| 38 | + }) |
| 39 | + |
| 40 | + const indicesBefore = await client.cat.indices() |
| 41 | + assert.equal(indicesBefore.trim().length, 0, "There should not be any indices.") |
| 42 | + |
| 43 | + await client.indices.create({ |
| 44 | + index: 'books', |
| 45 | + aliases: { novels: {}, 'things_to_read': {} }, |
| 46 | + }) |
| 47 | + |
| 48 | + const indicesDuring = await client.cat.indices() |
| 49 | + assert(indicesDuring.includes('books'), "Books index should exist") |
| 50 | + |
| 51 | + const body = [ |
| 52 | + {name: "Snow Crash", "author": "Neal Stephenson", "release_date": "1992-06-01", "page_count": 470}, |
| 53 | + {name: "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585}, |
| 54 | + {name: "1984", "author": "George Orwell", "release_date": "1985-06-01", "page_count": 328}, |
| 55 | + {name: "Fahrenheit 451", "author": "Ray Bradbury", "release_date": "1953-10-15", "page_count": 227}, |
| 56 | + {name: "Brave New World", "author": "Aldous Huxley", "release_date": "1932-06-01", "page_count": 268}, |
| 57 | + {name: "The Handmaid's Tale", "author": "Margaret Atwood", "release_date": "1985-06-01", "page_count": 311} |
| 58 | + ] |
| 59 | + |
| 60 | + let docCount = 0 |
| 61 | + const bulkResult = await client.helpers.bulk({ |
| 62 | + datasource: body, |
| 63 | + onDocument () { |
| 64 | + docCount++ |
| 65 | + return { |
| 66 | + index: { _index: 'books' } |
| 67 | + } |
| 68 | + } |
| 69 | + }) |
| 70 | + assert.equal(docCount, 6, "Docs indexed should be 6") |
| 71 | + assert.equal(bulkResult.total, 6, "Total docs indexed should be 6") |
| 72 | + assert.equal(bulkResult.failed, 0, "Total failed docs should be 0") |
| 73 | + assert.equal(bulkResult.successful, 6, "Total successful docs should be 6") |
| 74 | + |
| 75 | + await client.indices.refresh({ index: 'books' }) |
| 76 | + |
| 77 | + const result = await client.search({ |
| 78 | + index: 'things_to_read', |
| 79 | + query: { |
| 80 | + match: { |
| 81 | + author: 'Ray Bradbury' |
| 82 | + } |
| 83 | + } |
| 84 | + }) |
| 85 | + assert.equal(result.hits.hits.length, 1) |
| 86 | + assert.equal(result.hits.hits[0]._source.name, "Fahrenheit 451") |
| 87 | + |
| 88 | + const docId = result.hits.hits[0]._id |
| 89 | + const doc = await client.get({ index: 'novels', id: docId }) |
| 90 | + |
| 91 | + assert.equal(doc._source.author, "Ray Bradbury") |
| 92 | + |
| 93 | + await client.update({ index: 'novels', id: docId, doc: { author: 'Ray Bradberry', 'rating': 3.2 }}) |
| 94 | + await client.indices.refresh({ index: 'books' }) |
| 95 | + |
| 96 | + const doc2 = await client.get({ index: 'novels', id: docId }) |
| 97 | + assert.equal(doc2._source.author, "Ray Bradberry") |
| 98 | + assert.equal(doc2._source.rating, 3.2) |
| 99 | + |
| 100 | + await client.indices.delete({ index: 'books' }) |
| 101 | + |
| 102 | + const indicesAfter = await client.cat.indices() |
| 103 | + assert.equal(indicesAfter.trim().length, 0, "There should not be any indices.") |
| 104 | +} |
| 105 | + |
| 106 | +main() |
| 107 | + .catch(err => console.error(err)) |
| 108 | + .finally(() => console.log('Test succeeded')) |
0 commit comments