@@ -28,20 +28,20 @@ export const JavascriptCreateIndexExamples: CreateIndexLanguageExamples = {
2828 elasticsearchURL,
2929 apiKey,
3030 indexName,
31- } ) => `import { Client } from " @elastic/elasticsearch"
31+ } ) => `const { Client } = require(' @elastic/elasticsearch');
3232
3333const client = new Client({
3434 node: '${ elasticsearchURL } ',
3535 auth: {
36- apiKey: " ${ apiKey ?? API_KEY_PLACEHOLDER } "
36+ apiKey: ' ${ apiKey ?? API_KEY_PLACEHOLDER } '
3737 }
3838});
3939
4040client.indices.create({
41- index: " ${ indexName ?? INDEX_PLACEHOLDER } " ,
41+ index: ' ${ indexName ?? INDEX_PLACEHOLDER } ' ,
4242 mappings: {
4343 properties: {
44- text: { type: " text" }
44+ text: { type: ' text' }
4545 },
4646 },
4747});` ,
@@ -52,21 +52,21 @@ client.indices.create({
5252 elasticsearchURL,
5353 apiKey,
5454 indexName,
55- } ) => `import { Client } from " @elastic/elasticsearch"
55+ } ) => `const { Client } = require(' @elastic/elasticsearch');
5656
5757const client = new Client({
5858 node: '${ elasticsearchURL } ',
5959 auth: {
60- apiKey: " ${ apiKey ?? API_KEY_PLACEHOLDER } "
60+ apiKey: ' ${ apiKey ?? API_KEY_PLACEHOLDER } '
6161 }
6262});
6363
6464client.indices.create({
65- index: " ${ indexName ?? INDEX_PLACEHOLDER } " ,
65+ index: ' ${ indexName ?? INDEX_PLACEHOLDER } ' ,
6666 mappings: {
6767 properties: {
68- vector: { type: " dense_vector" , dims: 3 },
69- text: { type: " text" }
68+ vector: { type: ' dense_vector' , dims: 3 },
69+ text: { type: ' text' }
7070 },
7171 },
7272});` ,
@@ -77,20 +77,20 @@ client.indices.create({
7777 elasticsearchURL,
7878 apiKey,
7979 indexName,
80- } ) => `import { Client } from " @elastic/elasticsearch"
80+ } ) => `const { Client } = require(' @elastic/elasticsearch');
8181
8282const client = new Client({
8383 node: '${ elasticsearchURL } ',
8484 auth: {
85- apiKey: " ${ apiKey ?? API_KEY_PLACEHOLDER } "
85+ apiKey: ' ${ apiKey ?? API_KEY_PLACEHOLDER } '
8686 }
8787});
8888
8989client.indices.create({
90- index: " ${ indexName ?? INDEX_PLACEHOLDER } " ,
90+ index: ' ${ indexName ?? INDEX_PLACEHOLDER } ' ,
9191 mappings: {
9292 properties: {
93- text: { type: " semantic_text" }
93+ text: { type: ' semantic_text' }
9494 },
9595 },
9696});` ,
@@ -104,16 +104,16 @@ export const JSIngestDataExample: IngestDataCodeDefinition = {
104104 elasticsearchURL,
105105 sampleDocuments,
106106 indexName,
107- } ) => `import { Client } from " @elastic/elasticsearch" ;
107+ } ) => `const { Client } = require(' @elastic/elasticsearch') ;
108108
109109const client = new Client({
110110 node: '${ elasticsearchURL } ',
111111 auth: {
112- apiKey: " ${ apiKey ?? API_KEY_PLACEHOLDER } "
112+ apiKey: ' ${ apiKey ?? API_KEY_PLACEHOLDER } '
113113 },
114114});
115115
116- const index = " ${ indexName } " ;
116+ const index = ' ${ indexName } ' ;
117117const docs = ${ JSON . stringify ( sampleDocuments , null , 2 ) } ;
118118
119119const bulkIngestResponse = await client.helpers.bulk({
@@ -131,16 +131,16 @@ console.log(bulkIngestResponse);`,
131131 elasticsearchURL,
132132 indexName,
133133 mappingProperties,
134- } ) => `import { Client } from " @elastic/elasticsearch" ;
134+ } ) => `const { Client } = require(' @elastic/elasticsearch') ;
135135
136136const client = new Client({
137- node: '${ elasticsearchURL } ',
138- auth: {
139- apiKey: " ${ apiKey ?? API_KEY_PLACEHOLDER } "
140- }
137+ node: '${ elasticsearchURL } ',
138+ auth: {
139+ apiKey: ' ${ apiKey ?? API_KEY_PLACEHOLDER } '
140+ }
141141});
142142
143- const index = " ${ indexName } " ;
143+ const index = ' ${ indexName } ' ;
144144const mapping = ${ JSON . stringify ( mappingProperties , null , 2 ) } ;
145145
146146const updateMappingResponse = await client.indices.putMapping({
@@ -149,3 +149,41 @@ const updateMappingResponse = await client.indices.putMapping({
149149});
150150console.log(updateMappingResponse);` ,
151151} ;
152+
153+ export const JSSemanticIngestDataExample : IngestDataCodeDefinition = {
154+ ...JSIngestDataExample ,
155+ ingestCommand : ( {
156+ apiKey,
157+ elasticsearchURL,
158+ sampleDocuments,
159+ indexName,
160+ } ) => `const { Client } = require('@elastic/elasticsearch');
161+
162+ const client = new Client({
163+ node: '${ elasticsearchURL } ',
164+ auth: {
165+ apiKey: '${ apiKey ?? API_KEY_PLACEHOLDER } '
166+ },
167+ });
168+
169+ // Set the ingestion timeout to 5 minutes, to allow for semantic ingestion
170+ // to complete. The initial ingest with semantic_text fields may take longer
171+ // while the model loads, or ML nodes are allocated.
172+ const timeout = '5m';
173+
174+ const index = '${ indexName } ';
175+ const docs = ${ JSON . stringify ( sampleDocuments , null , 2 ) } ;
176+
177+ const bulkIngestResponse = await client.helpers.bulk({
178+ index,
179+ datasource: docs,
180+ timeout,
181+ onDocument() {
182+ return {
183+ index: {},
184+ };
185+ }
186+ });
187+
188+ console.log(bulkIngestResponse);` ,
189+ } ;
0 commit comments