[Serverless Search] Getting Started - Python PHP & Go code snippets (#162356)

## Summary

Adding code snippets for Python, PHP & Go to align with M0 designs

### Screenshots

![image](172224b8-a4c5-4739-95dc-dce93327a88a)
This commit is contained in:
Rodney Norris 2023-07-27 14:22:09 -05:00 committed by GitHub
parent 1c42ee9720
commit a409f80444
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 343 additions and 2 deletions

View file

@ -741,6 +741,8 @@ export const getDocLinks = ({ kibanaBranch }: GetDocLinkOptions): DocLinks => {
clients: {
/** Changes to these URLs must also be synched in src/plugins/custom_integrations/server/language_clients/index.ts */
guide: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/index.html`,
goConnecting: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/go-api/${DOC_LINK_VERSION}/connecting.html`,
goGettingStarted: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/go-api/${DOC_LINK_VERSION}/getting-started-go.html`,
goIndex: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/go-api/${DOC_LINK_VERSION}/index.html`,
goOverview: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/go-api/${DOC_LINK_VERSION}/overview.html`,
javaBasicAuthentication: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/java-api-client/${DOC_LINK_VERSION}/_basic_authentication.html`,
@ -757,11 +759,14 @@ export const getDocLinks = ({ kibanaBranch }: GetDocLinkOptions): DocLinks => {
netIntroduction: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/net-api/${DOC_LINK_VERSION}/introduction.html`,
netNest: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/net-api/${DOC_LINK_VERSION}/nest.html`,
netSingleNode: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/net-api/${DOC_LINK_VERSION}/connecting.html#single-node`,
phpConfiguration: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/php-api/${DOC_LINK_VERSION}/configuration.html`,
phpConnecting: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/php-api/${DOC_LINK_VERSION}/connecting.html`,
phpInstallation: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/php-api/${DOC_LINK_VERSION}/installation.html`,
phpGuide: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/php-api/${DOC_LINK_VERSION}/index.html`,
phpOverview: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/php-api/${DOC_LINK_VERSION}/overview.html`,
pythonAuthentication: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/python-api/${DOC_LINK_VERSION}/connecting.html#authentication`,
pythonConfig: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/python-api/${DOC_LINK_VERSION}/config.html`,
pythonConnecting: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/python-api/${DOC_LINK_VERSION}/connecting.html`,
pythonGuide: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/python-api/${DOC_LINK_VERSION}/index.html`,
pythonOverview: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/python-api/${DOC_LINK_VERSION}/overview.html`,
rubyAuthentication: `${ELASTIC_WEBSITE_URL}guide/en/elasticsearch/client/ruby-api/${DOC_LINK_VERSION}/connecting.html#client-auth`,

View file

@ -508,6 +508,8 @@ export interface DocLinks {
};
readonly clients: {
readonly guide: string;
readonly goConnecting: string;
readonly goGettingStarted: string;
readonly goIndex: string;
readonly goOverview: string;
readonly javaBasicAuthentication: string;
@ -524,11 +526,14 @@ export interface DocLinks {
readonly netIntroduction: string;
readonly netNest: string;
readonly netSingleNode: string;
readonly phpGuide: string;
readonly phpConfiguration: string;
readonly phpConnecting: string;
readonly phpGuide: string;
readonly phpInstallation: string;
readonly phpOverview: string;
readonly pythonAuthentication: string;
readonly pythonConfig: string;
readonly pythonConnecting: string;
readonly pythonGuide: string;
readonly pythonOverview: string;
readonly rubyAuthentication: string;

View file

@ -13,6 +13,9 @@ class ESDocLinks {
public connectors: string = '';
public elasticsearchClients: string = '';
public integrations: string = '';
public goAdvancedConfig: string = '';
public goBasicConfig: string = '';
public goClient: string = '';
public jsApiReference: string = '';
public jsAdvancedConfig: string = '';
public jsBasicConfig: string = '';
@ -20,6 +23,12 @@ class ESDocLinks {
public kibanaRunApiInConsole: string = '';
public logStash: string = '';
public metadata: string = '';
public phpAdvancedConfig: string = '';
public phpBasicConfig: string = '';
public phpClient: string = '';
public pythonAdvancedConfig: string = '';
public pythonBasicConfig: string = '';
public pythonClient: string = '';
public roleDescriptors: string = '';
public rubyAdvancedConfig: string = '';
public rubyBasicConfig: string = '';
@ -32,12 +41,22 @@ class ESDocLinks {
this.apiIntro = newDocLinks.apis.restApis;
this.elasticsearchClients = newDocLinks.clients.guide;
this.integrations = newDocLinks.serverlessSearch.integrations;
this.goAdvancedConfig = newDocLinks.clients.goConnecting;
this.goBasicConfig = newDocLinks.clients.goGettingStarted;
this.goClient = newDocLinks.clients.goOverview;
this.jsAdvancedConfig = newDocLinks.clients.jsAdvancedConfig;
this.jsApiReference = newDocLinks.clients.jsApiReference;
this.jsBasicConfig = newDocLinks.clients.jsBasicConfig;
this.jsClient = newDocLinks.clients.jsIntro;
this.kibanaRunApiInConsole = newDocLinks.console.guide;
this.metadata = newDocLinks.security.mappingRoles;
this.phpAdvancedConfig = newDocLinks.clients.phpConfiguration;
this.phpBasicConfig = newDocLinks.clients.phpConnecting;
this.phpClient = newDocLinks.clients.phpOverview;
this.phpBasicConfig = newDocLinks.clients.phpConnecting;
this.pythonAdvancedConfig = newDocLinks.clients.pythonConfig;
this.pythonBasicConfig = newDocLinks.clients.pythonConnecting;
this.pythonClient = newDocLinks.clients.pythonOverview;
this.roleDescriptors = newDocLinks.security.mappingRoles;
this.rubyAdvancedConfig = newDocLinks.clients.rubyAdvancedConfig;
this.rubyBasicConfig = newDocLinks.clients.rubyBasicConfig;

View file

@ -0,0 +1,98 @@
/*
* 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 { docLinks } from '../../../../common/doc_links';
import { LanguageDefinition, Languages } from './types';
export const goDefinition: LanguageDefinition = {
advancedConfig: docLinks.goAdvancedConfig,
basicConfig: docLinks.goBasicConfig,
buildSearchQuery: `searchResp, err := es.Search().
Index("books").
Q("snow").
Do(context.Background())
fmt.Println(searchResp, err)`,
configureClient: ({ url, apiKey }) => `import (
"context"
"fmt"
"log"
"strings"
"github.com/elastic/elasticsearch-serverless-go"
)
func main() {
cfg := elasticsearch.Config{
Address: "${url}",
APIKey: "${apiKey}",
}
es, err := elasticsearch.NewClient(cfg)
if err != nil {
log.Fatalf("Error creating the client: %s", err)
}
}`,
docLink: docLinks.goClient,
iconType: 'go.svg',
id: Languages.GO,
ingestData: `ingestResult, err := es.Bulk().
Index("books").
Raw(strings.NewReader(\`
{"index":{"_id":"9780553351927"}}
{"name":"Snow Crash","author":"Neal Stephenson","release_date":"1992-06-01","page_count": 470}
{ "index": { "_id": "9780441017225"}}
{"name": "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585}
{ "index": { "_id": "9780451524935"}}
{"name": "1984", "author": "George Orwell", "release_date": "1985-06-01", "page_count": 328}
{ "index": { "_id": "9781451673319"}}
{"name": "Fahrenheit 451", "author": "Ray Bradbury", "release_date": "1953-10-15", "page_count": 227}
{ "index": { "_id": "9780060850524"}}
{"name": "Brave New World", "author": "Aldous Huxley", "release_date": "1932-06-01", "page_count": 268}
{ "index": { "_id": "9780385490818"}}
{"name": "The Handmaid's Tale", "author": "Margaret Atwood", "release_date": "1985-06-01", "page_count": 311}\n\`)).
Do(context.Background())
fmt.Println(ingestResult, err)`,
ingestDataIndex: ({ apiKey, url, indexName }) => `import (
"context"
"fmt"
"log"
"strings"
"github.com/elastic/elasticsearch-serverless-go"
)
func main() {
cfg := elasticsearch.Config{
Address: "${url}",
APIKey: "${apiKey}",
}
es, err := elasticsearch.NewClient(cfg)
if err != nil {
log.Fatalf("Error creating the client: %s", err)
}
res, err := es.Bulk().
Index("${indexName}").
Raw(strings.NewReader(\`
{ "index": { "_id": "1"}}
{"name": "foo", "title": "bar"}\n\`)).
Do(context.Background())
fmt.Println(res, err)
}`,
installClient: 'go get -u github.com/elastic/elasticsearch-serverless-go@latest',
name: i18n.translate('xpack.serverlessSearch.languages.go', {
defaultMessage: 'Go',
}),
testConnection: `infores, err := es.Info().Do(context.Background())
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
fmt.Println(infores)`,
};

View file

@ -85,7 +85,7 @@ console.log(result);
`,
installClient: 'npm install @elastic/elasticsearch@8',
name: i18n.translate('xpack.serverlessSearch.languages.javascript', {
defaultMessage: 'JavaScript / Node.js',
defaultMessage: 'JavaScript',
}),
testConnection: `const resp = await client.info();

View file

@ -6,13 +6,19 @@
*/
import { curlDefinition } from './curl';
import { goDefinition } from './go';
import { javascriptDefinition } from './javascript';
import { phpDefinition } from './php';
import { pythonDefinition } from './python';
import { rubyDefinition } from './ruby';
import { Languages, LanguageDefinition } from './types';
const languageDefinitionRecords: Partial<Record<Languages, LanguageDefinition>> = {
[Languages.CURL]: curlDefinition,
[Languages.PYTHON]: pythonDefinition,
[Languages.JAVASCRIPT]: javascriptDefinition,
[Languages.PHP]: phpDefinition,
[Languages.GO]: goDefinition,
[Languages.RUBY]: rubyDefinition,
};

View file

@ -0,0 +1,143 @@
/*
* 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 { docLinks } from '../../../../common/doc_links';
import { INDEX_NAME_PLACEHOLDER } from '../../constants';
import { LanguageDefinition, Languages } from './types';
export const phpDefinition: LanguageDefinition = {
advancedConfig: docLinks.phpAdvancedConfig,
basicConfig: docLinks.phpBasicConfig,
buildSearchQuery: `$params = [
'index' => 'books',
'body' => [
'q' => 'snow'
]
];
$response = $client->search($params);
print_r($response->asArray());`,
configureClient: ({ url, apiKey }) => `$client = ClientBuilder::create()
->setHosts(['${url}'])
->setApiKey('${apiKey}')
->build();`,
docLink: docLinks.phpClient,
iconType: 'php.svg',
id: Languages.PHP,
ingestData: `$params = [
'body' => [
[
'index' => [
'_index' => 'books',
'_id' => '9780553351927',
],
],
[
'name' => 'Snow Crash',
'author' => 'Neal Stephenson',
'release_date' => '1992-06-01',
'page_count' => 470,
],
[
'index' => [
'_index' => 'books',
'_id' => '9780441017225',
],
],
[
'name' => 'Revelation Space',
'author' => 'Alastair Reynolds',
'release_date' => '2000-03-15',
'page_count' => 585,
],
[
'index' => [
'_index' => 'books',
'_id' => '9780451524935',
],
],
[
'name' => '1984',
'author' => 'George Orwell',
'release_date' => '1985-06-01',
'page_count' => 328,
],
[
'index' => [
'_index' => 'books',
'_id' => '9781451673319',
],
],
[
'name' => 'Fahrenheit 451',
'author' => 'Ray Bradbury',
'release_date' => '1953-10-15',
'page_count' => 227,
],
[
'index' => [
'_index' => 'books',
'_id' => '9780060850524',
],
],
[
'name' => 'Brave New World',
'author' => 'Aldous Huxley',
'release_date' => '1932-06-01',
'page_count' => 268,
],
[
'index' => [
'_index' => 'books',
'_id' => '9780385490818',
],
],
[
'name' => 'The Handmaid\'s Tale',
'author' => 'Margaret Atwood',
'release_date' => '1985-06-01',
'page_count' => 311,
],
],
];
$response = $client->bulk($params);
echo $response->getStatusCode();
echo (string) $response->getBody();`,
ingestDataIndex: ({ apiKey, url, indexName }) => `$client = ClientBuilder::create()
->setHosts(['${url}'])
->setApiKey('${apiKey}')
->build();
$params = [
'body' => [
[
'index' => [
'_index' => '${indexName ?? INDEX_NAME_PLACEHOLDER}',
'_id' => '1',
],
],
[
'name' => 'foo',
'title' => 'bar',
],
],
];
$response = $client->bulk($params);
echo $response->getStatusCode();
echo (string) $response->getBody();
`,
installClient: 'composer require elasticsearch/elasticsearch',
name: i18n.translate('xpack.serverlessSearch.languages.php', {
defaultMessage: 'PHP',
}),
testConnection: `$response = $client->info();
echo $response->getStatusCode();
echo (string) $response->getBody();`,
};

View file

@ -0,0 +1,65 @@
/*
* 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 { docLinks } from '../../../../common/doc_links';
import { LanguageDefinition, Languages } from './types';
import { INDEX_NAME_PLACEHOLDER } from '../../constants';
export const pythonDefinition: LanguageDefinition = {
advancedConfig: docLinks.pythonAdvancedConfig,
basicConfig: docLinks.pythonBasicConfig,
buildSearchQuery: `client.search(index="books", q="snow")`,
configureClient: ({ url, apiKey }) => `from elasticsearch import Elasticsearch
client = Elasticsearch(
"${url}",
api_key="${apiKey}"
)`,
docLink: docLinks.pythonClient,
iconType: 'python.svg',
id: Languages.PYTHON,
ingestData: `documents = [
{ "index": { "_index": "books", "_id": "9780553351927"}},
{"name": "Snow Crash", "author": "Neal Stephenson", "release_date": "1992-06-01", "page_count": 470},
{ "index": { "_index": "books", "_id": "9780441017225"}},
{"name": "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585},
{ "index": { "_index": "books", "_id": "9780451524935"}},
{"name": "1984", "author": "George Orwell", "release_date": "1985-06-01", "page_count": 328},
{ "index": { "_index": "books", "_id": "9781451673319"}},
{"name": "Fahrenheit 451", "author": "Ray Bradbury", "release_date": "1953-10-15", "page_count": 227},
{ "index": { "_index": "books", "_id": "9780060850524"}},
{"name": "Brave New World", "author": "Aldous Huxley", "release_date": "1932-06-01", "page_count": 268},
{ "index": { "_index": "books", "_id": "9780385490818"}},
{"name": "The Handmaid's Tale", "author": "Margaret Atwood", "release_date": "1985-06-01", "page_count": 311},
]
client.bulk(operations=documents)`,
ingestDataIndex: ({ apiKey, url, indexName }) => `from elasticsearch import Elasticsearch
client = Elasticsearch(
"${url}",
api_key="${apiKey}"
)
documents = [
{"index": {"_index": "${indexName ?? INDEX_NAME_PLACEHOLDER}"}},
{"name": "foo", "title": "bar"},
]
client.bulk(operations=documents)
`,
installClient: `python -m pip install elasticsearch
# If your application uses async/await in Python you can install with the async extra
# python -m pip install elasticsearch[async]
`,
name: i18n.translate('xpack.serverlessSearch.languages.python', {
defaultMessage: 'Python',
}),
testConnection: `client.info()`,
};