diff --git a/solutions/search/get-started/_snippets/index-basics/example1-console.md b/solutions/search/get-started/_snippets/index-basics/example1-console.md new file mode 100644 index 0000000000..2d87bd71bd --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example1-console.md @@ -0,0 +1,3 @@ +```console +PUT /books +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example1-curl.md b/solutions/search/get-started/_snippets/index-basics/example1-curl.md new file mode 100644 index 0000000000..374a189c3d --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example1-curl.md @@ -0,0 +1,4 @@ +```bash +curl -X PUT "$ELASTICSEARCH_URL/books" \ + -H "Authorization: ApiKey $ELASTIC_API_KEY" +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example1-js.md b/solutions/search/get-started/_snippets/index-basics/example1-js.md new file mode 100644 index 0000000000..70b93e3f8c --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example1-js.md @@ -0,0 +1,19 @@ +```js +const { Client } = require("@elastic/elasticsearch"); + +const client = new Client({ + nodes: [process.env["ELASTICSEARCH_URL"]], + auth: { + apiKey: process.env["ELASTIC_API_KEY"], + }, +}); + +async function run() { + const response = await client.indices.create({ + index: "books", + }); + console.log(response); +} + +run(); +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example1-php.md b/solutions/search/get-started/_snippets/index-basics/example1-php.md new file mode 100644 index 0000000000..0ed4e5c862 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example1-php.md @@ -0,0 +1,18 @@ +```php +setHosts([getenv("ELASTICSEARCH_URL")]) + ->setApiKey(getenv("ELASTIC_API_KEY")) + ->build(); + +$resp = $client->indices()->create([ + "index" => "books", +]); +echo $resp->asString(); + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example1-python.md b/solutions/search/get-started/_snippets/index-basics/example1-python.md new file mode 100644 index 0000000000..b200b3a942 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example1-python.md @@ -0,0 +1,15 @@ +```python +import os +from elasticsearch import Elasticsearch + +client = Elasticsearch( + hosts=[os.getenv("ELASTICSEARCH_URL")], + api_key=os.getenv("ELASTIC_API_KEY"), +) + +resp = client.indices.create( + index="books", +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example1-ruby.md b/solutions/search/get-started/_snippets/index-basics/example1-ruby.md new file mode 100644 index 0000000000..3238c841a3 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example1-ruby.md @@ -0,0 +1,14 @@ +```ruby +require "elasticsearch" + +client = Elasticsearch::Client.new( + host: ENV["ELASTICSEARCH_URL"], + api_key: ENV["ELASTIC_API_KEY"] +) + +response = client.indices.create( + index: "books" +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example2-console.md b/solutions/search/get-started/_snippets/index-basics/example2-console.md new file mode 100644 index 0000000000..6cedc23194 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example2-console.md @@ -0,0 +1,9 @@ +```console +POST books/_doc +{ + "name": "Snow Crash", + "author": "Neal Stephenson", + "release_date": "1992-06-01", + "page_count": 470 +} +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example2-curl.md b/solutions/search/get-started/_snippets/index-basics/example2-curl.md new file mode 100644 index 0000000000..0bf3dcc8d8 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example2-curl.md @@ -0,0 +1,6 @@ +```bash +curl -X POST "$ELASTICSEARCH_URL/books/_doc" \ + -H "Authorization: ApiKey $ELASTIC_API_KEY" \ + -H "Content-Type: application/json" \ + -d '{"name":"Snow Crash","author":"Neal Stephenson","release_date":"1992-06-01","page_count":470}' +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example2-js.md b/solutions/search/get-started/_snippets/index-basics/example2-js.md new file mode 100644 index 0000000000..5fb6dc8bca --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example2-js.md @@ -0,0 +1,12 @@ +```js +const response = await client.index({ + index: "books", + document: { + name: "Snow Crash", + author: "Neal Stephenson", + release_date: "1992-06-01", + page_count: 470, + }, +}); +console.log(response); +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example2-php.md b/solutions/search/get-started/_snippets/index-basics/example2-php.md new file mode 100644 index 0000000000..2ee1b6f42f --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example2-php.md @@ -0,0 +1,13 @@ +```php +$resp = $client->index([ + "index" => "books", + "body" => [ + "name" => "Snow Crash", + "author" => "Neal Stephenson", + "release_date" => "1992-06-01", + "page_count" => 470, + ], +]); +echo $resp->asString(); + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example2-python.md b/solutions/search/get-started/_snippets/index-basics/example2-python.md new file mode 100644 index 0000000000..a2c579f9c8 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example2-python.md @@ -0,0 +1,13 @@ +```python +resp = client.index( + index="books", + document={ + "name": "Snow Crash", + "author": "Neal Stephenson", + "release_date": "1992-06-01", + "page_count": 470 + }, +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example2-ruby.md b/solutions/search/get-started/_snippets/index-basics/example2-ruby.md new file mode 100644 index 0000000000..f44dd39057 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example2-ruby.md @@ -0,0 +1,13 @@ +```ruby +response = client.index( + index: "books", + body: { + "name": "Snow Crash", + "author": "Neal Stephenson", + "release_date": "1992-06-01", + "page_count": 470 + } +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example3-console.md b/solutions/search/get-started/_snippets/index-basics/example3-console.md new file mode 100644 index 0000000000..8b498e0e63 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example3-console.md @@ -0,0 +1,13 @@ +```console +POST /_bulk +{ "index" : { "_index" : "books" } } +{"name": "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585} +{ "index" : { "_index" : "books" } } +{"name": "1984", "author": "George Orwell", "release_date": "1985-06-01", "page_count": 328} +{ "index" : { "_index" : "books" } } +{"name": "Fahrenheit 451", "author": "Ray Bradbury", "release_date": "1953-10-15", "page_count": 227} +{ "index" : { "_index" : "books" } } +{"name": "Brave New World", "author": "Aldous Huxley", "release_date": "1932-06-01", "page_count": 268} +{ "index" : { "_index" : "books" } } +{"name": "The Handmaids Tale", "author": "Margaret Atwood", "release_date": "1985-06-01", "page_count": 311} +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example3-curl.md b/solutions/search/get-started/_snippets/index-basics/example3-curl.md new file mode 100644 index 0000000000..3e1681f18f --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example3-curl.md @@ -0,0 +1,6 @@ +```bash +curl -X POST "$ELASTICSEARCH_URL/_bulk" \ + -H "Authorization: ApiKey $ELASTIC_API_KEY" \ + -H "Content-Type: application/x-ndjson" \ + -d $'{"index":{"_index":"books"}}\n{"name":"Revelation Space","author":"Alastair Reynolds","release_date":"2000-03-15","page_count":585}\n{"index":{"_index":"books"}}\n{"name":"1984","author":"George Orwell","release_date":"1985-06-01","page_count":328}\n{"index":{"_index":"books"}}\n{"name":"Fahrenheit 451","author":"Ray Bradbury","release_date":"1953-10-15","page_count":227}\n{"index":{"_index":"books"}}\n{"name":"Brave New World","author":"Aldous Huxley","release_date":"1932-06-01","page_count":268}\n{"index":{"_index":"books"}}\n{"name":"The Handmaids Tale","author":"Margaret Atwood","release_date":"1985-06-01","page_count":311}\n' +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example3-js.md b/solutions/search/get-started/_snippets/index-basics/example3-js.md new file mode 100644 index 0000000000..9f2193d620 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example3-js.md @@ -0,0 +1,62 @@ +```js +const response = await client.bulk({ + operations: [ + { + index: { + _index: "books", + }, + }, + { + name: "Revelation Space", + author: "Alastair Reynolds", + release_date: "2000-03-15", + page_count: 585, + }, + { + index: { + _index: "books", + }, + }, + { + name: "1984", + author: "George Orwell", + release_date: "1985-06-01", + page_count: 328, + }, + { + index: { + _index: "books", + }, + }, + { + name: "Fahrenheit 451", + author: "Ray Bradbury", + release_date: "1953-10-15", + page_count: 227, + }, + { + index: { + _index: "books", + }, + }, + { + name: "Brave New World", + author: "Aldous Huxley", + release_date: "1932-06-01", + page_count: 268, + }, + { + index: { + _index: "books", + }, + }, + { + name: "The Handmaids Tale", + author: "Margaret Atwood", + release_date: "1985-06-01", + page_count: 311, + }, + ], +}); +console.log(response); +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example3-php.md b/solutions/search/get-started/_snippets/index-basics/example3-php.md new file mode 100644 index 0000000000..31c61af636 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example3-php.md @@ -0,0 +1,63 @@ +```php +$resp = $client->bulk([ + "body" => array( + [ + "index" => [ + "_index" => "books", + ], + ], + [ + "name" => "Revelation Space", + "author" => "Alastair Reynolds", + "release_date" => "2000-03-15", + "page_count" => 585, + ], + [ + "index" => [ + "_index" => "books", + ], + ], + [ + "name" => "1984", + "author" => "George Orwell", + "release_date" => "1985-06-01", + "page_count" => 328, + ], + [ + "index" => [ + "_index" => "books", + ], + ], + [ + "name" => "Fahrenheit 451", + "author" => "Ray Bradbury", + "release_date" => "1953-10-15", + "page_count" => 227, + ], + [ + "index" => [ + "_index" => "books", + ], + ], + [ + "name" => "Brave New World", + "author" => "Aldous Huxley", + "release_date" => "1932-06-01", + "page_count" => 268, + ], + [ + "index" => [ + "_index" => "books", + ], + ], + [ + "name" => "The Handmaids Tale", + "author" => "Margaret Atwood", + "release_date" => "1985-06-01", + "page_count" => 311, + ], + ), +]); +echo $resp->asString(); + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example3-python.md b/solutions/search/get-started/_snippets/index-basics/example3-python.md new file mode 100644 index 0000000000..985f5f349a --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example3-python.md @@ -0,0 +1,63 @@ +```python +resp = client.bulk( + operations=[ + { + "index": { + "_index": "books" + } + }, + { + "name": "Revelation Space", + "author": "Alastair Reynolds", + "release_date": "2000-03-15", + "page_count": 585 + }, + { + "index": { + "_index": "books" + } + }, + { + "name": "1984", + "author": "George Orwell", + "release_date": "1985-06-01", + "page_count": 328 + }, + { + "index": { + "_index": "books" + } + }, + { + "name": "Fahrenheit 451", + "author": "Ray Bradbury", + "release_date": "1953-10-15", + "page_count": 227 + }, + { + "index": { + "_index": "books" + } + }, + { + "name": "Brave New World", + "author": "Aldous Huxley", + "release_date": "1932-06-01", + "page_count": 268 + }, + { + "index": { + "_index": "books" + } + }, + { + "name": "The Handmaids Tale", + "author": "Margaret Atwood", + "release_date": "1985-06-01", + "page_count": 311 + } + ], +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example3-ruby.md b/solutions/search/get-started/_snippets/index-basics/example3-ruby.md new file mode 100644 index 0000000000..559bce5355 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example3-ruby.md @@ -0,0 +1,63 @@ +```ruby +response = client.bulk( + body: [ + { + "index": { + "_index": "books" + } + }, + { + "name": "Revelation Space", + "author": "Alastair Reynolds", + "release_date": "2000-03-15", + "page_count": 585 + }, + { + "index": { + "_index": "books" + } + }, + { + "name": "1984", + "author": "George Orwell", + "release_date": "1985-06-01", + "page_count": 328 + }, + { + "index": { + "_index": "books" + } + }, + { + "name": "Fahrenheit 451", + "author": "Ray Bradbury", + "release_date": "1953-10-15", + "page_count": 227 + }, + { + "index": { + "_index": "books" + } + }, + { + "name": "Brave New World", + "author": "Aldous Huxley", + "release_date": "1932-06-01", + "page_count": 268 + }, + { + "index": { + "_index": "books" + } + }, + { + "name": "The Handmaids Tale", + "author": "Margaret Atwood", + "release_date": "1985-06-01", + "page_count": 311 + } + ] +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example4-console.md b/solutions/search/get-started/_snippets/index-basics/example4-console.md new file mode 100644 index 0000000000..1d3684a64c --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example4-console.md @@ -0,0 +1,12 @@ +```console +POST /books/_doc +{ + "name": "The Great Gatsby", + "author": "F. Scott Fitzgerald", + "release_date": "1925-04-10", + "page_count": 180, + "language": "EN" <1> +} +``` + +1. The new field. diff --git a/solutions/search/get-started/_snippets/index-basics/example4-curl.md b/solutions/search/get-started/_snippets/index-basics/example4-curl.md new file mode 100644 index 0000000000..e6d8aef882 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example4-curl.md @@ -0,0 +1,6 @@ +```bash +curl -X POST "$ELASTICSEARCH_URL/books/_doc" \ + -H "Authorization: ApiKey $ELASTIC_API_KEY" \ + -H "Content-Type: application/json" \ + -d '{"name":"The Great Gatsby","author":"F. Scott Fitzgerald","release_date":"1925-04-10","page_count":180,"language":"EN"}' +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example4-js.md b/solutions/search/get-started/_snippets/index-basics/example4-js.md new file mode 100644 index 0000000000..726fcbfed0 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example4-js.md @@ -0,0 +1,13 @@ +```js +const response = await client.index({ + index: "books", + document: { + name: "The Great Gatsby", + author: "F. Scott Fitzgerald", + release_date: "1925-04-10", + page_count: 180, + language: "EN", + }, +}); +console.log(response); +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example4-php.md b/solutions/search/get-started/_snippets/index-basics/example4-php.md new file mode 100644 index 0000000000..585eabcfbe --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example4-php.md @@ -0,0 +1,14 @@ +```php +$resp = $client->index([ + "index" => "books", + "body" => [ + "name" => "The Great Gatsby", + "author" => "F. Scott Fitzgerald", + "release_date" => "1925-04-10", + "page_count" => 180, + "language" => "EN", + ], +]); +echo $resp->asString(); + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example4-python.md b/solutions/search/get-started/_snippets/index-basics/example4-python.md new file mode 100644 index 0000000000..efda319d4b --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example4-python.md @@ -0,0 +1,14 @@ +```python +resp = client.index( + index="books", + document={ + "name": "The Great Gatsby", + "author": "F. Scott Fitzgerald", + "release_date": "1925-04-10", + "page_count": 180, + "language": "EN" + }, +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example4-ruby.md b/solutions/search/get-started/_snippets/index-basics/example4-ruby.md new file mode 100644 index 0000000000..92f4db46cd --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example4-ruby.md @@ -0,0 +1,14 @@ +```ruby +response = client.index( + index: "books", + body: { + "name": "The Great Gatsby", + "author": "F. Scott Fitzgerald", + "release_date": "1925-04-10", + "page_count": 180, + "language": "EN" + } +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example5-console.md b/solutions/search/get-started/_snippets/index-basics/example5-console.md new file mode 100644 index 0000000000..5b277de59d --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example5-console.md @@ -0,0 +1,3 @@ +```console +GET /books/_mapping +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example5-curl.md b/solutions/search/get-started/_snippets/index-basics/example5-curl.md new file mode 100644 index 0000000000..3cf1906155 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example5-curl.md @@ -0,0 +1,4 @@ +```bash +curl -X GET "$ELASTICSEARCH_URL/books/_mapping" \ + -H "Authorization: ApiKey $ELASTIC_API_KEY" +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example5-js.md b/solutions/search/get-started/_snippets/index-basics/example5-js.md new file mode 100644 index 0000000000..17241ee766 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example5-js.md @@ -0,0 +1,6 @@ +```js +const response = await client.indices.getMapping({ + index: "books", +}); +console.log(response); +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example5-php.md b/solutions/search/get-started/_snippets/index-basics/example5-php.md new file mode 100644 index 0000000000..b90dc7349d --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example5-php.md @@ -0,0 +1,7 @@ +```php +$resp = $client->indices()->getMapping([ + "index" => "books", +]); +echo $resp->asString(); + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example5-python.md b/solutions/search/get-started/_snippets/index-basics/example5-python.md new file mode 100644 index 0000000000..ce23c1383a --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example5-python.md @@ -0,0 +1,7 @@ +```python +resp = client.indices.get_mapping( + index="books", +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example5-ruby.md b/solutions/search/get-started/_snippets/index-basics/example5-ruby.md new file mode 100644 index 0000000000..7caba1f334 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example5-ruby.md @@ -0,0 +1,7 @@ +```ruby +response = client.indices.get_mapping( + index: "books" +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example6-console.md b/solutions/search/get-started/_snippets/index-basics/example6-console.md new file mode 100644 index 0000000000..ec26471362 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example6-console.md @@ -0,0 +1,17 @@ +```console +PUT /my-explicit-mappings-books +{ + "mappings": { + "dynamic": false, <1> + "properties": { <2> + "name": { "type": "text" }, + "author": { "type": "text" }, + "release_date": { "type": "date", "format": "yyyy-MM-dd" }, + "page_count": { "type": "integer" } + } + } +} +``` + +1. `dynamic`: Turns off dynamic mapping for the index. If you don't define fields in the mapping, they'll still be stored in the document's `_source` field, but you can't index or search them. +2. `properties`: Defines the fields and their corresponding data types. diff --git a/solutions/search/get-started/_snippets/index-basics/example6-curl.md b/solutions/search/get-started/_snippets/index-basics/example6-curl.md new file mode 100644 index 0000000000..78dfc71151 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example6-curl.md @@ -0,0 +1,6 @@ +```bash +curl -X PUT "$ELASTICSEARCH_URL/my-explicit-mappings-books" \ + -H "Authorization: ApiKey $ELASTIC_API_KEY" \ + -H "Content-Type: application/json" \ + -d '{"mappings":{"dynamic":false,"properties":{"name":{"type":"text"},"author":{"type":"text"},"release_date":{"type":"date","format":"yyyy-MM-dd"},"page_count":{"type":"integer"}}}}' +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example6-js.md b/solutions/search/get-started/_snippets/index-basics/example6-js.md new file mode 100644 index 0000000000..26195f6168 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example6-js.md @@ -0,0 +1,24 @@ +```js +const response = await client.indices.create({ + index: "my-explicit-mappings-books", + mappings: { + dynamic: false, + properties: { + name: { + type: "text", + }, + author: { + type: "text", + }, + release_date: { + type: "date", + format: "yyyy-MM-dd", + }, + page_count: { + type: "integer", + }, + }, + }, +}); +console.log(response); +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example6-php.md b/solutions/search/get-started/_snippets/index-basics/example6-php.md new file mode 100644 index 0000000000..3fcc181056 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example6-php.md @@ -0,0 +1,27 @@ +```php +$resp = $client->indices()->create([ + "index" => "my-explicit-mappings-books", + "body" => [ + "mappings" => [ + "dynamic" => false, + "properties" => [ + "name" => [ + "type" => "text", + ], + "author" => [ + "type" => "text", + ], + "release_date" => [ + "type" => "date", + "format" => "yyyy-MM-dd", + ], + "page_count" => [ + "type" => "integer", + ], + ], + ], + ], +]); +echo $resp->asString(); + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example6-python.md b/solutions/search/get-started/_snippets/index-basics/example6-python.md new file mode 100644 index 0000000000..077ec46228 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example6-python.md @@ -0,0 +1,25 @@ +```python +resp = client.indices.create( + index="my-explicit-mappings-books", + mappings={ + "dynamic": False, + "properties": { + "name": { + "type": "text" + }, + "author": { + "type": "text" + }, + "release_date": { + "type": "date", + "format": "yyyy-MM-dd" + }, + "page_count": { + "type": "integer" + } + } + }, +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example6-ruby.md b/solutions/search/get-started/_snippets/index-basics/example6-ruby.md new file mode 100644 index 0000000000..8793f0ab6b --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example6-ruby.md @@ -0,0 +1,27 @@ +```ruby +response = client.indices.create( + index: "my-explicit-mappings-books", + body: { + "mappings": { + "dynamic": false, + "properties": { + "name": { + "type": "text" + }, + "author": { + "type": "text" + }, + "release_date": { + "type": "date", + "format": "yyyy-MM-dd" + }, + "page_count": { + "type": "integer" + } + } + } + } +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example7-console.md b/solutions/search/get-started/_snippets/index-basics/example7-console.md new file mode 100644 index 0000000000..3f3337a619 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example7-console.md @@ -0,0 +1,3 @@ +```console +GET books/_search +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example7-curl.md b/solutions/search/get-started/_snippets/index-basics/example7-curl.md new file mode 100644 index 0000000000..c5f98747dd --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example7-curl.md @@ -0,0 +1,4 @@ +```bash +curl -X GET "$ELASTICSEARCH_URL/books/_search" \ + -H "Authorization: ApiKey $ELASTIC_API_KEY" +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example7-js.md b/solutions/search/get-started/_snippets/index-basics/example7-js.md new file mode 100644 index 0000000000..aabfb858d4 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example7-js.md @@ -0,0 +1,6 @@ +```js +const response = await client.search({ + index: "books", +}); +console.log(response); +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example7-php.md b/solutions/search/get-started/_snippets/index-basics/example7-php.md new file mode 100644 index 0000000000..b54427705a --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example7-php.md @@ -0,0 +1,7 @@ +```php +$resp = $client->search([ + "index" => "books", +]); +echo $resp->asString(); + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example7-python.md b/solutions/search/get-started/_snippets/index-basics/example7-python.md new file mode 100644 index 0000000000..0518f95aa1 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example7-python.md @@ -0,0 +1,7 @@ +```python +resp = client.search( + index="books", +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example7-ruby.md b/solutions/search/get-started/_snippets/index-basics/example7-ruby.md new file mode 100644 index 0000000000..176fe908c5 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example7-ruby.md @@ -0,0 +1,7 @@ +```ruby +response = client.search( + index: "books" +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example8-console.md b/solutions/search/get-started/_snippets/index-basics/example8-console.md new file mode 100644 index 0000000000..57cf0c4f71 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example8-console.md @@ -0,0 +1,10 @@ +```console +GET books/_search +{ + "query": { + "match": { + "name": "brave" + } + } +} +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example8-curl.md b/solutions/search/get-started/_snippets/index-basics/example8-curl.md new file mode 100644 index 0000000000..baebefe060 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example8-curl.md @@ -0,0 +1,6 @@ +```bash +curl -X GET "$ELASTICSEARCH_URL/books/_search" \ + -H "Authorization: ApiKey $ELASTIC_API_KEY" \ + -H "Content-Type: application/json" \ + -d '{"query":{"match":{"name":"brave"}}}' +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example8-js.md b/solutions/search/get-started/_snippets/index-basics/example8-js.md new file mode 100644 index 0000000000..a1cf90f147 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example8-js.md @@ -0,0 +1,11 @@ +```js +const response = await client.search({ + index: "books", + query: { + match: { + name: "brave", + }, + }, +}); +console.log(response); +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example8-php.md b/solutions/search/get-started/_snippets/index-basics/example8-php.md new file mode 100644 index 0000000000..64764ce976 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example8-php.md @@ -0,0 +1,14 @@ +```php +$resp = $client->search([ + "index" => "books", + "body" => [ + "query" => [ + "match" => [ + "name" => "brave", + ], + ], + ], +]); +echo $resp->asString(); + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example8-python.md b/solutions/search/get-started/_snippets/index-basics/example8-python.md new file mode 100644 index 0000000000..89719370a6 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example8-python.md @@ -0,0 +1,12 @@ +```python +resp = client.search( + index="books", + query={ + "match": { + "name": "brave" + } + }, +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example8-ruby.md b/solutions/search/get-started/_snippets/index-basics/example8-ruby.md new file mode 100644 index 0000000000..084d4032b4 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example8-ruby.md @@ -0,0 +1,14 @@ +```ruby +response = client.search( + index: "books", + body: { + "query": { + "match": { + "name": "brave" + } + } + } +) +print(resp) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example9-console.md b/solutions/search/get-started/_snippets/index-basics/example9-console.md new file mode 100644 index 0000000000..43bca616f6 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example9-console.md @@ -0,0 +1,4 @@ +```console +DELETE /books +DELETE /my-explicit-mappings-books +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example9-curl.md b/solutions/search/get-started/_snippets/index-basics/example9-curl.md new file mode 100644 index 0000000000..586b630bbb --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example9-curl.md @@ -0,0 +1,6 @@ +```bash +curl -X DELETE "$ELASTICSEARCH_URL/books" \ + -H "Authorization: ApiKey $ELASTIC_API_KEY" +curl -X DELETE "$ELASTICSEARCH_URL/my-explicit-mappings-books" \ + -H "Authorization: ApiKey $ELASTIC_API_KEY" +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example9-js.md b/solutions/search/get-started/_snippets/index-basics/example9-js.md new file mode 100644 index 0000000000..f7a6e5e2e1 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example9-js.md @@ -0,0 +1,11 @@ +```js +const response = await client.indices.delete({ + index: "books", +}); +console.log(response); + +const response1 = await client.indices.delete({ + index: "my-explicit-mappings-books", +}); +console.log(response1); +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example9-php.md b/solutions/search/get-started/_snippets/index-basics/example9-php.md new file mode 100644 index 0000000000..4c7ef097bb --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example9-php.md @@ -0,0 +1,12 @@ +```php +$resp = $client->indices()->delete([ + "index" => "books", +]); +echo $resp->asString(); + +$resp1 = $client->indices()->delete([ + "index" => "my-explicit-mappings-books", +]); +echo $resp1->asString(); + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example9-python.md b/solutions/search/get-started/_snippets/index-basics/example9-python.md new file mode 100644 index 0000000000..d1558b7698 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example9-python.md @@ -0,0 +1,12 @@ +```python +resp = client.indices.delete( + index="books", +) +print(resp) + +resp1 = client.indices.delete( + index="my-explicit-mappings-books", +) +print(resp1) + +``` diff --git a/solutions/search/get-started/_snippets/index-basics/example9-ruby.md b/solutions/search/get-started/_snippets/index-basics/example9-ruby.md new file mode 100644 index 0000000000..6a99573e41 --- /dev/null +++ b/solutions/search/get-started/_snippets/index-basics/example9-ruby.md @@ -0,0 +1,12 @@ +```ruby +response = client.indices.delete( + index: "books" +) +print(resp) + +response1 = client.indices.delete( + index: "my-explicit-mappings-books" +) +print(resp1) + +``` diff --git a/solutions/search/get-started/index-basics.md b/solutions/search/get-started/index-basics.md index 1d271869d4..0c05902b0c 100644 --- a/solutions/search/get-started/index-basics.md +++ b/solutions/search/get-started/index-basics.md @@ -7,11 +7,6 @@ applies_to: This quickstart provides a hands-on introduction to the fundamental concepts of {{es}}: [indices, documents, and field type mappings](../../../manage-data/data-store/index-basics.md). You'll learn how to create an index, add documents, work with dynamic and explicit mappings, and perform your first basic searches. -::::{tip} -The code examples are in [Console](/explore-analyze/query-filter/tools/console.md) syntax by default. -You can [convert into other programming languages](/explore-analyze/query-filter/tools/console.md#import-export-console-requests) in the Console UI. -:::: - ## Requirements [getting-started-requirements] You can follow this guide using any {{es}} deployment. @@ -20,25 +15,62 @@ To get started quickly, spin up a cluster [locally in Docker](/deploy-manage/dep ## Add data to {{es}} [getting-started-index-creation] -::::{tip} +:::::{tip} This quickstart uses {{es}} APIs, but there are many other ways to [add data to {{es}}](/solutions/search/ingest-for-search.md). -:::: +::::: You add data to {{es}} as JSON objects called documents. {{es}} stores these documents in searchable indices. -:::::{stepper} -::::{step} Create an index +::::::{stepper} +:::::{step} Create an index Create a new index named `books`: -```console -PUT /books -``` +::::{tab-set} +:group: api-examples + +:::{tab-item} Console +:sync: console + +:::{include} _snippets/index-basics/example1-console.md +::: + +:::{tab-item} curl +:sync: curl + +:::{include} _snippets/index-basics/example1-curl.md +::: + +:::{tab-item} Python +:sync: python + +:::{include} _snippets/index-basics/example1-python.md +::: + +:::{tab-item} JavaScript +:sync: js + +:::{include} _snippets/index-basics/example1-js.md +::: + +:::{tab-item} PHP +:sync: php + +:::{include} _snippets/index-basics/example1-php.md +::: + +:::{tab-item} Ruby +:sync: ruby + +:::{include} _snippets/index-basics/example1-ruby.md +::: + +:::: The following response indicates the index was created successfully. -:::{dropdown} Example response +::::{dropdown} Example response ```console-result { @@ -48,26 +80,57 @@ The following response indicates the index was created successfully. } ``` -::: :::: -::::{step} Add a single document +::::: +:::::{step} Add a single document Use the following request to add a single document to the `books` index. If the index doesn't already exist, this request will automatically create it. -```console -POST books/_doc -{ - "name": "Snow Crash", - "author": "Neal Stephenson", - "release_date": "1992-06-01", - "page_count": 470 -} -``` +::::{tab-set} +:group: api-examples + +:::{tab-item} Console +:sync: console + +:::{include} _snippets/index-basics/example2-console.md +::: + +:::{tab-item} curl +:sync: curl + +:::{include} _snippets/index-basics/example2-curl.md +::: + +:::{tab-item} Python +:sync: python + +:::{include} _snippets/index-basics/example2-python.md +::: + +:::{tab-item} JavaScript +:sync: js + +:::{include} _snippets/index-basics/example2-js.md +::: + +:::{tab-item} PHP +:sync: php + +:::{include} _snippets/index-basics/example2-php.md +::: + +:::{tab-item} Ruby +:sync: ruby + +:::{include} _snippets/index-basics/example2-ruby.md +::: + +:::: The response includes metadata that {{es}} generates for the document, including a unique `_id` for the document within the index. -:::{dropdown} Example response +::::{dropdown} Example response ```console-result { @@ -95,30 +158,57 @@ The response includes metadata that {{es}} generates for the document, including 8. `failed`: The number of shards that failed during the indexing operation. *0* indicates no failures. 9. `_seq_no`: A monotonically increasing number incremented for each indexing operation on a shard. 10. `_primary_term`: A monotonically increasing number incremented each time a primary shard is assigned to a different node. -::: :::: -::::{step} Add multiple documents +::::: +:::::{step} Add multiple documents Use the [`_bulk` endpoint]({{es-apis}}operation/operation-bulk) to add multiple documents in a single request. Bulk data must be formatted as newline-delimited JSON (NDJSON). -```console -POST /_bulk -{ "index" : { "_index" : "books" } } -{"name": "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585} -{ "index" : { "_index" : "books" } } -{"name": "1984", "author": "George Orwell", "release_date": "1985-06-01", "page_count": 328} -{ "index" : { "_index" : "books" } } -{"name": "Fahrenheit 451", "author": "Ray Bradbury", "release_date": "1953-10-15", "page_count": 227} -{ "index" : { "_index" : "books" } } -{"name": "Brave New World", "author": "Aldous Huxley", "release_date": "1932-06-01", "page_count": 268} -{ "index" : { "_index" : "books" } } -{"name": "The Handmaids Tale", "author": "Margaret Atwood", "release_date": "1985-06-01", "page_count": 311} -``` +::::{tab-set} +:group: api-examples + +:::{tab-item} Console +:sync: console + +:::{include} _snippets/index-basics/example3-console.md +::: + +:::{tab-item} curl +:sync: curl + +:::{include} _snippets/index-basics/example3-curl.md +::: + +:::{tab-item} Python +:sync: python + +:::{include} _snippets/index-basics/example3-python.md +::: + +:::{tab-item} JavaScript +:sync: js + +:::{include} _snippets/index-basics/example3-js.md +::: + +:::{tab-item} PHP +:sync: php + +:::{include} _snippets/index-basics/example3-php.md +::: + +:::{tab-item} Ruby +:sync: ruby + +:::{include} _snippets/index-basics/example3-ruby.md +::: + +:::: You should receive a response indicating there were no errors. -:::{dropdown} Example response +::::{dropdown} Example response ```console-result { @@ -209,9 +299,9 @@ You should receive a response indicating there were no errors. } ``` -::: :::: -::::{step} Use dynamic mapping +::::: +:::::{step} Use dynamic mapping [Mappings](/manage-data/data-store/index-basics.md#elasticsearch-intro-documents-fields-mappings) define how data is stored and indexed in {{es}}, like a schema in a relational database. @@ -220,28 +310,93 @@ The documents you've added so far have used dynamic mapping, because you didn't To see how dynamic mapping works, add a new document to the `books` index with a field that isn't available in the existing documents. -```console -POST /books/_doc -{ - "name": "The Great Gatsby", - "author": "F. Scott Fitzgerald", - "release_date": "1925-04-10", - "page_count": 180, - "language": "EN" <1> -} -``` +::::{tab-set} +:group: api-examples + +:::{tab-item} Console +:sync: console + +:::{include} _snippets/index-basics/example4-console.md +::: + +:::{tab-item} curl +:sync: curl -1. The new field. +:::{include} _snippets/index-basics/example4-curl.md +::: + +:::{tab-item} Python +:sync: python + +:::{include} _snippets/index-basics/example4-python.md +::: + +:::{tab-item} JavaScript +:sync: js + +:::{include} _snippets/index-basics/example4-js.md +::: + +:::{tab-item} PHP +:sync: php + +:::{include} _snippets/index-basics/example4-php.md +::: + +:::{tab-item} Ruby +:sync: ruby + +:::{include} _snippets/index-basics/example4-ruby.md +::: + +:::: View the mapping for the `books` index with the [get mapping API]({{es-apis}}operation/operation-indices-get-mapping). The new field `language` has been added to the mapping with a `text` data type. -```console -GET /books/_mapping -``` +::::{tab-set} +:group: api-examples + +:::{tab-item} Console +:sync: console + +:::{include} _snippets/index-basics/example5-console.md +::: + +:::{tab-item} curl +:sync: curl + +:::{include} _snippets/index-basics/example5-curl.md +::: + +:::{tab-item} Python +:sync: python + +:::{include} _snippets/index-basics/example5-python.md +::: + +:::{tab-item} JavaScript +:sync: js + +:::{include} _snippets/index-basics/example5-js.md +::: + +:::{tab-item} PHP +:sync: php + +:::{include} _snippets/index-basics/example5-php.md +::: + +:::{tab-item} Ruby +:sync: ruby + +:::{include} _snippets/index-basics/example5-ruby.md +::: + +:::: The following response displays the mappings that were created by {{es}}. -:::{dropdown} Example response +::::{dropdown} Example response ```console-result { @@ -286,34 +441,57 @@ The following response displays the mappings that were created by {{es}}. } } ``` -::: :::: -::::{step} Define explicit mapping +::::: +:::::{step} Define explicit mapping Create an index named `my-explicit-mappings-books` and specify the mappings yourself. Pass each field's properties as a JSON object. This object should contain the [field data type](elasticsearch://reference/elasticsearch/mapping-reference/field-data-types.md) and any additional [mapping parameters](elasticsearch://reference/elasticsearch/mapping-reference/mapping-parameters.md). -```console -PUT /my-explicit-mappings-books -{ - "mappings": { - "dynamic": false, <1> - "properties": { <2> - "name": { "type": "text" }, - "author": { "type": "text" }, - "release_date": { "type": "date", "format": "yyyy-MM-dd" }, - "page_count": { "type": "integer" } - } - } -} -``` +::::{tab-set} +:group: api-examples + +:::{tab-item} Console +:sync: console + +:::{include} _snippets/index-basics/example6-console.md +::: + +:::{tab-item} curl +:sync: curl + +:::{include} _snippets/index-basics/example6-curl.md +::: + +:::{tab-item} Python +:sync: python + +:::{include} _snippets/index-basics/example6-python.md +::: + +:::{tab-item} JavaScript +:sync: js + +:::{include} _snippets/index-basics/example6-js.md +::: + +:::{tab-item} PHP +:sync: php -1. `dynamic`: Turns off dynamic mapping for the index. If you don't define fields in the mapping, they'll still be stored in the document's `_source` field, but you can't index or search them. -2. `properties`: Defines the fields and their corresponding data types. +:::{include} _snippets/index-basics/example6-php.md +::: + +:::{tab-item} Ruby +:sync: ruby + +:::{include} _snippets/index-basics/example6-ruby.md +::: + +:::: The following response indicates a successful operation. -:::{dropdown} Example response +::::{dropdown} Example response ```console-result { "acknowledged": true, @@ -321,29 +499,66 @@ The following response indicates a successful operation. "index": "my-explicit-mappings-books" } ``` -::: +:::: Explicit mappings are defined at index creation, and documents must conform to these mappings. You can also use the [update mapping API]({{es-apis}}operation/operation-indices-put-mapping). When an index has the `dynamic` flag set to `true`, you can add new fields to documents without updating the mapping, which allows you to combine explicit and dynamic mappings. Learn more about [managing and updating mappings](/manage-data/data-store/mapping.md#mapping-manage-update). -:::: ::::: +:::::: ## Search your data [getting-started-search-data] Indexed documents are available for search in near real-time, using the [`_search` API](/solutions/search/querying-for-search.md). -:::::{stepper} -::::{step} Search all documents +::::::{stepper} +:::::{step} Search all documents Use the following request to search all documents in the `books` index: -```console -GET books/_search -``` +::::{tab-set} +:group: api-examples + +:::{tab-item} Console +:sync: console + +:::{include} _snippets/index-basics/example7-console.md +::: + +:::{tab-item} curl +:sync: curl + +:::{include} _snippets/index-basics/example7-curl.md +::: + +:::{tab-item} Python +:sync: python + +:::{include} _snippets/index-basics/example7-python.md +::: + +:::{tab-item} JavaScript +:sync: js + +:::{include} _snippets/index-basics/example7-js.md +::: + +:::{tab-item} PHP +:sync: php + +:::{include} _snippets/index-basics/example7-php.md +::: + +:::{tab-item} Ruby +:sync: ruby + +:::{include} _snippets/index-basics/example7-ruby.md +::: -:::{dropdown} Example response +:::: + +::::{dropdown} Example response ```console-result { "took": 2, <1> @@ -389,30 +604,60 @@ GET books/_search 9. `_score`: The relevance score of the document 10. `_source`: The original JSON object submitted during indexing -::: :::: -::::{step} Search with a match query +::::: +:::::{step} Search with a match query Use the [`match` query](elasticsearch://reference/query-languages/query-dsl/query-dsl-match-query.md) to search for documents that contain a specific value in a specific field. This is the standard query for full-text searches. Use the following request to search the `books` index for documents containing `brave` in the `name` field: -```console -GET books/_search -{ - "query": { - "match": { - "name": "brave" - } - } -} -``` +::::{tab-set} +:group: api-examples -:::{tip} -This example uses [Query DSL](/explore-analyze/query-filter/languages/querydsl.md), which is the primary query language for {{es}}. +:::{tab-item} Console +:sync: console + +:::{include} _snippets/index-basics/example8-console.md +::: + +:::{tab-item} curl +:sync: curl + +:::{include} _snippets/index-basics/example8-curl.md ::: -:::{dropdown} Example response +:::{tab-item} Python +:sync: python + +:::{include} _snippets/index-basics/example8-python.md +::: + +:::{tab-item} JavaScript +:sync: js + +:::{include} _snippets/index-basics/example8-js.md +::: + +:::{tab-item} PHP +:sync: php + +:::{include} _snippets/index-basics/example8-php.md +::: + +:::{tab-item} Ruby +:sync: ruby + +:::{include} _snippets/index-basics/example8-ruby.md +::: + +:::: + +::::{tip} +This example uses [Query DSL](/explore-analyze/query-filter/languages/querydsl.md), which is the primary query language for {{es}}. +:::: + +::::{dropdown} Example response ```console-result { "took": 9, @@ -447,9 +692,9 @@ This example uses [Query DSL](/explore-analyze/query-filter/languages/querydsl.m ``` 1. `max_score`: Score of the highest-scoring document in the results. In this case, there is only one matching document, so the `max_score` is the score of that document. -::: :::: ::::: +:::::: ## Delete your indices [getting-started-delete-indices] @@ -457,16 +702,52 @@ If you want to delete an index to start from scratch at any point, use the [dele For example, use the following request to delete the indices created in this quickstart: -```console -DELETE /books -DELETE /my-explicit-mappings-books -``` +::::{tab-set} +:group: api-examples -::::{warning} -Deleting an index permanently deletes its documents, shards, and metadata. +:::{tab-item} Console +:sync: console + +:::{include} _snippets/index-basics/example9-console.md +::: + +:::{tab-item} curl +:sync: curl + +:::{include} _snippets/index-basics/example9-curl.md +::: + +:::{tab-item} Python +:sync: python + +:::{include} _snippets/index-basics/example9-python.md +::: + +:::{tab-item} JavaScript +:sync: js + +:::{include} _snippets/index-basics/example9-js.md +::: + +:::{tab-item} PHP +:sync: php + +:::{include} _snippets/index-basics/example9-php.md +::: + +:::{tab-item} Ruby +:sync: ruby + +:::{include} _snippets/index-basics/example9-ruby.md +::: :::: +:::::{warning} +Deleting an index permanently deletes its documents, shards, and metadata. + +::::: + ## Next steps This quickstart introduced the basics of creating indices, adding data, and performing basic searches with {{es}}.