大数据学习(35)—— ElasticSearch实验
安装ElasticSearch
在安装ES之前,需要安装JDK。
从官网下载ES8.1压缩包,在服务器上解压,只部署一个节点用来演示。
[es@localhost home]$ ls -ltr
总用量 767132
-rw-r--r--. 1 root root 515805548 3月 21 11:40 elasticsearch-8.1.0-linux-x86_64.tar.gz
drwxr-xr-x. 10 es es 167 3月 21 12:57 elasticsearch-8.1.0
修改elasticsearch-8.1.0/config/elasticsearch.yml,这几个配置需要修改一下。
network.host: 192.168.0.31
http.port: 9200
xpack.security.enabled: false
http.host: 0.0.0.0
discovery.seed_hosts: ["0.0.0.0","[::1]"]
ingest.geoip.downloader.enabled: false
切换目录到elasticsearch-8.1.0/bin,启动ElasticSearch。
[es@localhost bin]$ sh elasticsearch -d
启动完成后,查看ES状态。
[es@localhost bin]$ curl -X GET 'localhost:9200/_cluster/health?pretty'
{
"cluster_name" : "elasticsearch",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 10,
"active_shards" : 10,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
curl操作ES
创建索引
[es@localhost bin]$ curl -X PUT "localhost:9200/db3?pretty"
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "db3"
}
查看索引
@localhost bin]$ curl -X GET 'http://localhost:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open db1 eyOyU8sQTaeOGvdAXF1P4A 1 0 0 0 225b 225b
yellow open db3 2MIskmW5QaStxt7w4qTg_w 1 1 0 0 225b 225b
green open db2 ebgp3yDqQqSbDvp-2Yuxtw 1 0 0 0 225b 225b
从索引状态可以看出,刚刚创建的db3状态是yellow。原因是单机不能启动副本,要把副本数设置为0。
[es@localhost bin]$ curl -X PUT "localhost:9200/db3/_settings" -H 'Content-Type: application/json' -d'
> {
> "index" : {
> "number_of_replicas" : 0
> }
> }
> '
返回:
{"acknowledged":true}
[es@localhost bin]$
[es@localhost bin]$ curl -X GET 'http://localhost:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open db1 eyOyU8sQTaeOGvdAXF1P4A 1 0 0 0 225b 225b
green open db3 2MIskmW5QaStxt7w4qTg_w 1 0 0 0 225b 225b
green open db2 ebgp3yDqQqSbDvp-2Yuxtw 1 0 0 0 225b 225b
创建文档
[es@localhost bin]$ curl -H 'Content-Type:application/json' -X POST http://localhost:9200/db3/_doc/1 -d '{
"id": "1",
"name": "ZhangSan",
"age": 17,
"sex": "M"
}'
返回:
{"_index":"db3","_id":"1","_version":1,"result":"created","_shards":{"total":1,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
通过ID查看文档
[es@localhost bin]$ curl -X GET localhost:9200/db3/_doc/1?pretty
{
"_index" : "db3",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"id" : "1",
"name" : "ZhangSan",
"age" : 17,
"sex" : "M"
}
}
查找索引中所有文档
[es@localhost bin]$ curl -X GET localhost:9200/db3/_search?pretty
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "db3",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : "1",
"name" : "ZhangSan",
"age" : 17,
"sex" : "M"
}
}
]
}
}
match查询
[es@localhost bin]$ curl -X GET "localhost:9200/db3/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query" : {
"match" : {
"name" : "ZhangSan"
}
}
}'
返回:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "db3",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"id" : "1",
"name" : "ZhangSan",
"age" : 17,
"sex" : "M"
}
}
]
}
}
filter查询
[es@localhost bin]$ curl -X GET "localhost:9200/db3/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query" : {
"bool": {
"filter": {
"range" : {
"age" : { "gt" : 15 }
}
}
}
}
}'
返回:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "db3",
"_id" : "1",
"_score" : 0.0,
"_source" : {
"id" : "1",
"name" : "ZhangSan",
"age" : 17,
"sex" : "M"
}
}
]
}
}
更多的语法使用,请参考ElasticSearch:权威指南,这里面有些语法在8.1里已经不支持了。