Skip to content
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

[Search][Onboarding] Start page code view #192642

2 changes: 2 additions & 0 deletions x-pack/plugins/search_indices/public/analytics/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@

export enum AnalyticsEvents {
startPageOpened = 'start_page_opened',
startPageShowCodeClick = 'start_page_show_code',
startPageShowCreateIndexUIClick = 'start_page_show_create_index_ui',
startCreateIndexClick = 'start_create_index',
}
6 changes: 6 additions & 0 deletions x-pack/plugins/search_indices/public/assets/curl.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions x-pack/plugins/search_indices/public/assets/javascript.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions x-pack/plugins/search_indices/public/assets/python.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { CreateIndexCodeExamples } from '../types';

import { CurlExamples } from './curl';
import { JavascriptServerlessExamples } from './javascript';
import { PythonServerlessExamples } from './python';
import { ConsoleExamples } from './sense';

export const DefaultServerlessCodeExamples: CreateIndexCodeExamples = {
sense: ConsoleExamples.default,
curl: CurlExamples.default,
python: PythonServerlessExamples.default,
javascript: JavascriptServerlessExamples.default,
};

export const DenseVectorSeverlessCodeExamples: CreateIndexCodeExamples = {
sense: ConsoleExamples.dense_vector,
curl: CurlExamples.dense_vector,
python: PythonServerlessExamples.dense_vector,
javascript: JavascriptServerlessExamples.dense_vector,
};
47 changes: 47 additions & 0 deletions x-pack/plugins/search_indices/public/code_examples/curl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { i18n } from '@kbn/i18n';
import { API_KEY_PLACEHOLDER, INDEX_PLACEHOLDER } from '../constants';
import { CodeLanguage, CreateIndexLanguageExamples } from '../types';

export const CURL_INFO: CodeLanguage = {
id: 'curl',
title: i18n.translate('xpack.searchIndices.codingLanguages.curl', { defaultMessage: 'cURL' }),
icon: 'curl.svg',
codeBlockLanguage: 'shell',
};

export const CurlExamples: CreateIndexLanguageExamples = {
default: {
createIndex: ({ elasticsearchURL, apiKey, indexName }) => `curl PUT '${elasticsearchURL}/${
indexName ?? INDEX_PLACEHOLDER
}' \
--header 'Authorization: ApiKey ${apiKey ?? API_KEY_PLACEHOLDER}' \
--header 'Content-Type: application/json'`,
},
dense_vector: {
createIndex: ({ elasticsearchURL, apiKey, indexName }) => `curl PUT '${elasticsearchURL}/${
indexName ?? INDEX_PLACEHOLDER
}' \
--header 'Authorization: ApiKey ${apiKey ?? API_KEY_PLACEHOLDER}' \
--header 'Content-Type: application/json' \
--data-raw '{
"mappings": {
"properties":{
"vector":{
"type": "dense_vector",
"dims": 3
},
"text":{
"type":"text"
}
}
}
}'`,
},
};
20 changes: 20 additions & 0 deletions x-pack/plugins/search_indices/public/code_examples/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { CURL_INFO } from './curl';
import { JAVASCRIPT_INFO } from './javascript';
import { PYTHON_INFO } from './python';

export const Languages = {
curl: CURL_INFO,
python: PYTHON_INFO,
javascript: JAVASCRIPT_INFO,
};

export const LanguageOptions = Object.values(Languages);

export type AvailableLanguages = keyof typeof Languages;
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { i18n } from '@kbn/i18n';
import { API_KEY_PLACEHOLDER, INDEX_PLACEHOLDER } from '../constants';
import { CodeLanguage, CreateIndexLanguageExamples } from '../types';

export const JAVASCRIPT_INFO: CodeLanguage = {
id: 'javascript',
title: i18n.translate('xpack.searchIndices.codingLanguages.javascript', {
defaultMessage: 'Javascript',
}),
icon: 'javascript.svg',
codeBlockLanguage: 'javascript',
};

const SERVERLESS_INSTALL_CMD = `npm install @elastic/elasticsearch-serverless`;

