Elasticsearch

一、集群状态查看

1:查看集群健康状态

        GET /_cat/health?v

2:查看节点状态;

        GET /_cat/nodes?v

3:查看所有索引信息

        GET /_cat/indices?v

4:查看主节点

        GET /_cat/master?v

二、索引操作

1:创建索引并查看;

        PUT /customer
        GET /_cat/indices?v

2:删除索引并查看

        DELETE /customer
        GET /_cat/indices?v

三、文档操作

1:在索引中添加文档;

        PUT /customer/doc/1
        {
          "name": "John Doe"
        }

2:查看索引中的文档;

        GET /customer/doc/1

3:修改索引中的文档:

        POST /customer/doc/1/_update
        {
            "doc": { "name": "Jane Doe" }
        }

4:删除索引中的文档;

        DELETE /customer/doc/1

5:对索引中的文档执行批量操作;

        POST /customer/doc/_bulk
        {"index":{"_id":"1"}}
        {"name": "John Doe" }
        {"index":{"_id":"2"}}
        {"name": "Jane Doe" }

四、文档的类型操作

1:创建文档的类型

       PUT /new_bank/
        {
          "mappings" : {
              "properties" : {
                "account_number" : {
                  "type" : "long"
                },
                "address" : {
                  "type" : "text"
                },
                "age" : {
                  "type" : "integer"
                },
                "balance" : {
                  "type" : "long"
                },
                "city" : {
                  "type" : "keyword"
                },
                "email" : {
                  "type" : "keyword"
                },
                "employer" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "firstname" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "gender" : {
                  "type" : "keyword"
                },
                "lastname" : {
                  "type" : "keyword"
                },
                "state" : {
                  "type" : "keyword"
                }
              }
            }
        }

2:查看文档的类型

      GET /new_bank/_mapping

五、简单收索

1:最简单的搜索,使用match_all来表示,例如搜索全部

        GET /bank/_search
        {
          "query": { "match_all": {} }
        }

2:分页搜索,from表示偏移量,从0开始,size表示每页显示的数量

        GET /bank/_search
        {
            "query": { "match_all": {} },
            "from": 0,
            "size": 10
        }

3:搜索排序,使用sort表示,例如按balance字段降序排列

        GET /bank/_search
        {
            "query": { "match_all": {} },
            "sort": { "balance": { "order": "desc" } }
        }

4:搜索并返回指定字段内容,使用_source表示,例如只返回account_number和balance两个字段内容

        GET /bank/_search
        {
            "query": { "match_all": {} },
            "_source": ["account_number", "balance"]
        }

六、条件搜索

1:条件搜索,使用match表示匹配条件,例如搜索出account_number为20的文档

        GET /bank/_search
        {
          "query": {
            "match": {
              "account_number": 20
            }
          }
        }

2:文本类型字段的条件搜索,对于数值类型match操作使用的是精确匹配,对于文本类型使用的是模糊匹配

        GET /bank/_search
        {
          "query": {
            "match": {
              "address": "mill"
            }
          },
          "_source": [
            "address",
            "account_number"
          ]
        }

3:短语匹配搜索,使用match_phrase表示,例如搜索address字段中同时包含mill和lane的文档

        GET /bank/_search
        {
          "query": {
            "match_phrase": {
              "address": "mill lane"
            }
          }
        }

七、组合搜索

1:组合搜索,使用bool来进行组合,must表示同时满足,例如搜索address字段中同时包含mill和lane的文档

          GET /bank/_search
          {
            "query": {
              "bool": {
                "must": [
                  { "match": { "address": "mill" } },
                  { "match": { "address": "lane" } }
                ]
              }
            }
          }

2:组合搜索,should表示满足其中任意一个,搜索address字段中包含mill或者lane的文档

          GET /bank/_search
          {
            "query": {
              "bool": {
                "should": [
                  { "match": { "address": "mill" } },
                  { "match": { "address": "lane" } }
                ]
              }
            }
          }

