Skip to content

Commit

Permalink
add shards and replicas configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Varner committed Dec 15, 2022
1 parent 1cbb939 commit 141ffc5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- (Experimental) Aggregation Extension endpoint /aggregate
- Added pre-hook and post-hook Lambda examples
- POST /collections endpoint to create collections
- Configuration of shards and replicas for the indices containing Items can now be done
with environment variables ITEMS_INDICIES_NUM_OF_SHARDS and ITEMS_INDICIES_NUM_OF_REPLICAS.

### Changed

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ There are some settings that should be reviewed and updated as needeed in the se
| OPENSEARCH_PASSWORD | The password to authenticate to OpenSearch with if fine-grained access control is enabled. | |
| OPENSEARCH_CREDENTIALS_SECRET_ID | The AWS Secrets Manager secret to retrieve the username and password from, to authenticate to OpenSearch with if fine-grained access control is enabled. | |


| ITEMS_INDICIES_NUM_OF_SHARDS | Configure the number of shards for the indices that contain Items. | none |
| ITEMS_INDICIES_NUM_OF_REPLICAS | Configure the number of replicas for the indices that contain Items. | none |

After reviewing the settings, build and deploy:

```shell
Expand Down
26 changes: 16 additions & 10 deletions fixtures/collections.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
const dynamicTemplates = require('./dynamicTemplates')

module.exports = {
mappings: {
numeric_detection: false,
dynamic_templates: dynamicTemplates.templates,
properties: {
'extent.spatial.bbox': { type: 'long' },
'extent.temporal.interval': { type: 'date' },
providers: { type: 'object', enabled: false },
links: { type: 'object', enabled: false },
item_assets: { type: 'object', enabled: false }
const collectionsIndexConfiguration = function () {
return {
mappings: {
numeric_detection: false,
dynamic_templates: dynamicTemplates.templates,
properties: {
'extent.spatial.bbox': { type: 'long' },
'extent.temporal.interval': { type: 'date' },
providers: { type: 'object', enabled: false },
links: { type: 'object', enabled: false },
item_assets: { type: 'object', enabled: false }
}
}
}
}

module.exports = {
collectionsIndexConfiguration
}
23 changes: 21 additions & 2 deletions fixtures/items.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
const dynamicTemplates = require('./dynamicTemplates')

module.exports = {
mappings: {
const itemsIndexConfiguration = function () {
const numberOfShards = process.env.ITEMS_INDICIES_NUM_OF_SHARDS
const numberOfReplicas = process.env.ITEMS_INDICIES_NUM_OF_REPLICAS

const config = {}

if (numberOfShards || numberOfReplicas) {
const index = {}
if (numberOfShards) index.number_of_shards = Number(numberOfShards)
if (numberOfReplicas) index.number_of_replicas = Number(numberOfReplicas)

config.settings = { index }
}

config.mappings = {
numeric_detection: false,
dynamic_templates: dynamicTemplates.templates,
properties: {
Expand All @@ -28,4 +41,10 @@ module.exports = {
}
}
}

return config
}

module.exports = {
itemsIndexConfiguration
}
11 changes: 6 additions & 5 deletions src/lib/esClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const { createAWSConnection: createAWSConnectionES,

const logger = console //require('./logger')

const collectionsMapping = require('../../fixtures/collections')
const itemsMapping = require('../../fixtures/items')
const { collectionsIndexConfiguration } = require('../../fixtures/collections')
const { itemsIndexConfiguration } = require('../../fixtures/items')

let _esClient

Expand Down Expand Up @@ -85,13 +85,14 @@ async function esClient() {
async function createIndex(index) {
const client = await esClient()
const exists = await client.indices.exists({ index })
const mapping = index === 'collections' ? collectionsMapping : itemsMapping
const indexConfiguration = index === 'collections'
? collectionsIndexConfiguration() : itemsIndexConfiguration()
if (!exists.body) {
logger.info(`${index} does not exist, creating...`)
try {
await client.indices.create({ index, body: mapping })
await client.indices.create({ index, body: indexConfiguration })
logger.info(`Created index ${index}`)
logger.debug(`Mapping: ${JSON.stringify(mapping)}`)
logger.debug(`Mapping: ${JSON.stringify(indexConfiguration)}`)
} catch (error) {
const debugMessage = `Error creating index ${index}, already created: ${error}`
logger.debug(debugMessage)
Expand Down

0 comments on commit 141ffc5

Please sign in to comment.