Skip to content

[Search] add longer timeouts to ingest examples for semantic_text #221315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import { i18n } from '@kbn/i18n';
import { IngestDataCodeExamples } from '../types';

import { JSIngestDataExample } from './javascript';
import { PythonIngestDataExample } from './python';
import { JSIngestDataExample, JSSemanticIngestDataExample } from './javascript';
import { PythonIngestDataExample, PythonSemanticIngestDataExample } from './python';
import { ConsoleIngestDataExample } from './sense';
import { CurlIngestDataExample } from './curl';
import { INSTALL_INSTRUCTIONS_TITLE, INSTALL_INSTRUCTIONS_DESCRIPTION } from './constants';
Expand Down Expand Up @@ -78,6 +78,6 @@ export const SemanticIngestDataCodeExamples: IngestDataCodeExamples = {
},
sense: ConsoleIngestDataExample,
curl: CurlIngestDataExample,
python: PythonIngestDataExample,
javascript: JSIngestDataExample,
python: PythonSemanticIngestDataExample,
javascript: JSSemanticIngestDataExample,
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import { i18n } from '@kbn/i18n';
import { API_KEY_PLACEHOLDER, INDEX_PLACEHOLDER } from '../constants';
import {
CodeLanguage,
CodeSnippetParameters,
IngestDataCodeDefinition,
SearchCodeDefinition,
SearchCodeSnippetFunction,
SearchCodeSnippetParameters,
} from '../types';
import { CreateIndexLanguageExamples } from './types';

Expand All @@ -25,178 +27,146 @@ export const JAVASCRIPT_INFO: CodeLanguage = {
};

const INSTALL_CMD = `npm install @elastic/elasticsearch`;
const CLIENT_SERVERLESS_OPTION = `\n serverMode: 'serverless',`;

const createClientSnippet = ({
apiKey,
elasticsearchURL,
isServerless,
}: CodeSnippetParameters): string => `const client = new Client({
node: '${elasticsearchURL}',
auth: {
apiKey: '${apiKey ?? API_KEY_PLACEHOLDER}'
},${isServerless ? CLIENT_SERVERLESS_OPTION : ''}
});`;

