Skip to content

Commit

Permalink
# Added Sample code for rollups
Browse files Browse the repository at this point in the history
Signed-off-by: Theo Truong <theotr@amazon.com>
  • Loading branch information
nhtruong committed Jun 12, 2024
1 parent caf9ded commit 136ea1c
Showing 1 changed file with 146 additions and 0 deletions.
146 changes: 146 additions & 0 deletions samples/rollups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
*/

// const { Client } = require('@opensearch-project/opensearch');
const {Client} = require('../index.js') // from 'opensearch-project/opensearch

Check failure on line 12 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, ubuntu-latest)

Replace `Client}·=·require('../index.js')` with `·Client·}·=·require('../index.js');`

Check failure on line 12 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, macOS-13)

Replace `Client}·=·require('../index.js')` with `·Client·}·=·require('../index.js');`

// Instantiate a client with basic auth as setup in the README.md file
const client = new Client({
ssl: {
rejectUnauthorized: false,
},
node: 'https://localhost:9200',
auth: {
username: 'admin',
password: 'myStrongPassword123!',
},
});

const start = async () => {
const sourceIndex = 'sample-index';
const rollupIndex = 'rollup-index';
const jobId = 'my-rollup-job';

Check failure on line 29 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, ubuntu-latest)

Delete `⏎`

Check failure on line 29 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, macOS-13)

Delete `⏎`


// await client.rollups.delete({ id: jobId });
if ((await client.indices.exists({ index: sourceIndex })).body)
await client.indices.delete({ index: sourceIndex });
if ((await client.indices.exists({ index: rollupIndex })).body)
await client.indices.delete({ index: rollupIndex });

/////////////////// SETUP //////////////////

// Create source index
await client.indices.create({
index: sourceIndex,
body: {
mappings: {
properties: {
timestamp: { type: 'date' },
field1: { type: 'keyword' },
field2: { type: 'integer' }

Check failure on line 48 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, ubuntu-latest)

Insert `,`

Check failure on line 48 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, macOS-13)

Insert `,`
}

Check failure on line 49 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, ubuntu-latest)

Insert `,`

Check failure on line 49 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, macOS-13)

Insert `,`
}

Check failure on line 50 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, ubuntu-latest)

Insert `,`

Check failure on line 50 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, macOS-13)

Insert `,`
}

Check failure on line 51 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, ubuntu-latest)

Insert `,`

Check failure on line 51 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, macOS-13)

Insert `,`
});

// Add some documents to the index
const now = Date.now();
for (let i = 0; i < 100; i++) {
await client.index({
index: sourceIndex,
body: {
timestamp: new Date(now - i * 3600000), // Subtract 1 hour for each document
field1: `field1_value_${i % 10}`, // Cycle through 10 different values
field2: i

Check failure on line 62 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, ubuntu-latest)

Insert `,`

Check failure on line 62 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, macOS-13)

Insert `,`
}

Check failure on line 63 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, ubuntu-latest)

Insert `,`

Check failure on line 63 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, macOS-13)

Insert `,`
});
}

// Refresh the index to make sure all documents are searchable
await client.indices.refresh({ index: sourceIndex });
//////////// ROLLUPS OPERATIONS ////////////
// Create a rollup job
await client.rollups.put({
id: jobId,
body: {
rollup: {
description: "sample rollup",

Check failure on line 75 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, ubuntu-latest)

Replace `"sample·rollup"` with `'sample·rollup'`

Check failure on line 75 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, macOS-13)

Replace `"sample·rollup"` with `'sample·rollup'`
source_index: sourceIndex,
target_index: rollupIndex,
schedule: {
interval: {
period: 1,
unit: "Minutes",

Check failure on line 81 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, ubuntu-latest)

Replace `"Minutes"` with `'Minutes'`

Check failure on line 81 in samples/rollups.js

View workflow job for this annotation

GitHub Actions / Test (16.x, macOS-13)

Replace `"Minutes"` with `'Minutes'`
start_time: 1602100553
}
},
page_size: 10,
delay: "0",
continuous: false,
dimensions: [
{
date_histogram: {
source_field: "timestamp",
fixed_interval: "1440m",
timezone: "UTC"
}
},
{
terms: {
source_field: "field1"
}
}
],
metrics: [
{
source_field: "field2",
metrics: [
{
avg: {}
},
{
max: {}
}
]
}
]
}
}
});

// Start the rollup job
await client.rollups.start({ id: jobId });

// Check the rollup job
const status = await client.rollups.get({ id: jobId });
console.log('Running Rollup Job:')
console.log(status.body);

// Wait till the rollup index is created
while (!(await client.indices.exists({ index: rollupIndex })).body) {
await new Promise(resolve => setTimeout(resolve, 5000));
}

// Explain the rollup job
const explain = await client.rollups.explain({ id: jobId });
console.log('Rollup Job Explanation:')
console.log(explain.body);

// Stop the rollup job
await client.rollups.stop({ id: jobId });

///////////////// TEAR DOWN ///////////////
await client.indices.delete({ index: sourceIndex });
await client.indices.delete({ index: rollupIndex });
await client.rollups.delete({ id: jobId });
};

start().then(() => console.log('done'));

0 comments on commit 136ea1c

Please sign in to comment.