Example of how to connect and use Elasticsearch on IBM Cloud Databases using bash automation and CURL commands
Example how to connect to Elasticsearch on IBM Cloud with Databases for Elasticsearch using bash scripting with cURL.
Content:
- Setup
- Example usage of search indexes using bash automation
- Elasticsearch example CURL search command
Useful resources:
- Elasticsearch API documentation here you can easily copy the commands
- IBM Cloud Elasticsearch
CURL
documentation
git clone https://github.com/thomassuedbroecker/example-connect-to-elastic-search
cd example-connect-to-elastic-search
open https://cloud.ibm.com/databases/databases-for-elasticsearch/create
Download the certificate file into the folder ./code/cert.
cat code/.env_template > .env
# IBM Cloud
export IBM_CLOUD_API_KEY=XXXX
export IBM_CLOUD_REGION=us-south
export IBM_CLOUD_RESOURCE_GROUP=default
# Elasticsearch service
export E_SEARCH_SERVICE=YOUR_DatabasesForElasticsearch
export E_CERT_FILE_NAME=d5290bfc-XXXXX-XXX-9337-XXXX40bd
export E_HOST=XXXXX-XXXXX-XXXXX-XXXXX.XXXX.databases.appdomain.cloud
export E_PORT=0815
export E_ADMIN_USER=admin
export E_ADMIN_PASSWORD=YOUR_AWESOME_PASSWORD
cd /code
sh change_admin_password.sh
- Example output:
Key Value
ID crn:XXX
Deployment ID crn:vXXX
Description Updating user
Created At 2023-XX
Status running
Progress Percentage 0
Status completed
Progress Percentage 100
Location https://api.us-east.databases.cloud.ibm.com/XXXX
OK
cd /code
sh test_connection.sh
- Example output:
{
"cluster_name" : "XXX",
"status" : "green",
...
}
cd /code
sh create_indexes.sh
- Example output:
Create: documents
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 85
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "documents"
}
Create: passages
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 84
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "passages"
}
cd /code
sh upload-data_to_indexes.sh
- Example output:
[
{
"chunckid": "1.0815",
"text": [
"Hello world",
"This is the first hello world example!" ],
"url": "http://example.com/docs/0001",
"title": "Hello world"
},
{
"chunckid": "1.0815",
"text": [
"This is the second hello world example!" ],
"url": "http://example.com/docs/0001",
"title": "Hello world"
}
]HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 451
{"took":119,"errors":false,"items":[{"index":{"_index":"passages","_type":"_doc","_id":"http://example.com/docs/0001","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":0,"_primary_term":1,"status":201}},{"index":{"_index":"passages","_type":"_doc","_id":"http://example.com/docs/0001","_version":2,"result":"updated","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":1,"_primary_term":1,"status":200}}]}Upload next file: /Users/thomassuedbroecker/Downloads/dev/example-connect-to-elastic-search/code/full/full1.json
[
{
"title": "Hello world",
"url": "http://example.com/docs/0001",
"text": [
"Hello world",
"This is the first hello world example!",
"This is the second hello world example!"
]
}
]HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 245
{"took":117,"errors":false,"items":[{"index":{"_index":"documents","_type":"_doc","_id":"http://example.com/docs/0001","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":0,"_primary_term":1,"status":201}}]}
cd /code
sh search_in_indexes.sh
cd /code
sh delete_indexes.sh
mkdir cert
cd cert
export E_CERT_PATH=$(pwd)
Step 5: Download the certificate file for your Databases for Elasticsearch instance
Download the certificate file to your local computer into the newly created folder.
- Example:
export E_CERT_FILE_NAME=d5290bfc-XXXXX-XXX-9337-XXXX40bd
export E_HOST=XXXXX-XXXXX-XXXXX-XXXXX.XXXX.databases.appdomain.cloud
export E_PORT=0815
export E_ADMIN_USER=admin
export E_ADMIN_PASSWORD=YOUR_AWESOME_PASSWORD
CURL_CA_BUNDLE=$E_CERT_PATH/$E_CERT_FILE_NAME curl -u ${E_ADMIN_USER}:${E_ADMIN_PASSWORD} -XGET -H "Content-Type: application/json" "https://${E_HOST}:${E_PORT}/documents/_search" -d '{ "query": {"multi_match" : {"query" : "Second Hello World?","fields": ["title", "text"]}}}' | jq '.'
- Example output:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:100 453 100 423 100 30 1193 84 --:--:-- --:--:-- --:--:-- 1279
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "documents",
"_type": "_doc",
"_id": "http://example.com/docs/0001",
"_score": 1,
"_source": {
"title": "Hello world",
"url": "http://example.com/docs/0001",
"text": [
"Hello world",
"This is the first hello world example!",
"This is the second hello world example!"
]
}
}
]
}
}
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--: 0 0 0 0 0 0 0 0 --:--:-- --:--:100 526 100 435 100 91 1264 264 --:--:-- --:--:-- --:--:-- 1529
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.1918257,
"hits": [
{
"_index": "documents",
"_type": "_doc",
"_id": "http://example.com/docs/0001",
"_score": 1.1918257,
"_source": {
"title": "Hello world",
"url": "http://example.com/docs/0001",
"text": [
"Hello world",
"This is the first hello world example!",
"This is the second hello world example!"
]
}
}
]
}
}
Start Elasticsearch on your local computer using the Quarkus documentation
docker run --name elasticsearch -e "discovery.type=single-node" -e "ES_JAVA_OPTS=-Xms512m -Xmx512m"\
--rm -p 9200:9200 docker.io/elastic/elasticsearch:7.16.3
Example how to start with basic authentication.
docker run --name elasticsearch \
-e "discovery.type=single-node" \
-e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
-e "xpack.security.enabled=true" \
-e "ELASTIC_USERNAME=elastic" \
-e "ELASTIC_PASSWORD=123456789" \
--rm -p 9200:9200 docker.io/elastic/elasticsearch:7.16.3
cat code/.env_template > .env
export E_PORT=9200
export E_HOST=localhost
sh create_indexes_local.sh
sh upload-data_to_indexes_local.sh
sh search_in_index_local.sh
sh delete_indexes_local.sh