Introduction
Alibaba Cloud Elasticsearch is a fully managed cloud search-and-analytics engine that is developed based on open source Elasticsearch. Alibaba Cloud Elasticsearch is hosted on Alibaba Cloud to provide scalable, highly available, and secure search-and-analytics features. On Alibaba Cloud Elasticsearch, you just need to focus on search and analytics to build various applications related to data search, analytics, monitoring, and alerting without the need to worry over issues related to hardware, network, and data backup. Alibaba Cloud Elasticsearch is suitable for various scenarios, such as enterprise search, log analysis, security analysis, as well as monitoring and diagnostics. It can be integrated with other Alibaba Cloud services, such as Object Storage Service (OSS), ApsaraDB RDS, Data Transmission Service (DTS), and Virtual Private Cloud (VPC). Alibaba Cloud Elasticsearch also provides various data visualization tools, such as Kibana, Grafana, and Logstash, to help you search for and analyze data.
Features
-
Fast search and analytics: Alibaba Cloud Elasticsearch provides powerful search and analytics capabilities, supports features such as full-text search, aggregate analysis, and geographic location search, and can process large amounts of data in an efficient manner.
-
High scalability: Alibaba Cloud Elasticsearch has high scalability. It can be scaled up or out to fit your business requirements and improve system performance and capacity.
-
High availability: Alibaba Cloud Elasticsearch supports the multi-replica mechanism, which allows data to be backed up across multiple nodes. This helps achieve high data availability and fault tolerance.
-
Security: Alibaba Cloud Elasticsearch provides a variety of security measures, such as access control, data encryption, and security audit, to ensure data security and integrity.
-
Integration: Alibaba Cloud Elasticsearch can be integrated with various data sources and third-party tools, such as Hadoop, Spark, Logstash, and Kafka, to improve data availability and increase data values.
-
Ease of use: Alibaba Cloud Elasticsearch provides a user-friendly web interface and APIs for you to search and analyze data. It also supports client software development kits (SDKs) in multiple programming languages.
Instructions
1. Purchase an Alibaba Cloud Elasticsearch cluster
Background information
Alibaba Cloud Elasticsearch is a fully managed cloud search-and-analytics engine and is a hosting service for open source Elasticsearch on Alibaba Cloud. An Alibaba Cloud Elasticsearch cluster can contain multiple nodes, each of which runs Elasticsearch processes and stores data. These nodes can be automatically identified and added to an Alibaba Cloud Elasticsearch cluster. This provides high availability and fault tolerance.
Alibaba Cloud Elasticsearch provides a unified entry point for you to create, manage, and use Alibaba Cloud Elasticsearch clusters. You can use Alibaba Cloud Elasticsearch clusters to create search and analytics applications in an efficient manner, without the need to be concerned about issues, such as underlying infrastructure, security, and availability. This allows you to focus only on the search and analytics requirements of your business. Alibaba Cloud Elasticsearch also supports various data visualization tools, such as Kibana, Grafana, and Logstash, to help you search for and analyze data in an efficient manner.
Procedure
-
Log on to the Alibaba Cloud International site (alibabacloud.com). In the top navigation bar, choose Products > Analytics Computing > Elasticsearch.
-
On the product page of Alibaba Cloud Elasticsearch, click Buy Now. On the buy page of Alibaba Cloud Elasticsearch, select the version that you want to purchase, such as 7.10.
-
Configure the parameters of the Alibaba Cloud Elasticsearch cluster, such as Zone, Data Nodes, Data Node Storage Space, Data Node Disk Type, and Kibana Node Type. You must also configure the Network Type, VPC, and VSwitch parameters.
-
At the bottom of the buy page, configure the Username and Password parameters. These parameters are required when you access Alibaba Cloud Elasticsearch and Kibana.
-
Click Buy Now. Then, enter the password or select another payment method to complete the payment as promoted.
-
After the payment is successful, you can view the Alibaba Cloud Elasticsearch cluster that you purchase in the Elasticsearch console and configure and manage the Alibaba Cloud Elasticsearch cluster. You can also view information about the cluster on the cluster details page, such as basic information, node information, and network information, and perform operations such as security group configuration, access control configuration, and node management.
Precautions
Before you create an Alibaba Cloud Elasticsearch cluster, make sure that you have an Alibaba Cloud account and complete real-name verification.
When you create an Alibaba Cloud Elasticsearch cluster, configure the cluster based on your business requirements.
After you create an Alibaba Cloud Elasticsearch cluster, you can configure and manage the cluster. To ensure cluster security, we recommend that you configure parameters on the Network Settings page. Before you use Kibana, you must configure the Public Network Whitelist parameter on the Network Settings page.
2. Create an Elasticsearch index
Background information
An Elasticsearch index is a basic unit that is used to store data in Elasticsearch and is similar to a table in a relational database. An Elasticsearch index consists of a group of documents. Each of the documents has a group of fields and each of the fields contains a value. You can specify fields to search for and filter the documents in an index to meet your business requirements. All search and analytics operations of Elasticsearch are performed on an index basis.
Features of Elasticsearch indexes
-
Data storage: An Elasticsearch index serves as a container, which stores various data such as text, digits, dates, and geographic locations.
-
Search and analytics: Elasticsearch indexes support powerful search-and-analytics features and allow you to perform various operations in an efficient manner. For example, you can search for, filter, sort, and aggregate data in indexes.
-
Distribution: Elasticsearch indexes support distributed deployment. You can deploy Elasticsearch indexes on multiple nodes to improve high availability and fault tolerance.
-
Scalability: Elasticsearch indexes can be scaled out when the amount of data increases. You can dynamically add or delete nodes on which Elasticsearch indexes are deployed to improve system performance and availability.
-
Customization: Elasticsearch indexes support multiple attributes, such as custom mappings, analyzers, shards, and replicas. You can configure Elasticsearch indexes based on your business requirements.
Description of Elasticsearch indexes
-
Index name: An index name is a key concept in Elasticsearch and uniquely identifies an index. An index name contains lowercase letters, digits, and underscores (_) and cannot exceed 255 characters in length.
-
Mapping: A mapping in an Elasticsearch index specifies information about each field, such as the type, attribute, and analyzer. The mapping is static and cannot be modified after it is created.
-
Shard and replica: Data in an Elasticsearch index is stored in shards. Each shard is a Lucene index. To improve system availability and fault tolerance capabilities, Elasticsearch allows you to configure multiple replicas for a shard. Each replica is a complete copy of a shard.
-
Dynamic mapping: Elasticsearch indexes support dynamic mapping. When a new document is stored in an Elasticsearch index, Elasticsearch automatically creates a mapping based on the field information in the document.
-
Index alias: An Elasticsearch index can have multiple aliases. Aliases are used to simplify search and analytics operations. Each alias can point to multiple Elasticsearch indexes.
Procedure
Create an Elasticsearch index
-
Log on to the Elasticsearch console. In the left-side navigation pane, click Elasticsearch Clusters. On the Elasticsearch Clusters page, find the desired cluster and click the name of the cluster in the Cluster ID/Name column.
-
In the left-side navigation pane, choose Configuration and Management > Data Visualization. In the Kibana section of the page that appears, click Access over Internet.
-
On the Kibana logon page, enter the username and password and click Log in.
-
In the left-side navigation pane, choose Management > Dev Tools. On the Dev Tools page, click the Console tab.
-
On the Console tab, enter the command for creating an Elasticsearch index and click the Run icon to create the index.
Precautions
-
Before you create an Elasticsearch index, you need to learn about the syntax of creating Elasticsearch indexes.
-
An index name cannot contain uppercase letters.
-
When you configure fields during index creation, you need to specify the tokenizer and analyzer based on the data types of the fields and analysis requirements.
-
When you configure shards during index creation, you need to specify the number of shards and the number of replicas based on the amount of data and query requirements.
-
When you configure advanced settings during index creation, you need to specify an index alias and shard routes based on your business requirements.
-
After you create an index, you can view the detailed configuration information about the index in the Kibana console of Alibaba Cloud Elasticsearch. The information includes field, shard, and analyzer information.
Access and use Alibaba Cloud Elasticsearch
- Obtain information about the Elasticsearch cluster, such as the endpoint, username, and password, in the Elasticsearch console.
- Reference dependencies of the SDK for Java of Alibaba Cloud Elasticsearch and common dependencies of Redis in a Java project.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-elasticsearch</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.10.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.10.2</version>
</dependency>
- Create an Alibaba Cloud Elasticsearch client.
Use an Alibaba Cloud SDK to create an Alibaba Cloud Elasticsearch client.
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("accessKeyId", "accessKeySecret")); // Enter the actual username and password.
ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("es-cn-xxx.aliyuncs.com") // Enter the actual endpoint of Alibaba Cloud Elasticsearch.
.withConnectTimeout(Duration.ofSeconds(5))
.withSocketTimeout(Duration.ofSeconds(3))
.build();
RestHighLevelClient restClient = new RestHighLevelClient(
RestClient.builder(HttpHost.create("http://es-cn-xxx.aliyuncs.com"))
.setHttpClientConfigCallback(httpClientBuilder ->
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
.setConnectTimeout(5000)
.setSocketTimeout(3000))
.setHttpClientConfigCallback(httpClientBuilder ->
httpClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom()
.setIoThreadCount(1)
.build()))
);
Note: In the preceding code, you must replace accessKeyId with the actual username, replace accessKeySecret with the actual password, and replace es-cn-xxx.aliyuncs.com with the actual endpoint of your Alibaba Cloud Elasticsearch cluster.
- Use RESTClient to perform operations related to Alibaba Cloud Elasticsearch
You can use RESTClient to perform basic operations related to Alibaba Cloud Elasticsearch. For example, you can use RESTClient to create an Elasticsearch client and add, remove, modify, and query data.
●submitData: submits a data record.
●updateData: updates a data record.
●deleteData: deletes a data record.
●queryData: queries a data record.
●searchData: queries multiple data records.
●createIndex: creates an index.
RestHighLevelClient is an advanced Java client provided by Elasticsearch in RESTClient. RestHighLevelClient allows you to use Elasticsearch to perform various operations and provides easy-to-use API operations. You can also use other common Elasticsearch API operations, such as CreateIndexRequest and SearchSourceBuilder.
import java.io.IOException;
import java.util.Map;
import org.apache.http.HttpHost;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
public class ElasticsearchUtil {
// Client
private static RestHighLevelClient client;
/**
* Create a client.
*/
static {
client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
}
/**
* Submit a data record.
*
* @param index Index name
* @param type Type name
* @param id Data record ID
* @param data Data record
*/
public static void submitData(String index, String type, String id, Map<String, Object> data) {
IndexRequest request = new IndexRequest(index, type, id);
request.source(data);
try {
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Update a data record.
*
* @param index Index name
* @param type Type name
* @param id Data record ID
* @param data Data record
*/
public static void updateData(String index, String type, String id, Map<String, Object> data) {
UpdateRequest request = new UpdateRequest(index, type, id).doc(data);
try {
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Delete a data record.
*
* @param index Index name
* @param type Type name
* @param id Data record ID
*/
public static void deleteData(String index, String type, String id) {
DeleteRequest request = new DeleteRequest(index, type, id);
try {
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Query a data record.
*
* @param index Index name
* @param type Type name
* @param id Data record ID
* @return Data record
*/
public static Map<String, Object> queryData(String index, String type, String id) {
GetRequest request = new GetRequest(index, type, id);
try {
GetResponse response = client.get(request, RequestOptions.DEFAULT);
return response.getSourceAsMap();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* Query data records that meet the query condition.
*
* @param index Index name
* @param type Type name
* @param query Query condition
* @return Data record
*/
public static SearchResponse searchData(String index, String type, String query) {
SearchRequest request = new SearchRequest(index);
request.types(type);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.queryStringQuery(query));
request.source(sourceBuilder);
try {
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
return response;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* Create an index.
*
* @param index Index name
* @param type Type name
*/
public static void createIndex(String index, String type) {
try {
CreateIndexRequest request = new CreateIndexRequest(index);
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.startObject("properties")
.startObject("id")
.field("type", "text")
.endObject()
.startObject("name")
.field("type", "text")
.field("analyzer", "ik_max_word")
.endObject()
.startObject("age")
.field("type", "integer")
.endObject()
.startObject("address")
.field("type", "text")
.endObject()
.endObject()
.endObject();
request.mapping(type, builder);
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Was this helpful?
Was this helpful?