export const JavascriptCreateIndexExamples: CreateIndexLanguageExamples = {
default: {
installCommand: INSTALL_CMD,
createIndex: ({
elasticsearchURL,
apiKey,
indexName,
}) => `import { Client } from "@elastic/elasticsearch"
createIndex: (args) => `const { Client } = require('@elastic/elasticsearch');

const client = new Client({
node: '${elasticsearchURL}',
auth: {
apiKey: "${apiKey ?? API_KEY_PLACEHOLDER}"
}
});
${createClientSnippet(args)}

async function run() {
await client.indices.create({
index: "${indexName ?? INDEX_PLACEHOLDER}",
mappings: {
properties: {
text: { type: "text"}
},
const createResponse = await client.indices.create({
index: '${args.indexName ?? INDEX_PLACEHOLDER}',
mappings: {
properties: {
text: { type: 'text'}
},
});
}
run().catch(console.log);`,
console.log(createResponse);`,
},
dense_vector: {
installCommand: INSTALL_CMD,
createIndex: ({
elasticsearchURL,
apiKey,
indexName,
}) => `import { Client } from "@elastic/elasticsearch"
createIndex: (args) => `const { Client } = require('@elastic/elasticsearch');

const client = new Client({
node: '${elasticsearchURL}',
auth: {
apiKey: "${apiKey ?? API_KEY_PLACEHOLDER}"
}
});
${createClientSnippet(args)}

async function run() {
await client.indices.create({
index: "${indexName ?? INDEX_PLACEHOLDER}",
mappings: {
properties: {
vector: { type: "dense_vector", dims: 3 },
text: { type: "text"}
},
const createResponse = await client.indices.create({
index: '${args.indexName ?? INDEX_PLACEHOLDER}',
mappings: {
properties: {
vector: { type: 'dense_vector', dims: 3 },
text: { type: 'text'}
},
});
}
run().catch(console.log);`,
console.log(createResponse);`,
},
semantic: {
installCommand: INSTALL_CMD,
createIndex: ({
elasticsearchURL,
apiKey,
indexName,
}) => `import { Client } from "@elastic/elasticsearch"
createIndex: (args) => `const { Client } = require('@elastic/elasticsearch');

const client = new Client({
node: '${elasticsearchURL}',
auth: {
apiKey: "${apiKey ?? API_KEY_PLACEHOLDER}"
}
});
${createClientSnippet(args)}

async function run() {
await client.indices.create({
index: "${indexName ?? INDEX_PLACEHOLDER}",
mappings: {
properties: {
text: { type: "semantic_text"}
},
const createResponse = client.indices.create({
index: '${args.indexName ?? INDEX_PLACEHOLDER}',
mappings: {
properties: {
text: { type: 'semantic_text'}
},
});
}
run().catch(console.log);`,
console.log(createResponse);`,
},
};

export const JSIngestDataExample: IngestDataCodeDefinition = {
installCommand: INSTALL_CMD,
ingestCommand: ({
apiKey,
elasticsearchURL,
sampleDocuments,
indexName,
}) => `import { Client } from "@elastic/elasticsearch";

const client = new Client({
node: '${elasticsearchURL}',
auth: {
apiKey: "${apiKey ?? API_KEY_PLACEHOLDER}"
},
});
ingestCommand: (args) => `const { Client } = require('@elastic/elasticsearch');

const index = "${indexName}";
const docs = ${JSON.stringify(sampleDocuments, null, 2)};

async function run() {}
const bulkIngestResponse = await client.helpers.bulk({
index,
datasource: docs,
onDocument() {
return {
index: {},
};
}
});
console.log(bulkIngestResponse);
}
run().catch(console.log);`,
updateMappingsCommand: ({
apiKey,
elasticsearchURL,
indexName,
mappingProperties,
}) => `import { Client } from "@elastic/elasticsearch";

const client = new Client({
node: '${elasticsearchURL}',
auth: {
apiKey: "${apiKey ?? API_KEY_PLACEHOLDER}"
}
${createClientSnippet(args)}

const index = '${args.indexName}';
const docs = ${JSON.stringify(args.sampleDocuments, null, 2)};

const bulkIngestResponse = await client.helpers.bulk({
index,
datasource: docs,
onDocument() {
return {
index: {},
};
}
});
console.log(bulkIngestResponse);`,
updateMappingsCommand: (args) => `const { Client } = require('@elastic/elasticsearch');

async function run() {
const index = "${indexName}";
const mapping = ${JSON.stringify(mappingProperties, null, 2)};
${createClientSnippet(args)}

const updateMappingResponse = await client.indices.putMapping({
index,
properties: mapping,
});
console.log(updateMappingResponse);
}
run().catch(console.log);`,
const index = '${args.indexName}';
const mapping = ${JSON.stringify(args.mappingProperties, null, 2)};

const updateMappingResponse = await client.indices.putMapping({
index,
properties: mapping,
});
console.log(updateMappingResponse);`,
};

const searchCommand: SearchCodeSnippetFunction = ({
elasticsearchURL,
apiKey,
indexName,
queryObject,
}) => `import { Client } from "@elastic/elasticsearch";
const searchCommand: SearchCodeSnippetFunction = (
args: SearchCodeSnippetParameters
) => `import { Client } from '@elastic/elasticsearch';

const client = new Client({
node: '${elasticsearchURL}',
auth: {
apiKey: "${apiKey ?? API_KEY_PLACEHOLDER}"
}
});
${createClientSnippet(args)}

const index = "${indexName}";
const query = ${JSON.stringify(queryObject, null, 2)};
const index = '${args.indexName}';
const query = ${JSON.stringify(args.queryObject, null, 2)};

async function run() {
const result = await client.search({
index,
query,
});
const result = await client.search({
index,
query,
});

console.log(result.hits.hits);
}
run().catch(console.log);`;
console.log(result.hits.hits);`;

export const JavascriptSearchExample: SearchCodeDefinition = {
searchCommand,
};

export const JSSemanticIngestDataExample: IngestDataCodeDefinition = {
...JSIngestDataExample,
ingestCommand: (args) => `const { Client } = require('@elastic/elasticsearch');

${createClientSnippet(args)}

// Timeout to allow ML node allocation and semantic ingestion to complete
const timeout = '5m';

const index = '${args.indexName}';
const docs = ${JSON.stringify(args.sampleDocuments, null, 2)};

const bulkIngestResponse = await client.helpers.bulk({
index,
datasource: docs,
timeout,
onDocument() {
return {
index: {},
};
}
});

console.log(bulkIngestResponse);`,
};
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,32 @@ mapping_response = client.indices.put_mapping(index=index_name, body=mappings)
print(mapping_response)
`;

const semanticIngestCommand: IngestCodeSnippetFunction = ({
elasticsearchURL,
apiKey,
indexName,
sampleDocuments,
}) => `from elasticsearch import Elasticsearch, helpers

client = Elasticsearch(
"${elasticsearchURL}",
api_key="${apiKey ?? API_KEY_PLACEHOLDER}",
)

index_name = "${indexName}"

docs = ${JSON.stringify(sampleDocuments, null, 4)}

# Timeout to allow ML node allocation and semantic ingestion to complete
ingestion_timeout=300

bulk_response = helpers.bulk(
client.options(request_timeout=ingestion_timeout),
docs,
index=index_name
)
print(bulk_response)`;

export const PythonIngestDataExample: IngestDataCodeDefinition = {
installCommand: PYTHON_INSTALL_CMD,
ingestCommand: ingestionCommand,
Expand Down Expand Up @@ -165,3 +191,8 @@ print(search_response['hits']['hits'])
export const PythonSearchExample: SearchCodeDefinition = {
searchCommand,
};

export const PythonSemanticIngestDataExample: IngestDataCodeDefinition = {
...PythonIngestDataExample,
ingestCommand: semanticIngestCommand,
};
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const AddDocumentsCodeExample = ({
indexName,
mappingProperties,
}: AddDocumentsCodeExampleProps) => {
const { application, share, console: consolePlugin } = useKibana().services;
const { application, share, console: consolePlugin, cloud } = useKibana().services;
const elasticsearchUrl = useElasticsearchUrl();
const usageTracker = useUsageTracker();
const indexHasMappings = Object.keys(mappingProperties).length > 0;
Expand Down Expand Up @@ -73,8 +73,17 @@ export const AddDocumentsCodeExample = ({
indexHasMappings,
mappingProperties: codeSampleMappings,
apiKey: apiKey || undefined,
isServerless: cloud?.isServerlessEnabled ?? undefined,
};
}, [indexName, elasticsearchUrl, sampleDocuments, codeSampleMappings, indexHasMappings, apiKey]);
}, [
indexName,
elasticsearchUrl,
sampleDocuments,
codeSampleMappings,
indexHasMappings,
apiKey,
cloud,
]);

return (
<EuiPanel
Expand Down
Loading