使用multipass 安装
sudo snap install multipass
安装ubuntu20.04
multipass launch --name ubuntu
multipass list
Name State IPv4 Image
ubuntu Running 10.16.232.162 Ubuntu 20.04 LTS
进入虚拟机
multipass shell ubuntu
uname -a
Linux ubuntu 5.4.0-99-generic #112-Ubuntu SMP Thu Feb 3 13:50:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
sudo apt update && sudo apt upgrade
Install Java
sudo apt install openjdk-8-jdk
Verify the installation by checking the java version
java -version
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-8u312-b07-0ubuntu1~20.04-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)
Install and configure Elasticsearch
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
Create the repository file using the command
echo "deb https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-8.x.list
sudo apt update && sudo apt install elasticsearch
注意会生成用户密码,类似这样。
--------------------------- Security autoconfiguration information ------------------------------
Authentication and authorization are enabled.
TLS for the transport and HTTP layers is enabled and configured.
The generated password for the elastic built-in superuser is : wynW6PEAcpowTdai89*F
If this node should join an existing cluster, you can reconfigure this with
'/usr/share/elasticsearch/bin/elasticsearch-reconfigure-node --enrollment-token <token-here>'
after creating an enrollment token on your existing cluster.
You can complete the following actions at any time:
Reset the password of the elastic built-in superuser with
'/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic'.
The default configuration file of elasticsearch is located at /etc/elasticsearch/elasticsearch.yml . Use any text editor and uncomment the lines:
network.host: localhost
http.port: 9200
Start and enable elasticsearch
sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch
Run the following command to view Elasticsearch status and details
curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://localhost:9200
Enter host password for user 'elastic':
{
"name" : "ubuntu",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "tAw89cXJRmuth97Tm2bxcA",
"version" : {
"number" : "8.0.0",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "1b6a7ece17463df5ff54a3e1302d825889aa1161",
"build_date" : "2022-02-03T16:47:57.507843096Z",
"build_snapshot" : false,
"lucene_version" : "9.0.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
或者:
curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic:wynW6PEAcpowTdai89*F https://localhost:9200
{
"name" : "ubuntu",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "tAw89cXJRmuth97Tm2bxcA",
"version" : {
"number" : "8.0.0",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "1b6a7ece17463df5ff54a3e1302d825889aa1161",
"build_date" : "2022-02-03T16:47:57.507843096Z",
"build_snapshot" : false,
"lucene_version" : "9.0.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
计算集群中文档的数量
curl --cacert /etc/elasticsearch/certs/http_ca.crt -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq -XGET 'https://localhost:9200/_count?pretty' -d '
返回值:
{
"count" : 0,
"_shards" : {
"total" : 0,
"successful" : 0,
"skipped" : 0,
"failed" : 0
}
}
创建第一个索引
curl --cacert /etc/elasticsearch/certs/http_ca.crt -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq -X PUT https://localhost:9200/my-index-000001?pretty
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "my-index-000001"
}
查看是否成功创建索引
curl -k -u elastic:DCukiJJUAq4u7cEcDyHq "https://localhost:9200/_cat/indices?v"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open my-index-000001 W0g1xmavRciKFTbBljq8Ng 1 1 0 0 225b 225b
添加数据
curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq -X POST -d'{"@timestamp": "2099-05-06T16:21:15.000Z","event": {"original": "192.0.2.42 - - [06/May/2099:16:21:15 +0000] \"GET /images/bg.jpg HTTP/1.0\" 200 24736"}}' "https://localhost:9200/logs-my_app-default/_doc?pretty"
结果:
{
"_index" : ".ds-logs-my_app-default-2022.02.28-000001",
"_id" : "giyiP38BLdok8EvkSQ4j",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
命令
curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq -X POST -d'{"@timestamp": "2099-05-06T16:21:15.000Z","event": {"original": "192.0.2.42 - - [06/May/2099:16:21:15 +0000] \"GET /images/bg.jpg HTTP/1.0\" 200 24736"}}' "https://localhost:9200/my-index-000001/_doc?pretty"
结果
{
"_index" : "my-index-000001",
"_id" : "hCyiP38BLdok8Evkzg7X",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
创建新的索引(index)和类别(type)
Create index命令
curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq -XPUT "https://localhost:9200/i1"
结果
{"acknowledged":true,"shards_acknowledged":true,"index":"i1"}
命令:
curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq -XPUT "https://localhost:9200/i2?pretty"
结果:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "i2"
}
命令:
curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq "https://localhost:9200/i1/_mapping?pretty"
结果:
{
"i1" : {
"mappings" : { }
}
}
ElasticSearch8.0.0已经移除了对type的支持,不再使用type了。
命令
curl -k -u elastic:DCukiJJUAq4u7cEcDyHq -H "Content-Type: application/json" -XPUT "https://localhost:9200/i3?pretty" -d '{"mappings": {"properties": {"field3": {"type": "text"},"field4": {"type": "text"}}}}'
结果
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "i3"
}
命令
curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq "https://localhost:9200/i3/_mapping?pretty"
结果
{
"i3" : {
"mappings" : {
"properties" : {
"field3" : {
"type" : "text"
},
"field4" : {
"type" : "text"
}
}
}
}
}
参考链接:https://www.elastic.co/cn/blog/moving-from-types-to-typeless-apis-in-elasticsearch-7-0
添加记录命令
curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq -XPUT "https://localhost:9200/i3/_doc/1" -d '{"field3":"3", "field4":"4"}'
结果
{"_index":"i3","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
再次运行命令
curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq -XPUT "https://localhost:9200/i3/_doc/1?pretty" -d '{"field3":"3", "field4":"4"}'
结果:
{
"_index" : "i3",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
查询当前记录命令
curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq "https://localhost:9200/i3/_doc/1?pretty"
结果
{
"_index" : "i3",
"_id" : "1",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"field3" : "3",
"field4" : "4"
}
}
查询命令
curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq "https://localhost:9200/i3/_search?pretty"
结果
{
"took" : 1008,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "i3",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"field3" : "3",
"field4" : "4"
}
}
]
}
}