Windows下ElasticSearch学习(二)

2022-07-04 17:45:20 浏览数 (1)

今天继续学习ES 在Windows 下的使用,主要是通过curl 命令行来操作ES: 备注:说明一下ES 的版本为6.8.8.

1.查看ES的相关信息

代码语言:javascript复制
C:Userstdcengineer>curl http://127.0.0.1:9200/?pretty
{
"name" : "node-1",
"cluster_name" : "myes",
"cluster_uuid" : "jImvcOwnQbilYTD8JLNtFA",
"version" : {
"number" : "6.8.8",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "2f4c224",
"build_date" : "2020-03-18T23:22:18.622755Z",
"build_snapshot" : false,
"lucene_version" : "7.7.2",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}

2.创建一个索引

代码语言:javascript复制
C:Userstdcengineer>curl -XPUT http://127.0.0.1:9200/index_01
{"acknowledged":true,"shards_acknowledged":true,"index":"index_01"}

3.创建一个索引,并初始化一个文档

代码语言:javascript复制
C:Userstdcengineer>curl -XPUT -H "content-Type:application/json" "http://127.0.0.1:9200/testindex/student/1" -d"{"name":"tony","age":25,"score":99}"
{"_index":"testindex","_type":"student","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

C:Userstdcengineer>curl -XPUT -H "content-Type:application/json" "http://127.0.0.1:9200/testindex/student/2" -d"{"name":"tony","age":33,"score":99}"
{"_index":"testindex","_type":"student","_id":"2","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

4.更新指定的id=2的索引内容

代码语言:javascript复制
C:Userstdcengineer>curl -XPUT -H "content-Type:application/json" "http://127.0.0.1:9200/testindex/student/2" -d"{"name":"tony","age":32,"score":98}"
{"_index":"testindex","_type":"student","_id":"2","_version":2,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}

5.删除指定索引下的数据

代码语言:javascript复制
C:Userstdcengineer>curl -XDELETE -H "content-Type:application/json" "http://127.0.0.1:9200/testindex/student/2"
{"_index":"testindex","_type":"student","_id":"2","_version":3,"result":"deleted","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":2,"_primary_term":1}

6.删除指定的索引

代码语言:javascript复制
C:Userstdcengineer>curl -XDELETE -H "content-Type:application/json" "http://127.0.0.1:9200/test0823"
{"acknowledged":true}

7.获取指定索引下id=2 的数据信息

代码语言:javascript复制
C:Userstdcengineer>curl -XGET -H "content-Type:application/json" "http://127.0.0.1:9200/testindex/student/2"
{"_index":"testindex","_type":"student","_id":"2","_version":1,"_seq_no":3,"_primary_term":1,"found":true,"_source":{"name":"tony","age":32,"score":98}}

格式化后输出:

代码语言:javascript复制
C:Userstdcengineer>curl -XGET -H "content-Type:application/json" "http://127.0.0.1:9200/testindex/student/2?pretty"
{
"_index" : "testindex",
"_type" : "student",
"_id" : "2",
"_version" : 1,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "tony",
"age" : 32,
"score" : 98
}
}

8.查询指定索引下的所有数据

代码语言:javascript复制
C:Userstdcengineer>curl -XGET -H "content-Type:application/json" "http://127.0.0.1:9200/testindex/_search?pretty"
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "testindex",
"_type" : "student",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "tony",
"age" : 32,
"score" : 98
}
},
{
"_index" : "testindex",
"_type" : "student",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "tom",
"age" : 35,
"score" : 89
}
}
]
}
}

9.指定条件name=tony的数据

代码语言:javascript复制
C:Userstdcengineer>curl -XPOST -H "content-Type:application/json" "http://127.0.0.1:9200/testindex/_search?pretty" -d "{"query":{"match":{"name":"tony"}}}"
{
"took" : 72,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "testindex",
"_type" : "student",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"name" : "tony",
"age" : 32,
"score" : 98
}
}
]
}
}

10.查询条件为name=tony or tom 的数据

代码语言:javascript复制
C:Userstdcengineer>curl -XPOST -H "content-Type:application/json" "http://127.0.0.1:9200/testindex/_search?pretty" -d "{"query":{"match":{"name":"tony tom"}}}"
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "testindex",
"_type" : "student",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"name" : "tony",
"age" : 32,
"score" : 98
}
},
{
"_index" : "testindex",
"_type" : "student",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "tom",
"age" : 35,
"score" : 89
}
}
]
}
}

11.查询结果返回size=1的数据

代码语言:javascript复制
C:Userstdcengineer>curl -XPOST -H "content-Type:application/json" "http://127.0.0.1:9200/testindex/_search?pretty" -d "{"query":{"match":{"name":"tony"}},"size":1}"
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "testindex",
"_type" : "student",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"name" : "tony",
"age" : 32,
"score" : 98
}
}
]
}
}

12.从查询结果的第二条开始返回size=1的数据

代码语言:javascript复制
C:Userstdcengineer>curl -XPOST -H "content-Type:application/json" "http://127.0.0.1:9200/testindex/_search?pretty" -d "{"query":{"match":{"name":"tony"}},"size":1,"from":1}"
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "testindex",
"_type" : "student",
"_id" : "3",
"_score" : 0.2876821,
"_source" : {
"name" : "tony",
"age" : 33,
"score" : 90
}
}
]
}
}

13.设置mapping

代码语言:javascript复制
C:Userstdcengineer>curl -XPUT -H "content-Type:application/json" "http://127.0.0.1:9200/index0823/person/_mapping" -d "{"person" : {"properties" : {"name" : {"type" : "text"},"status" : {"type" : "integer" }}}}"
{"acknowledged":true}

14.获取mapping

代码语言:javascript复制
C:Userstdcengineer>curl -XGET -H "content-Type:application/json" "http://127.0.0.1:9200/testindex/_mapping?pretty"
{
"testindex" : {
"mappings" : {
"student" : {
"properties" : {
"age" : {
"type" : "long"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"score" : {
"type" : "long"
}
}
}
}
}
}

15.查看索引的配置信息

代码语言:javascript复制
C:Userstdcengineer>curl -XGET -H "content-Type:application/json" "http://127.0.0.1:9200/testindex/_settings?pretty"
{
"testindex" : {
"settings" : {
"index" : {
"creation_date" : "1598166190725",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "-Vc2G8szSLKNsmykPfL3WQ",
"version" : {
"created" : "6080899"
},
"provided_name" : "testindex"
}
}
}
}

16.设置索引的副本信息(分片个数不允许修改)

代码语言:javascript复制
C:Userstdcengineer>curl -XPUT -H "content-Type:application/json" "http://127.0.0.1:9200/testindex/_settings" -d "{"number_of_replicas":5}"
{"acknowledged":true}

总结:以上内容就是今天的全部分享,文章内容都是个人在工作中常用的一些操作示例总结,有些内容可能不太全面,大家需要自行补充一下(查资料),同时可以结合“ Windows下ElasticSearch学习(一)”文章一起来阅读。

友情提示:“无量测试之道”原创著作,欢迎关注交流,禁止第三方不显示文章来源时转载。

0 人点赞