We can put this mapping by:-
1) hard coded 2) can be read from a properties file 3) build it using Xcontentbuilder
We can use the ElasticsearchTemplate class provided by spring data to do this like :-
if (!elasticsearchTemplate.indexExists(esIndex)) {
elasticsearchTemplate.createIndex(esIndex);
elasticsearchTemplate.putMapping(esIndex, esType,mappingObject);
}
This mappingObject is a string read from properties file.
It can be like this :-
es-configs.properties
mapping={"_source": {"includes": [ "id","name","marks","grade"],"excludes": ["contact_info","phone_num"]}}
In this case we are excluding fields contact_info and phone_num. So those fields won't be included in _source of index.
This is the case when we use spring data elasticsearch.
If you are not using spring data then we can do the same with Xcontentbuilder.