src/core/packages/data-streams/server/README.md
While its possible to use @kbn/data-streams as a standalone package by passing an appropriate esClient object during inititalize() (to handle communication with Elasticsearch), the core data streams service allows for a coordinated initialization of your data stream indices as part of the CoreSetup and CoreStart plugin initalization routine.
Some additional benefits to using the core data streams service:
core.elasticsearch.client.asInternalUser), but can rely on the internal behavior of this service during initializeAllDataStreams() which will take care of passing it as soon as its available.Please refer to examples/data_streams_example/server/index.ts
export const plugin = (ctx: PluginInitializerContext) => {
return {
setup({ dataStreams }: CoreSetup) {
dataStreams.registerDataStream(dataStream);
},
start({ dataStreams }: CoreStart) {
const initializeClient = async () => {
return await dataStreams.initializeClient<typeof dataStreamMappings, DataStreamDocument>(
dataStream.name
);
};
return {
createSpecialDocument: async () => {
const client = await initializeClient();
const document: DataStreamDocument = {
'@timestamp': +new Date(),
name: 'John Doe',
description: 'This is a test document for my data stream.',
age: 30,
unMappedField: 'Unmapped field but exists in the document _source',
};
const result = await client.create({
documents: [document],
});
ctx.logger.get('data-streams-example').info(JSON.stringify(result, null, 2));
},
getDocument: async (id: string) => {
const client = await initializeClient();
const result = await client.search({
query: {
term: { description: 'This is a test document for my data stream.' },
},
});
return result;
},
};
},
stop() {},
};
};