Skip to content

Commit 12051c6

Browse files
Issue 179 (#184)
#179 Update data type for challengeId in the index, by creating a new index
1 parent 5f4a69b commit 12051c6

9 files changed

+53
-9
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ This script will load the data from `scripts/data` directory into ES
116116
npm run start
117117
```
118118

119+
#### Duplicating the ES Index
120+
121+
To duplicate the existing ES Index (from the `ES_INDEX` to `ES_INDEX_V2` based on the configs in `config/default.js`) run `npm run create-new-index`
122+
119123
#### Linting JS files
120124

121125
```

config/default.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ module.exports = {
2828
AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID,
2929
AUTH0_CLIENT_SECRET: process.env.AUTH0_CLIENT_SECRET,
3030
esConfig: {
31-
HOST: process.env.ES_HOST,
31+
HOST: process.env.ES_HOST || 'localhost:9200',
3232
API_VERSION: process.env.ES_API_VERSION || '6.3',
3333
ES_INDEX: process.env.ES_INDEX || 'submission',
34+
ES_INDEX_V2: process.env.ES_INDEX_V2 || 'new_submission',
3435
ES_TYPE: process.env.ES_TYPE || '_doc' // ES 6.x accepts only 1 Type per index and it's mandatory to define it
3536
},
3637
PAGE_SIZE: process.env.PAGE_SIZE || 20,

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"create-tables": "node scripts/createTables.js",
1212
"init-db": "node scripts/importData.js",
1313
"create-index": "node scripts/createIndex.js",
14+
"create-new-index": "node scripts/createNewIndex.js",
1415
"delete-index": "node scripts/deleteIndex.js",
1516
"init-es": "node scripts/loadES.js",
1617
"db-to-es": "node scripts/migrateFromDBToES.js",

scripts/ESloadHelper.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const esClient = helper.getEsClient()
2020
function deleteDatafromES () {
2121
logger.info('Clear data from ES if any')
2222
const filter = {
23-
index: config.get('esConfig.ES_INDEX'),
23+
index: config.get('esConfig.ES_INDEX_V2'),
2424
type: config.get('esConfig.ES_TYPE'),
2525
q: '*'
2626
}
@@ -34,7 +34,7 @@ function * loadReviewTypes () {
3434
const promises = []
3535
reviewTypes.forEach((reviewType) => {
3636
const record = {
37-
index: config.get('esConfig.ES_INDEX'),
37+
index: config.get('esConfig.ES_INDEX_V2'),
3838
type: config.get('esConfig.ES_TYPE'),
3939
id: reviewType.id,
4040
body: _.extend({ resource: 'reviewType' }, reviewType)
@@ -51,7 +51,7 @@ function * loadSubmissions () {
5151
const promises = []
5252
submissions.forEach((submission) => {
5353
const record = {
54-
index: config.get('esConfig.ES_INDEX'),
54+
index: config.get('esConfig.ES_INDEX_V2'),
5555
type: config.get('esConfig.ES_TYPE'),
5656
id: submission.id,
5757
body: _.extend({ resource: 'submission' }, submission)
@@ -68,7 +68,7 @@ function * loadReviews () {
6868
const promises = []
6969
reviews.forEach((review) => {
7070
const record = {
71-
index: config.get('esConfig.ES_INDEX'),
71+
index: config.get('esConfig.ES_INDEX_V2'),
7272
type: config.get('esConfig.ES_TYPE'),
7373
id: review.id,
7474
body: _.extend({ resource: 'review' }, review)
@@ -85,7 +85,7 @@ function * loadReviewSummations () {
8585
const promises = []
8686
reviewSummations.forEach((reviewSummation) => {
8787
const record = {
88-
index: config.get('esConfig.ES_INDEX'),
88+
index: config.get('esConfig.ES_INDEX_V2'),
8989
type: config.get('esConfig.ES_TYPE'),
9090
id: reviewSummation.id,
9191
body: _.extend({ resource: 'reviewSummation' }, reviewSummation)

scripts/createIndex.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ co(function * createIndex () {
4444
}
4545
}
4646
yield esClient.indices.create({
47-
index: config.get('esConfig.ES_INDEX'),
47+
index: config.get('esConfig.ES_INDEX_V2'),
4848
body
4949
})
5050
logger.info('ES Index creation succeeded!')

scripts/createNewIndex.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copies the existing index to a new index and updates the type of challengeId
3+
* to be keyword (It is long in the source index)
4+
*/
5+
6+
const co = require('co')
7+
const config = require('config')
8+
const logger = require('../src/common/logger')
9+
const helper = require('../src/common/helper')
10+
11+
co(function * createIndex () {
12+
logger.info('ES Index creation started!')
13+
const esClient = helper.getEsClient()
14+
const indices = yield esClient.indices.get({
15+
index: config.get('esConfig.ES_INDEX')
16+
})
17+
const existingIndex = indices[config.get('esConfig.ES_INDEX')]
18+
const existingMappings = existingIndex.mappings[config.get('esConfig.ES_TYPE')]
19+
const body = { mappings: {} }
20+
body.mappings[config.get('esConfig.ES_TYPE')] = {
21+
properties: {
22+
...existingMappings.properties,
23+
challengeId: { type: 'keyword' }
24+
}
25+
}
26+
yield esClient.indices.create({
27+
index: config.get('esConfig.ES_INDEX_V2'),
28+
body
29+
})
30+
logger.info('ES Index creation succeeded!')
31+
process.exit(0)
32+
}).catch((err) => {
33+
logger.logFullError(err)
34+
process.exit(1)
35+
})

scripts/data/Submissions.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"id": "a12a4180-65aa-42ec-a945-5fd21dec0501",
44
"type": "ContestSubmission",
55
"url": "https://software.topcoder.com/review/actions/DownloadContestSubmission?uid=123456",
6-
"memberId": 40493050,
6+
"memberId": "b24d4180-65aa-42ec-a945-5fd21dec0502",
77
"challengeId": 30055732,
88
"review": [
99
{

scripts/deleteIndex.js

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ co(function * deleteIndex () {
1313
yield esClient.indices.delete({
1414
index: config.get('esConfig.ES_INDEX')
1515
})
16+
yield esClient.indices.delete({
17+
index: config.get('esConfig.ES_INDEX_V2')
18+
})
1619
logger.info('ES Index deletion succeeded!')
1720
process.exit(0)
1821
}).catch((err) => {

scripts/migrateFromDBToES.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function * migrateRecords (tableName) {
3030
logger.debug(`Number of ${tableName}s fetched from DB - ` + totalRecords)
3131
for (let i = 0; i < totalRecords; i++) {
3232
const record = {
33-
index: config.get('esConfig.ES_INDEX'),
33+
index: config.get('esConfig.ES_INDEX_V2'),
3434
type: config.get('esConfig.ES_TYPE'),
3535
id: records.Items[i].id,
3636
body: {

0 commit comments

Comments
 (0)