Skip to content

Commit 7ae9d44

Browse files
search: add longer timeouts to ingest examples for semantic_text
1 parent ab77347 commit 7ae9d44

File tree

3 files changed

+95
-26
lines changed

3 files changed

+95
-26
lines changed

x-pack/solutions/search/plugins/search_indices/public/code_examples/ingest_data.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import { i18n } from '@kbn/i18n';
99
import { IngestDataCodeExamples } from '../types';
1010

11-
import { JSIngestDataExample } from './javascript';
12-
import { PythonIngestDataExample } from './python';
11+
import { JSIngestDataExample, JSSemanticIngestDataExample } from './javascript';
12+
import { PythonIngestDataExample, PythonSemanticIngestDataExample } from './python';
1313
import { ConsoleIngestDataExample } from './sense';
1414
import { CurlIngestDataExample } from './curl';
1515
import { INSTALL_INSTRUCTIONS_TITLE, INSTALL_INSTRUCTIONS_DESCRIPTION } from './constants';
@@ -78,6 +78,6 @@ export const SemanticIngestDataCodeExamples: IngestDataCodeExamples = {
7878
},
7979
sense: ConsoleIngestDataExample,
8080
curl: CurlIngestDataExample,
81-
python: PythonIngestDataExample,
82-
javascript: JSIngestDataExample,
81+
python: PythonSemanticIngestDataExample,
82+
javascript: JSSemanticIngestDataExample,
8383
};

x-pack/solutions/search/plugins/search_indices/public/code_examples/javascript.ts

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3333
const client = new Client({
3434
node: '${elasticsearchURL}',
3535
auth: {
36-
apiKey: "${apiKey ?? API_KEY_PLACEHOLDER}"
36+
apiKey: '${apiKey ?? API_KEY_PLACEHOLDER}'
3737
}
3838
});
3939
4040
client.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
5757
const client = new Client({
5858
node: '${elasticsearchURL}',
5959
auth: {
60-
apiKey: "${apiKey ?? API_KEY_PLACEHOLDER}"
60+
apiKey: '${apiKey ?? API_KEY_PLACEHOLDER}'
6161
}
6262
});
6363
6464
client.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
8282
const client = new Client({
8383
node: '${elasticsearchURL}',
8484
auth: {
85-
apiKey: "${apiKey ?? API_KEY_PLACEHOLDER}"
85+
apiKey: '${apiKey ?? API_KEY_PLACEHOLDER}'
8686
}
8787
});
8888
8989
client.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
109109
const 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}';
117117
const docs = ${JSON.stringify(sampleDocuments, null, 2)};
118118
119119
const 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
136136
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+
}
141141
});
142142
143-
const index = "${indexName}";
143+
const index = '${indexName}';
144144
const mapping = ${JSON.stringify(mappingProperties, null, 2)};
145145
146146
const updateMappingResponse = await client.indices.putMapping({
@@ -149,3 +149,41 @@ const updateMappingResponse = await client.indices.putMapping({
149149
});
150150
console.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+
};

x-pack/solutions/search/plugins/search_indices/public/code_examples/python.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,39 @@ mapping_response = client.indices.put_mapping(index=index_name, body=mappings)
133133
print(mapping_response)
134134
`;
135135

136+
const semanticIngestCommand: IngestCodeSnippetFunction = ({
137+
elasticsearchURL,
138+
apiKey,
139+
indexName,
140+
sampleDocuments,
141+
}) => `from elasticsearch import Elasticsearch, helpers
142+
143+
# Set the ingestion timeout to 5 minutes, to allow time for semantic ingestion
144+
# to complete. The initial ingest with semantic_text fields may take longer
145+
# while the model loads, or ML nodes are allocated.
146+
ingestion_timeout=300
147+
148+
client = Elasticsearch(
149+
"${elasticsearchURL}",
150+
api_key="${apiKey ?? API_KEY_PLACEHOLDER}",
151+
request_timeout=ingestion_timeout,
152+
retry_on_timeout=True,
153+
)
154+
155+
index_name = "${indexName}"
156+
157+
docs = ${JSON.stringify(sampleDocuments, null, 4)}
158+
159+
bulk_response = helpers.bulk(client, docs, index=index_name)
160+
print(bulk_response)`;
161+
136162
export const PythonIngestDataExample: IngestDataCodeDefinition = {
137163
installCommand: PYTHON_INSTALL_CMD,
138164
ingestCommand: ingestionCommand,
139165
updateMappingsCommand,
140166
};
167+
168+
export const PythonSemanticIngestDataExample: IngestDataCodeDefinition = {
169+
...PythonIngestDataExample,
170+
ingestCommand: semanticIngestCommand,
171+
};

0 commit comments

Comments
 (0)