LeanDB Elasticsearch Guide
LeanDB Elasticsearch is the hosted database provided by Cloud Engine. You can have your project connect to the database using Elasticsearch libraries or HTTP APIs and have access to all the functions provided by Elasticsearch. See Cloud Engine Overview to learn about the other hosted database services provided by Cloud Engine.
Elasticsearch has the following features:
- High availability: With clusters containing multiple nodes, errors on a single node won’t affect the availability of the service.
- Resizing online: Adjust the size of your instance at any time.
- Multiple instances: Get larger storage and better performance as you need.
Creating and Managing Instances
You can create and manage LeanDB Elasticsearch instances on Developer Center > Your game > Game Services > Cloud Services > Cloud Engine > Database > Elasticsearch.
Creating Instances
You will see the following options when you click Create instance:
- Specification You can choose from
512M
,1GB
,2GB
,4GB
, and8GB
.
Each specification has its limit on the space. Upgrade to a higher specification to allow for more space.
You’ll see the price of the current instance once you choose a specification.
More about the billing of LeanDB
LeanDB instances are charged every day. If you use an instance for less than one day, you will be charged for one day’s usage. You will be billed for your usage of the previous day every single day. You will be charged for your LeanDB instances based on the specification you chose. Even though you don’t use the service after you create an instance, you will still be billed. You will see the prices of different specifications when you create your LeanDB instance. You can also view the prices on the pricing page. You can view your bill history on the Transactions page on the Developer Center.
Elasticsearch Version
LeanDB only provides Elasticsearch 7.9 at this time.
Resizing Online
At this time, you can’t resize LeanDB Elasticsearch instances on your own. Please reach out to us if you need to have your instances resized.
Sharing Instances
You can use the “Manage sharing” function to share your LeanDB instances with other applications. When you share an instance with another application, the instance will appear in this application. The relevant environment variables will also be available in this application’s Cloud Engine instances.
Accessing From Cloud Engine
When you deploy a project to the Cloud Engine instances under an application, some environment variables containing information for connecting to Elasticsearch will be injected, including:
ELASTICSEARCH_URL_<NAME>
Here <NAME>
is the name you provided when creating your LeanDB instance. If the name of your LeanDB instance is MYES
, there will be an environment variable named ELASTICSEARCH_URL_MYES
.
The environment variable has a format like http://username:password@host:port
, containing everything you need to connect to the instance, including credentials.
- Node.js
To connect to Elasticsearch from Node.js:
const { Client } = require("@elastic/elasticsearch");
const client = new Client({
node: process.env.ELASTICSEARCH_URL_MYES,
});
// promise API
const result = await client.search({
index: "my-index",
body: {
query: {
match: { hello: "world" },
},
},
});
// callback API
client.search(
{
index: "my-index",
body: {
query: {
match: { hello: "world" },
},
},
},
(err, result) => {
if (err) console.log(err);
}
);
- Make sure to install the dependencies used in the code above with
npm install @elastic/elasticsearch
- See Elasticsearch Node.js client’s docs for more information
Custom Dictionary
You can upload a custom dictionary on the dashboard. The dictionary should be encoded with UTF-8 with every line containing a word. The file should be no larger than 10 MB. For example:
Object-oriented programming
Functional programming
Higher-order function
Responsive web design
Save the content into a text file with its name being something like dict.txt
. Once you upload the file, it should be effective in 2 minutes. You can test if the dictionary is working with the analyze API. Keep in mind that when using the analyze API, an index has to be specified, so the request looks like curl -X POST "localhost:9200/my-index/_analyze?pretty"
.
Managing Data
Besides accessing LeanDB with your code from Cloud Engine, you can also use the following ways to manage, debug, or perform operations on your instances.
Connecting With the CLI
You can open an interactive shell connected to LeanDB using tds db shell
, a command provided by the CLI:
$ tds db shell mysqldb
Welcome to the MySQL monitor.
Your MySQL connection id is 3450564
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test
Database changed
mysql> insert into users set name = 'leancloud';
Query OK, 1 row affected (0.04 sec)
mysql> select * from users;
+------+-----------+
| id | name |
+------+-----------+
| 1 | zhenzhen |
| 2 | leancloud |
+------+-----------+
2 rows in set (0.06 sec)
With tds db proxy
, you can export LeanDB to a local port and have local programs or GUI clients connect to the database:
$ tds db proxy myredis
[INFO] Now, you can connect myredis via [redis-cli -h 127.0.0.1 -a hsdt9wIAuKcTZmpg -p 5678]
As long as you keep the terminal open, you’ll be able to access LeanDB from the port 5678. You can use a GUI client to browse and interact with LeanDB. While running your project with tds up
, you can also have your program connected to LeanDB using this feature. You can set the environment variable (from the output of tds db proxy
):
export REDIS_URL_myredis=redis://default:[email protected]:5678
You should only use tds db
for developing and debugging locally. Don’t use it for the production environment, as the connection might be interrupted occasionally.