@@ -28,20 +28,20 @@ export const JavascriptCreateIndexExamples: CreateIndexLanguageExamples = {
28
28
elasticsearchURL,
29
29
apiKey,
30
30
indexName,
31
- } ) => `import { Client } from " @elastic/elasticsearch"
31
+ } ) => `const { Client } = require(' @elastic/elasticsearch');
32
32
33
33
const client = new Client({
34
34
node: '${ elasticsearchURL } ',
35
35
auth: {
36
- apiKey: " ${ apiKey ?? API_KEY_PLACEHOLDER } "
36
+ apiKey: ' ${ apiKey ?? API_KEY_PLACEHOLDER } '
37
37
}
38
38
});
39
39
40
40
client.indices.create({
41
- index: " ${ indexName ?? INDEX_PLACEHOLDER } " ,
41
+ index: ' ${ indexName ?? INDEX_PLACEHOLDER } ' ,
42
42
mappings: {
43
43
properties: {
44
- text: { type: " text" }
44
+ text: { type: ' text' }
45
45
},
46
46
},
47
47
});` ,
@@ -52,21 +52,21 @@ client.indices.create({
52
52
elasticsearchURL,
53
53
apiKey,
54
54
indexName,
55
- } ) => `import { Client } from " @elastic/elasticsearch"
55
+ } ) => `const { Client } = require(' @elastic/elasticsearch');
56
56
57
57
const client = new Client({
58
58
node: '${ elasticsearchURL } ',
59
59
auth: {
60
- apiKey: " ${ apiKey ?? API_KEY_PLACEHOLDER } "
60
+ apiKey: ' ${ apiKey ?? API_KEY_PLACEHOLDER } '
61
61
}
62
62
});
63
63
64
64
client.indices.create({
65
- index: " ${ indexName ?? INDEX_PLACEHOLDER } " ,
65
+ index: ' ${ indexName ?? INDEX_PLACEHOLDER } ' ,
66
66
mappings: {
67
67
properties: {
68
- vector: { type: " dense_vector" , dims: 3 },
69
- text: { type: " text" }
68
+ vector: { type: ' dense_vector' , dims: 3 },
69
+ text: { type: ' text' }
70
70
},
71
71
},
72
72
});` ,
@@ -77,20 +77,20 @@ client.indices.create({
77
77
elasticsearchURL,
78
78
apiKey,
79
79
indexName,
80
- } ) => `import { Client } from " @elastic/elasticsearch"
80
+ } ) => `const { Client } = require(' @elastic/elasticsearch');
81
81
82
82
const client = new Client({
83
83
node: '${ elasticsearchURL } ',
84
84
auth: {
85
- apiKey: " ${ apiKey ?? API_KEY_PLACEHOLDER } "
85
+ apiKey: ' ${ apiKey ?? API_KEY_PLACEHOLDER } '
86
86
}
87
87
});
88
88
89
89
client.indices.create({
90
- index: " ${ indexName ?? INDEX_PLACEHOLDER } " ,
90
+ index: ' ${ indexName ?? INDEX_PLACEHOLDER } ' ,
91
91
mappings: {
92
92
properties: {
93
- text: { type: " semantic_text" }
93
+ text: { type: ' semantic_text' }
94
94
},
95
95
},
96
96
});` ,
@@ -104,16 +104,16 @@ export const JSIngestDataExample: IngestDataCodeDefinition = {
104
104
elasticsearchURL,
105
105
sampleDocuments,
106
106
indexName,
107
- } ) => `import { Client } from " @elastic/elasticsearch" ;
107
+ } ) => `const { Client } = require(' @elastic/elasticsearch') ;
108
108
109
109
const client = new Client({
110
110
node: '${ elasticsearchURL } ',
111
111
auth: {
112
- apiKey: " ${ apiKey ?? API_KEY_PLACEHOLDER } "
112
+ apiKey: ' ${ apiKey ?? API_KEY_PLACEHOLDER } '
113
113
},
114
114
});
115
115
116
- const index = " ${ indexName } " ;
116
+ const index = ' ${ indexName } ' ;
117
117
const docs = ${ JSON . stringify ( sampleDocuments , null , 2 ) } ;
118
118
119
119
const bulkIngestResponse = await client.helpers.bulk({
@@ -131,16 +131,16 @@ console.log(bulkIngestResponse);`,
131
131
elasticsearchURL,
132
132
indexName,
133
133
mappingProperties,
134
- } ) => `import { Client } from " @elastic/elasticsearch" ;
134
+ } ) => `const { Client } = require(' @elastic/elasticsearch') ;
135
135
136
136
const 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
+ }
141
141
});
142
142
143
- const index = " ${ indexName } " ;
143
+ const index = ' ${ indexName } ' ;
144
144
const mapping = ${ JSON . stringify ( mappingProperties , null , 2 ) } ;
145
145
146
146
const updateMappingResponse = await client.indices.putMapping({
@@ -149,3 +149,41 @@ const updateMappingResponse = await client.indices.putMapping({
149
149
});
150
150
console.log(updateMappingResponse);` ,
151
151
} ;
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