Es Mapping Ts
This library is used to generate elasticsearch mapping through typescript decorator
Installation
npm install es-mapping-ts --save
Exemple
Create the mapping
import { EsEntity, EsField } from "../lib/es-mapping-ts";
import { Budget } from "./budget.entity";
import { MasterEntity } from "./master.entity";
@EsEntity({
index: 'user',
type: 'user'
})
export class UserEntity {
@EsField({
type: "text",
analyzer : 'whitespace'
})
name: string;
@EsField({
type: "integer",
})
age: number;
@EsField({
type: 'join',
relations: { "parent" : "child"}
})
children: Array<UserEntity>;
@EsField({
type: 'object'
})
dog: Budget;
@EsField({
type: 'nested',
nestedType : MasterEntity
})
master: Array<MasterEntity>;
}
Get the generated mappings
Simply call the "uploadMappings" function
import { EsMappingService } from 'es-mapping-ts';
import { Client } from 'elasticsearch';
const esClient = new Client();
const mappings = EsMappingService.getInstance(esClient).uploadMappings();
only none readonly entity will be uploaded
or do it yourself
import { EsMappingService } from 'es-mapping-ts';
const mappings = EsMappingService.getInstance().getMappings();
Bluebird.each(mappings, async (mapping) => {
await esclient.indices.create({ index: mapping.index });
await esclient.indices.putMapping(mapping);
});
Decorator liste
@EsEntity
index | string | Allow you to define the index name |
type | string | Allow you to define the index type |
readonly | boolean | Define if the mapping must be uploaded when using uploadMappings function |
@EsField
type | string | Allow you to define the type of the index |
name | string | Allow you to define the name of the property if different from the property name |
analyzer | string | Allow you to define the elasticsearch analyzer |
fields | string | Allow you to define the elasticsearch fields |
format | string | Allow you to define the format (ie for date field) |
enabled | string | Allow you to enable ou disable the field |
null_value | string | Allow you to define the null value of the field |
copy_to | string | Allow you to copy the field value into a group field for _search |
relations | string | Define the releation for a join type |
nestedType | string | Class used to get the properties of the nested type |
Additional properties are allowed, allowing you to manage other elasticsearch properties
License
MIT