Truy cập trang chủ Elasticsearch thường gặp vấn đề về tốc độ. Nên sử dụng nguồn mirror tại https://elasticsearch.cn/download/ để tải phiên bản tương thích, kết hợp với phần mềm hỗ trợ download như迅雷 để đạt tốc độ tối ưu (khoảng 40MB/s).
Cài đặt từ file nén
Khởi động Elasticsearch bằng file elasticsearch-7.10.0\bin\elasticsearch.bat. Kiểm tra hoạt động thành công qua URL http://127.0.0.1:9200.
Chạy Kibana thông qua kibana-7.10.0-windows-x86_64\bin\kibana.bat và truy cập giao diện quản trị tại http://localhost:5601.
Cấu hình xác thực người dùng
Thêm cấu hình bảo mật trong file config/elasticsearch.yml:
xpack.security.enabled: true
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.enabled: true
Khởi động lại Elasticsearch và thiết lập mật khẩu bằng lệnh:
.\bin\elasticsearch-setup-passwords.bat interactive
Hệ thống sẽ yêu cầu nhập mật khẩu cho các tài khoản hệ thống (elastic, kibana_system,...). Lưu ý: Mật khẩu đơn giản như 123456 chỉ dùng cho môi trường test, môi trường production cần sử dụng mật khẩu mạnh.
Cập nhật thông tin đăng nhập vào file kibana-7.10.0-windows-x86_64\config\kibana.yml:
elasticsearch.hosts: ["http://localhost:9200"]
elasticsearch.username: "kibana_system"
elasticsearch.password: "123456"
Khởi động lại Kibana để áp dụng cấu hình.
Nhập dữ liệu mẫu
Tạo index news qua Kibana Dev Tools:
PUT /news
{
"mappings": {
"properties": {
"id": { "type": "long" },
"title": { "type": "text", "analyzer": "standard" },
"tags": { "type": "keyword" },
"read_count": { "type": "long" },
"like_count": { "type": "long" },
"comment_count": { "type": "long" },
"rank": { "type": "double" },
"location": { "type": "geo_point" },
"pub_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd HH:mm||yyyy-MM-dd||epoch_millis"
}
}
}
}
Thêm dữ liệu mẫu bằng lệnh bulk:
POST _bulk
{"create": {"_index": "news", "_id": 1}}
{"comment_count":600,"id":1,"like_count":2000,"location":[118.55199,24.78144],"pub_time":"2023-07-29 09:47","rank":0.0,"read_count":10000,"tags":["bão","Dusu","Miền Nam"],"title":"Bão Dusu đổ bộ Miền Nam"}
{"create": {"_index": "news", "_id": 2}}
{"comment_count":60,"id":2,"like_count":200,"location":[116.23128,40.22077],"pub_time":"2023-06-29 14:49:38","rank":0.0,"read_count":1000,"tags":["bão","Dusu","Bắc Kinh"],"title":"Bắc Kinh chuẩn bị ứng phó bão Dusu"}
{"create": {"_index": "news", "_id": 3}}
{"comment_count":6,"id":3,"like_count":20,"location":[120.21201,30.208],"pub_time":"2020-07-29 14:49:38","rank":0.99,"read_count":100,"tags":["bão","Hàng Châu"],"title":"Hàng Châu dỡ cảnh báo bão"}
Kết nối từ ứng dụng Java
Thêm dependency vào file pom.xml:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.10.0</version>
</dependency>
Triển khai lớp kết nối và phương thức truy vấn:
private static final RestHighLevelClient elasticClient;
static {
final var authProvider = new BasicCredentialsProvider();
authProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials("elastic", "123456")
);
final var clientBuilder = RestClient.builder(new HttpHost("127.0.0.1", 9200))
.setHttpClientConfigCallback(httpBuilder ->
httpBuilder.setDefaultCredentialsProvider(authProvider)
);
elasticClient = new RestHighLevelClient(clientBuilder);
}
public static Map<String, Object> fetchDocument(String indexName, String docId)
throws IOException {
final var request = new GetRequest(indexName, docId);
final var response = elasticClient.get(request, RequestOptions.DEFAULT);
return response.getSourceAsMap();
}
public static void main(String[] args) throws IOException {
final var result = fetchDocument("news", "1");
result.forEach((key, value) -> System.out.println(key + ": " + value));
}