export const JavascriptServerlessExamples: CreateIndexLanguageExamples = {
default: {
installCommand: SERVERLESS_INSTALL_CMD,
createIndex: ({
elasticsearchURL,
apiKey,
indexName,
}) => `import { Client } from "@elastic/elasticsearch-serverless"

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

client.indices.create({
index: "${indexName ?? INDEX_PLACEHOLDER}",
});`,
},
dense_vector: {
installCommand: SERVERLESS_INSTALL_CMD,
createIndex: ({
elasticsearchURL,
apiKey,
indexName,
}) => `import { Client } from "@elastic/elasticsearch-serverless"

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

client.indices.create({
index: "${indexName ?? INDEX_PLACEHOLDER}",
mappings: {
properties: {
vector: { type: "dense_vector", dims: 3 },
text: { type: "text"}
},
},
});`,
},
};
62 changes: 62 additions & 0 deletions x-pack/plugins/search_indices/public/code_examples/python.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { i18n } from '@kbn/i18n';
import { API_KEY_PLACEHOLDER, INDEX_PLACEHOLDER } from '../constants';
import { CodeLanguage, CodeSnippetParameters, CreateIndexLanguageExamples } from '../types';

export const PYTHON_INFO: CodeLanguage = {
id: 'python',
title: i18n.translate('xpack.searchIndices.codingLanguages.python', { defaultMessage: 'Python' }),
icon: 'python.svg',
codeBlockLanguage: 'python',
};

const SERVERLESS_PYTHON_INSTALL_CMD = 'pip install elasticsearch-serverless';

export const PythonServerlessExamples: CreateIndexLanguageExamples = {
default: {
installCommand: SERVERLESS_PYTHON_INSTALL_CMD,
createIndex: ({
elasticsearchURL,
apiKey,
indexName,
}: CodeSnippetParameters) => `from elasticsearch-serverless import Elasticsearch

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

client.indices.create(
index="${indexName ?? INDEX_PLACEHOLDER}"
)`,
},
dense_vector: {
installCommand: SERVERLESS_PYTHON_INSTALL_CMD,
createIndex: ({
elasticsearchURL,
apiKey,
indexName,
}: CodeSnippetParameters) => `from elasticsearch-serverless import Elasticsearch

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

client.indices.create(
index="${indexName ?? INDEX_PLACEHOLDER}"
mappings={
"properties": {
"vector": {"type": "dense_vector", "dims": 3 },
"text": {"type": "text"}
}
}
)`,
},
};
31 changes: 31 additions & 0 deletions x-pack/plugins/search_indices/public/code_examples/sense.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { INDEX_PLACEHOLDER } from '../constants';
import { CreateIndexLanguageExamples } from '../types';

export const ConsoleExamples: CreateIndexLanguageExamples = {
default: {
createIndex: ({ indexName }) => `PUT /${indexName ?? INDEX_PLACEHOLDER}`,
},
dense_vector: {
createIndex: ({ indexName }) => `PUT /${indexName ?? INDEX_PLACEHOLDER}
{
"mappings": {
"properties":{
"vector":{
"type": "dense_vector",
"dims": 3
},
"text":{
"type":"text"
}
}
}
}`,
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React, { useMemo } from 'react';
import { EuiIcon, EuiSuperSelect, EuiText, EuiFlexGroup } from '@elastic/eui';
import { i18n } from '@kbn/i18n';

import { useAssetBasePath } from '../../hooks/use_asset_base_path';
import { AvailableLanguages } from '../../code_examples';
import { CodeLanguage } from '../../types';

export interface LanguageSelectorProps {
selectedLanguage: AvailableLanguages;
options: CodeLanguage[];
onSelectLanguage: (value: AvailableLanguages) => void;
}

export const LanguageSelector = ({
selectedLanguage,
options,
onSelectLanguage,
}: LanguageSelectorProps) => {
const assetBasePath = useAssetBasePath();
const languageOptions = useMemo(
() =>
options.map((lang) => ({
value: lang.id as AvailableLanguages,
'aria-label': i18n.translate('xpack.searchIndices.codeLanguage.selectChangeAriaLabel', {
defaultMessage:
'Change language of code examples to {language} for every instance on this page',
values: {
language: lang.title,
},
}),
'data-test-subj': `lang-option-${lang.id}`,
inputDisplay: (
<EuiFlexGroup gutterSize="s" alignItems="center">
<EuiIcon type={`${assetBasePath}/${lang.icon}`} />
<EuiText>{lang.title}</EuiText>
</EuiFlexGroup>
),
})),
[assetBasePath, options]
);
return (
<EuiSuperSelect<AvailableLanguages>
options={languageOptions}
valueOfSelected={selectedLanguage}
onChange={(value) => onSelectLanguage(value)}
/>
);
};
Loading