-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create projections and populations to generate ingest data for elasti…
…csearch
- Loading branch information
Showing
8 changed files
with
331 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module.exports = schemas => { | ||
return Object.keys(schemas).reduce((accumulator, schemaKey) => { | ||
if (schemas[schemaKey].class === 'collection') { | ||
accumulator[schemaKey] = generateProjections(schemaKey, schemas) | ||
} | ||
|
||
return accumulator | ||
}, {}) | ||
} | ||
|
||
const generateProjections = (schemaKey, schemas) => { | ||
return Object.keys(schemas[schemaKey].fields).reduce((accumulator, fKey) => { | ||
if (!(schemas[schemaKey].fields[fKey].type === 'reference' && schemas[schemaKey].fields[fKey].ref === schemaKey) | ||
&& !(schemas[schemaKey].fields[fKey].type === 'array' && schemas[schemaKey].fields[fKey].item.type === 'reference' && schemas[schemaKey].fields[fKey].item.ref === schemaKey) | ||
) { | ||
accumulator[fKey] = 1 | ||
} | ||
|
||
return accumulator | ||
}, {}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
exports.generateElasticMappings = require('./generateElasticMappings') | ||
exports.generateElasticMappings = require('./generateElasticMappings') | ||
exports.generateElasticProjections = require('./generateElasticProjections') | ||
exports.generateElasticPopulations = require('./generateElasticPopulations') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
const expect = require('chai').expect | ||
const generateElasticPopulations = require('../src/elasticsearch').generateElasticPopulations | ||
|
||
const schemas = { | ||
person: { | ||
class: 'collection', | ||
fields: { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
es_indexed: true, | ||
es_keyword: true | ||
}, | ||
pokedex: { | ||
type: 'reference', | ||
required: true, | ||
ref: 'pokedex', | ||
es_indexed: true | ||
}, | ||
region: { | ||
type: 'reference', | ||
required: true, | ||
ref: 'region', | ||
es_indexed: true | ||
}, | ||
visited_regions: { | ||
type: 'array', | ||
required: true, | ||
item: { | ||
type: 'reference', | ||
required: true, | ||
ref: 'region', | ||
es_indexed: true | ||
} | ||
}, | ||
aliases: { | ||
type: 'array', | ||
required: false, | ||
item: { | ||
type: 'reference', | ||
required: false, | ||
ref: 'person', | ||
es_indexed: true | ||
} | ||
} | ||
} | ||
}, | ||
|
||
pokedex: { | ||
class: 'embedded', | ||
fields: { | ||
pokemon: { | ||
type: 'array', | ||
required: true, | ||
item: { | ||
type: 'reference', | ||
required: true, | ||
ref: 'pokemon', | ||
es_indexed: true | ||
} | ||
}, | ||
model: { | ||
type: 'reference', | ||
required: true, | ||
ref: 'model', | ||
es_indexed: true | ||
} | ||
} | ||
}, | ||
|
||
pokemon: { | ||
class: 'collection', | ||
fields: { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
es_indexed: true, | ||
es_keyword: true | ||
}, | ||
region: { | ||
type: 'reference', | ||
required: true, | ||
ref: 'region', | ||
es_indexed: true | ||
}, | ||
evolves_to: { | ||
type: 'reference', | ||
required: false, | ||
ref: 'pokemon', | ||
es_indexed: true | ||
} | ||
} | ||
}, | ||
|
||
region: { | ||
class: 'collection', | ||
fields: { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
es_indexed: true, | ||
es_keyword: true | ||
} | ||
} | ||
}, | ||
|
||
model: { | ||
class: 'collection', | ||
fields: { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
es_indexed: true, | ||
es_keyword: true | ||
} | ||
} | ||
} | ||
} | ||
|
||
describe('generateElasticPopulations', () => { | ||
it('should list all collection level documents', () => { | ||
const populations = generateElasticPopulations(schemas) | ||
|
||
expect(populations).to.have.all.keys('person', 'pokemon', 'region', 'model') | ||
}) | ||
|
||
it('should generate populations for all collection level fields', () => { | ||
const populations = generateElasticPopulations(schemas) | ||
|
||
// person | ||
expect(populations.person()).to.be.an('array').that.deep.includes({ | ||
path: 'region', | ||
model: 'region', | ||
select: { | ||
name: 1 | ||
} | ||
}) | ||
|
||
// pokemon | ||
expect(populations.pokemon()).to.be.an('array').that.deep.includes({ | ||
path: 'region', | ||
model: 'region', | ||
select: { | ||
name: 1 | ||
} | ||
}) | ||
|
||
// region | ||
expect(populations.region()).to.be.empty | ||
|
||
// model | ||
expect(populations.model()).to.be.empty | ||
}) | ||
|
||
it('should generate populations for all array fields whose items are collection level fields', () => { | ||
const populations = generateElasticPopulations(schemas) | ||
|
||
// person | ||
expect(populations.person()).to.be.an('array').that.deep.includes({ | ||
path: 'visited_regions', | ||
model: 'region', | ||
select: { | ||
name: 1 | ||
} | ||
}) | ||
}) | ||
|
||
it('should generate nested population paths for fields with collection level references in embedded documents', () => { | ||
const populations = generateElasticPopulations(schemas) | ||
|
||
// person | ||
expect(populations.person()).to.be.an('array').that.deep.includes({ | ||
path: 'pokedex.model', | ||
model: 'model', | ||
select: { | ||
name: 1 | ||
} | ||
}) | ||
}) | ||
|
||
it('should generate nested population paths for fields with collection level references inside arrays in embedded documents', () => { | ||
const populations = generateElasticPopulations(schemas) | ||
|
||
// person | ||
expect(populations.person()).to.be.an('array').that.deep.includes({ | ||
path: 'pokedex.pokemon', | ||
model: 'pokemon', | ||
select: { | ||
name: 1, | ||
region: 1 | ||
}, | ||
populate: [{ | ||
path: 'region', | ||
model: 'region', | ||
select: { | ||
name: 1 | ||
} | ||
}] | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const expect = require('chai').expect | ||
const generateElasticProjections = require('../src/elasticsearch').generateElasticProjections | ||
|
||
const schemas = { | ||
person: { | ||
class: 'collection', | ||
fields: { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
es_indexed: true, | ||
es_keyword: false | ||
}, | ||
contact: { | ||
type: 'reference', | ||
ref: 'contact', | ||
required: true, | ||
es_indexed: true | ||
}, | ||
address: { | ||
type: 'reference', | ||
ref: 'address', | ||
required: false, | ||
es_indexed: true | ||
}, | ||
spouse: { | ||
type: 'reference', | ||
ref: 'person', | ||
required: false, | ||
es_indexed: true | ||
}, | ||
aliases: { | ||
type: 'array', | ||
required: false, | ||
item: { | ||
type: 'reference', | ||
required: false, | ||
ref: 'person', | ||
es_indexed: true | ||
} | ||
} | ||
} | ||
}, | ||
address: { | ||
class: 'collection', | ||
fields: { | ||
line_1: { | ||
type: 'string', | ||
required: true, | ||
es_indexed: true, | ||
es_keyword: true | ||
} | ||
} | ||
}, | ||
contact: { | ||
class: 'embedded', | ||
fields: { | ||
email: { | ||
type: 'string', | ||
required: true, | ||
es_indexed: true, | ||
es_keyword: true | ||
} | ||
} | ||
} | ||
} | ||
|
||
describe('generateElasticProjections', () => { | ||
it('should generate all collection documents, projecting all fields except self referential fields', () => { | ||
let projections = generateElasticProjections(schemas) | ||
|
||
expect(projections).to.deep.equal({ | ||
person: { | ||
name: 1, | ||
contact: 1, | ||
address: 1 | ||
}, | ||
address: { | ||
line_1: 1 | ||
} | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const config = require('../data/configurationSchemas.js') | ||
const generateProjections = require('../../src/elasticsearch/generateProjections.js') | ||
|
||
let projections = generateProjections(config) | ||
|
||
console.log(projections) |