CASE1: 查看ES中有哪些索引
//command:
GET /_cat/indices
//return
green open .kibana_7.15.1_001 xcU1uNbVS-eb2HI-79NT9A 1 0 66 48 2.4mb 2.4mb
green open .security-7 RHOq3AFWQ3y8-GB7EM1xRQ 1 0 57 0 247kb 247kb
yellow open megacorp R_0Gf8aFQket3Uq73u7I7A 1 1 4 5 24.5kb 24.5kb
green open .apm-custom-link EtVP4ii0ToawK07SAvfzhg 1 0 0 0 208b 208b
green open .kibana-event-log-7.15.1-000001 J90ey43LQgCFkvurvxyl_g 1 0 15 0 27kb 27kb
green open .apm-agent-configuration nFGHLZfaRte22bPreC5Dqg 1 0 0 0 208b 208b
yellow open crazy HhBgRIapTn2ON5DNtL2Rdw 1 1 1 0 4.2kb 4.2kb
yellow open jd CMdVrC3ZTbWtfk40c0z79g 1 1 1 1 6.7kb 6.7kb
green open .tasks 6nf34TnYQhiKUJUzlaQgjw 1 0 22 0 48.3kb 48.3kb
green open .kibana_task_manager_7.15.1_001 wpGZUDNWR0ig_rqVZqihIQ 1 0 16 65613 5.8mb 5.8mb
CASE2: 查看指定索引的相关信息
GET /spouse
{
"spouse" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : { //field 字段,age,整形
"type" : "long"
},
"desc" : { //field信息
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : { //field信息
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1", //分片数量
"provided_name" : "spouse", //索引名称
"creation_date" : "1637047304536", //创建日期
"number_of_replicas" : "1", //副本数量
"uuid" : "ShAkxFOVSu6fOjBznE6C6A",
"version" : {
"created" : "7150199"
}
}
}
}
}
CASE3: 根据ID查看文档
GET /spouse/female/1
{
"_index" : "spouse",
"_type" : "female",
"_id" : "1",
"_version" : 1,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "llj",
"age" : 18,
"desc" : "student"
}
}
CASE4: 查看索引下面所有文档
GET /spouse/female/_search
{
"_index" : "spouse",
"_type" : "female",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "liuyifei",
"age" : 21,
"desc" : "actress"
}
},
{
"_index" : "spouse",
"_type" : "female",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "luxueqi",
"age" : 22,
"desc" : "anchor"
}
CASE5: 查看AGE是20岁的文档
GET /spouse/female/_search?q=age:20
{
"_index" : "spouse",
"_type" : "female",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "lengmeng",
"age" : 20,
"desc" : "actress"
}
}
注: 如果设置了认证,使用curl执行命令的时候需要带上账号信息
curl --user elastic:password -X GET "localhost:9200/megacorp/employee/_search?q=last_name:Smith&pretty"
CASE6: DSL方式搜索
GET /spouse/female/_search
{
"query" : {
"match" : {
"age" : "21"
}
}
}
//return
{
"_index" : "spouse",
"_type" : "female",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "liuyifei",
"age" : 21,
"desc" : "actress"
}
或者只展示特定字段
GET /spouse/female/_search
{
"query" : {
"match" : {
"age" : "21"
}
},
"_source":["name"] //展示字段
}
排序:
GET /spouse/female/_search
{
"query" : {
"match" : {
"desc" : "beautiful"
}
},
"sort":[
{
"age":{
"order":"desc" //asc升序,desc降序
}
}
]
}
分页查询
GET /spouse/female/_search
{
"query" : {
"match" : {
"desc" : "beautiful"
}
},
"sort":[
{
"age":{
"order":"desc"
}}
],
"from":0, //从哪条开始,下标从0开始
"size":1 // 每页的大小
}
CASE 7:
搜索描述为actress,并且年龄大于20岁的文档
//COMMAND
GET /spouse/female/_search
{
"query" : {
"bool": {
"must": {
"match" : {
"desc" : "actress"
}
},
"filter": {
"range" : {
"age" : { "gt" : 20 }
}
}
}
}
}
//return
"hits" : [
{
"_index" : "spouse",
"_type" : "female",
"_id" : "4",
"_score" : 0.6931471,
"_source" : {
"name" : "liuyifei",
"age" : 21,
"desc" : "actress"
}
}
]
must表示所有条件都要符合,相当于mysql 中的and条件;
should,相当于mysql or 条件
must not : 相当于not,不等于
filter: 过滤器,对查询得到的结果进行过滤筛选;
大小判断,同shell
gt: 大于
gte: 大于等于
lt: 小于
lte: 小于等于
eq: 等于
多条件查询
GET /spouse/female/_search
{
"query" : {
"bool": {
"must":
[
{
"match" :
{
"desc" : "beautiful"
}
},
{
"match":
{
"age": "21"
}
}
]
}
}
}
}
CASE 8: 全文搜索,模糊查询
//command
GET /spouse/female/_search
{
"query" : {
"match" : {
"desc" : "actress"
}
}
}
//return
"hits" : [
{
"_index" : "spouse",
"_type" : "female",
"_id" : "3",
"_score" : 0.6931471,
"_source" : {
"name" : "lengmeng",
"age" : 20,
"desc" : "actress"
}
},
{
"_index" : "spouse",
"_type" : "female",
"_id" : "4",
"_score" : 0.6931471,
"_source" : {
"name" : "liuyifei",
"age" : 21,
"desc" : "actress"
}
}
CASE 9: 短语搜索,搜索特定的短语,将其当做一个整体
//command
GET /spouse/female/_search
{
"query" : {
"match" : {
"desc" : "play tennis"
}
}
}
//return
"hits" : [
{
"_index" : "spouse",
"_type" : "female",
"_id" : "7",
"_score" : 2.160181,
"_source" : {
"name" : "wangfei",
"age" : 32,
"desc" : "play tennis"
}
},
{
"_index" : "spouse",
"_type" : "female",
"_id" : "8",
"_score" : 2.160181,
"_source" : {
"name" : "daqiqi",
"age" : 32,
"desc" : "tennis play"
}
}
CASE 10: 高亮搜索
GET /spouse/female/_search
{
"query" : {
"match_phrase" : {
"desc" : "play tennis"
}
},
"highlight": {
"fields" : {
"desc" : {}
}
}
}
//return
"hits" : [
{
"_index" : "spouse",
"_type" : "female",
"_id" : "7",
"_score" : 2.160181,
"_source" : {
"name" : "wangfei",
"age" : 32,
"desc" : "play tennis"
},
"highlight" : {
"desc" : [
"<em>play</em> <em>tennis</em>"
]
}
}
CASE 11: 数据聚合,类似group by, 例如按照年龄进行分类
GET /spouse/female/_search
{
"aggs": {
"all_interests": {
"terms": { "field": "age" }
}
}
}
//return
"aggregations" : {
"all_interests" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 32,
"doc_count" : 3
},
{
"key" : 18,
"doc_count" : 1
},
{
"key" : 19,
"doc_count" : 1
},
{
"key" : 20,
"doc_count" : 1
},
{
"key" : 21,
"doc_count" : 1
},
{
"key" : 22,
"doc_count" : 1
}
]
}
}
CASE 12: 数据汇总
GET /spouse/female/_search
{
"aggs" : {
"all_interests" : {
"terms" : { "field" : "age" },
"aggs" : {
"avg_age" : {
"sum": { "field" : "age" }
}
}
}
}
}
//return
"aggregations" : {
"all_interests" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 32,
"doc_count" : 3,
"avg_age" : {
"value" : 96.0
}
},
{
"key" : 18,
"doc_count" : 1,
"avg_age" : {
"value" : 18.0
}
},
{
"key" : 19,
"doc_count" : 1,
"avg_age" : {
"value" : 19.0
}
},
{
"key" : 20,
"doc_count" : 1,
"avg_age" : {
"value" : 20.0
}
},
{
"key" : 21,
"doc_count" : 1,
"avg_age" : {
"value" : 21.0
}
},
{
"key" : 22,
"doc_count" : 1,
"avg_age" : {
"value" : 22.0
}
}
]
}
}