3:组合搜索,must_not表示同时不满足,例如搜索address字段中不包含mill且不包含lane的文档

        GET /bank/_search
        {
          "query": {
            "bool": {
              "must_not": [
                { "match": { "address": "mill" } },
                { "match": { "address": "lane" } }
              ]
            }
          }
        }

4: 组合搜索,组合must和must_not,例如搜索age字段等于40且state字段不包含ID的文档

        GET /bank/_search
        {
          "query": {
            "bool": {
              "must": [
                { "match": { "age": "40" } }
              ],
              "must_not": [
                { "match": { "state": "ID" } }
              ]
            }
          }
        }

八、过滤搜索

1:搜索过滤,使用filter来表示,例如过滤出balance字段在20000~30000的文档

              GET /bank/_search
              {
                "query": {
                  "bool": {
                    "must": { "match_all": {} },
                    "filter": {
                      "range": {
                        "balance": {
                          "gte": 20000,
                          "lte": 30000
                        }
                      }
                    }
                  }
                }
              }

九、搜索聚合

1:对搜索结果进行聚合,使用aggs来表示,类似于MySql中的group by,例如对state字段进行聚合,统计出相同state的文档数量,terms相当count(1)

            GET /bank/_search
            {
              "size": 0,
              "aggs": {
                "group_by_state": {
                  "terms": {
                    "field": "state.keyword"
                  }
                }
              }
            }

2:嵌套聚合,按照年龄聚会,并且请求这些年龄段的这些人的平均薪资

            GET /bank/_search
              {
                "query": {
                  "match_all": {}
                },
                "aggs": {
                  "avgAge": {
                    "terms": {
                      "field": "age",
                      "size": 100
                    },
                    "aggs": {
                      "avgBalance": {
                        "avg": {
                          "field": "balance"
                        }
                      }
                    }
                  }
                },
                "size": 0
               }

3: 嵌套聚合:查出所有年龄分布,并且这些年龄段中M的平均薪资和F的平均薪资以及这个年龄段的总体平均薪资

              GET /bank/_search
              {
                "query": {
                    "match_all": {}
                },
                "aggs": {
                  "age_agg": {
                      "terms": {
                        "field": "age",
                        "size": 100
                      },
                      "aggs": {
                        "gender_agg": {
                           "terms": {
                             "field": "gender.keyword",
                             "size": 100
                           },
                           "aggs": {
                             "balance_avg": {
                               "avg": {
                                 "field": "balance"
                               }
                             }
                           }
                        },
                        "balance_avg":{
                          "avg": {
                            "field": "balance"
                          }
                        }
                      }
                   }
                },
                "size": 0
              }

4:对聚合搜索的结果进行排序,例如按balance的平均值降序排列

                GET /bank/_search
                {
                  "size": 0,
                  "aggs": {
                    "group_by_state": {
                      "terms": {
                        "field": "state.keyword",
                        "order": {
                          "average_balance": "desc"
                        }
                      },
                      "aggs": {
                        "average_balance": {
                          "avg": {
                            "field": "balance"
                          }
                        }
                      }
                    }
                  }
                }

5:按字段值的范围进行分段聚合,例如分段范围为age字段的[20,30] [30,40] [40,50],之后按gender统计文档个数和balance的平均值

                GET /bank/_search
                  {
                    "size": 0,
                    "aggs": {
                      "group_by_age": {
                        "range": {
                          "field": "age",
                          "ranges": [
                            {
                              "from": 20,
                              "to": 30
                            },
                            {
                              "from": 30,
                              "to": 40
                            },
                            {
                              "from": 40,
                              "to": 50
                            }
                          ]
                        },
                        "aggs": {
                          "group_by_gender": {
                            "terms": {
                              "field": "gender.keyword"
                            },
                            "aggs": {
                              "average_balance": {
                                "avg": {
                                  "field": "balance"
                                }
                              }
                            }
                          }
                        }
                      }
                    }

十、数据迁移

1:source指原索引,dest指新索引

          POST _reindex
          {
            "source": {
              "index": "bank",
              "type": "account"
            },
            "dest": {
              "index": "new_bank"
            }
          }
posted @ 2021-10-28 17:27  jock_javaEE  阅读(68)  评论(0)    收藏  举报