Elastic Search常用命令

1 测试环境信息

Elastic Search
服务器:192.168.0.100
用户:docker

启停:
docker start/stop/restart elasticsearch

Kibana控制台:
http://192.168.0.100:5601/app/kibana#/dev_tools/console

2 基本概念

Elasticsearch也是基于Lucene的全文检索库,本质也是存储数据,很多概念与MySQL类似的。

概念 英文 描述
索引库 indices indices是index的复数,类似于Databases
文档 Document 存入索引库原始的数据。比如每一条记录,就是一个文档。类似于Row字段(Field)文档中的属性,类似于Column。
映射配置 mappings 字段的数据类型、属性、是否索引、是否存储等特性。

3 索引(index)

3.1 创建索引

索引名只能是小写,number_of_shards表示分片数,number_of_replicas表示副本数。

PUT index_name
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  }
}

结果

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "index_name"
}

3.2 查询索引是否存在

HEAD index_name

结果

200 - OK

3.3 查看索引

GET index_name

结果

{
  "index_name" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1585209049905",
        "number_of_shards" : "3",
        "number_of_replicas" : "2",
        "uuid" : "mYpHG1HkSuy2gWKXoowHug",
        "version" : {
          "created" : "7060099"
        },
        "provided_name" : "index_name"
      }
    }
  }
}

3.4 删除索引

DELETE index_name

结果

{
  "acknowledged" : true
}

4 映射(mappings)

属性名 属性 描述
类型 type text、long、short、date、integer、object等
是否索引 index 默认true
是否存储 store 默认false
分词器 analyzer standard、whitespace 空格为分隔符、simple、stop、keyword等,参考https://www.cnblogs.com/xiaobaozi-95/p/9328948.html

4.1 创建映射

  • 创建
PUT index_name/_mapping
{
  "properties": {
    "name": {
      "type": "text",
      "analyzer": "whitespace",
      "store": false
    },
    "weight": {
      "type": "integer",
      "index": "true"
    },
    "code": {
      "type": "keyword"
    }
  }
}

结果

{
  "acknowledged" : true
}
  • 多次创建,如果能属性不冲突,则合并
PUT index_name/_mapping
{
  "properties": {
    "code": {
      "type": "keyword"
    },
    "sex": {
      "type": "text"
    }
  }
}

结果

{
  "acknowledged" : true
}

4.2 查询映射

GET index_name/_mapping

结果

{
  "index_name" : {
    "mappings" : {
      "properties" : {
        "code" : {
          "type" : "keyword"
        },
        "name" : {
          "type" : "text",
          "analyzer" : "whitespace"
        },
        "sex" : {
          "type" : "text"
        },
        "weight" : {
          "type" : "integer"
        }
      }
    }
  }
}

5 文档

5.1 新增文档

  • 指定id为id1新增文档
POST index_name/_doc/id1
{
  "name" : "name1",
  "weight" : 1,
  "code": "code1"
}

结果

{
  "_index" : "index_name",
  "_type" : "_doc",
  "_id" : "id1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 3,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
  • 不指定id,新增文档,会自动生成id
POST index_name/_doc
{
  "name" : "name2",
  "weight" : 2,
  "code": "code2"
}

结果

{
  "_index" : "index_name",
  "_type" : "_doc",
  "_id" : "JZ_VFXEBZtdZ_6fzGLrJ",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 3,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

5.2 修改文档

有相同id则会更新

POST index_name/_doc/id1
{
  "name" : "name3",
  "weight" : 3,
  "code": "code3"
}

结果

{
  "_index" : "index_name",
  "_type" : "_doc",
  "_id" : "id1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 3,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

5.3 读取文档

GET index_name/_doc/id1

结果

{
  "_index" : "index_name",
  "_type" : "_doc",
  "_id" : "id1",
  "_version" : 2,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "name3",
    "weight" : 3,
    "code" : "code3"
  }
}

5.4 删除文档

DELETE index_name/_doc/id1

结果

{
  "_index" : "index_name",
  "_type" : "_doc",
  "_id" : "id1",
  "_version" : 3,
  "result" : "deleted",
  "_shards" : {
    "total" : 3,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

6 查询

  • 【准备数据】
DELETE index_name/_doc/JZ_VFXEBZtdZ_6fzGLrJ
POST index_name/_doc/id1
{
  "name" : "name1",
  "weight" : 1,
  "code": "code1"
}
POST index_name/_doc/id2
{
  "name" : "name2",
  "weight" : 2,
  "code": "code2"
}
POST index_name/_doc/id3
{
  "name" : "name1 name2",
  "weight" : 3,
  "code": "code1 code2"
}

6.1 查询总数

GET index_name/_count

结果

{
  "count" : 3,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  }
}

6.2 查询所有

GET index_name/_search

或者

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

结果

{
  "took" : 959,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id1",
        "_score" : 1.0,
        "_source" : {
          "name" : "name1",
          "weight" : 1,
          "code" : "code1"
        }
      },
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id2",
        "_score" : 1.0,
        "_source" : {
          "name" : "name2",
          "weight" : 2,
          "code" : "code2"
        }
      },
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id3",
        "_score" : 1.0,
        "_source" : {
          "name" : "name1 name2",
          "weight" : 3,
          "code" : "code1 code2"
        }
      }
    ]
  }
}

6.3 条件查询

  • 查询name为小明的记录,默认为分词or查询,会查询出name1、name2和“name1 name2”的记录。
GET index_name/_search
{
  "query": {
    "match": {
      "name": "name1 name2"
    }
  }
}

结果

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.2039728,
    "hits" : [
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id2",
        "_score" : 1.2039728,
        "_source" : {
          "name" : "name2",
          "weight" : 2,
          "code" : "code2"
        }
      },
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id1",
        "_score" : 0.6931471,
        "_source" : {
          "name" : "name1",
          "weight" : 1,
          "code" : "code1"
        }
      },
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id3",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "name1 name2",
          "weight" : 3,
          "code" : "code1 code2"
        }
      }
    ]
  }
}
  • 修改为and查询,只会查出“name1 name2”的记录
GET index_name/_search
{
  "query": {
    "match": {
      "name":{
        "query": "name1 name2",
        "operator": "and"
      }
    }
  }
}

结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id3",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "name1 name2",
          "weight" : 3,
          "code" : "code1 code2"
        }
      }
    ]
  }
}

6.4 设置匹配度查询

GET index_name/_search
{
  "query": {
    "match": {
      "name":{
        "query": "name1 name2",
        "minimum_should_match": "50%"
      }
    }
  }
}

结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.2039728,
    "hits" : [
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id2",
        "_score" : 1.2039728,
        "_source" : {
          "name" : "name2",
          "weight" : 2,
          "code" : "code2"
        }
      },
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id1",
        "_score" : 0.6931471,
        "_source" : {
          "name" : "name1",
          "weight" : 1,
          "code" : "code1"
        }
      },
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id3",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "name1 name2",
          "weight" : 3,
          "code" : "code1 code2"
        }
      }
    ]
  }
}

6.5 精确查询

GET index_name/_search
{
    "query": {
        "terms": {
            "code": [
                "code1",
                "code2"
            ]
        }
    }
}

结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id1",
        "_score" : 1.0,
        "_source" : {
          "name" : "name1",
          "weight" : 1,
          "code" : "code1"
        }
      },
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id2",
        "_score" : 1.0,
        "_source" : {
          "name" : "name2",
          "weight" : 2,
          "code" : "code2"
        }
      }
    ]
  }
}

6.6 范围查询

GET index_name/_search
{
  "query":{
    "range": {
      "weight": {
        "gte": 0.9,
        "lte": 1.5
      }
    }
  }
}

结果

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id1",
        "_score" : 1.0,
        "_source" : {
          "name" : "name1",
          "weight" : 1,
          "code" : "code1"
        }
      }
    ]
  }
}

6.7 多字段查询

查询name或者code中包含name1值的记录

GET index_name/_search
{
    "query": {
        "multi_match": {
            "query": "name1",
            "fields": [
                "name",
                "code"
            ]
        }
    }
}

结果

{
  "took" : 18,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id1",
        "_score" : 0.6931471,
        "_source" : {
          "name" : "name1",
          "weight" : 1,
          "code" : "code1"
        }
      },
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id3",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "name1 name2",
          "weight" : 3,
          "code" : "code1 code2"
        }
      }
    ]
  }
}

6.8 多条件的复合查询

备注:bool里面的都是一些 条件,must必须瞒足,should只要满足minimum_should_match个条件是ture。

GET index_name/_search
{
  "query":{
    "bool": {
      "must": [{ 
        "match": {
          "name": "name1"
        }
      }],
      "should": [
        {
          "match": {
            "code": "code1"
          }
        },
        {
          "match": {
            "weight": "2"
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

结果

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.3862942,
    "hits" : [
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id1",
        "_score" : 1.3862942,
        "_source" : {
          "name" : "name1",
          "weight" : 1,
          "code" : "code1"
        }
      }
    ]
  }
}

6.9 结果展示列设置

  • 只显示name数据
GET index_name/_search
{
    "query": {
        "match": {
            "name":{
                "query": "name1 name2",
                "operator": "and"
            }
        }
    },
    "_source": "name"
}

结果

{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id3",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "name1 name2"
        }
      }
    ]
  }
}
  • 不显示weight
GET index_name/_search
{
    "query": {
        "match": {
            "name":{
                "query": "name1 name2",
                "operator": "and"
            }
        }
    },
    "_source": {
        "excludes": "weight"
    }
}

结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id3",
        "_score" : 0.5753642,
        "_source" : {
          "code" : "code1 code2",
          "name" : "name1 name2"
        }
      }
    ]
  }
}
  • 只显示name和code
GET index_name/_search
{
    "query": {
        "match": {
            "name":{
                "query": "name1 name2",
                "operator": "and"
            }
        }
    },
    "_source": {
        "includes": [
            "name",
            "code"
        ]
    }
}

结果

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id3",
        "_score" : 0.5753642,
        "_source" : {
          "code" : "code1 code2",
          "name" : "name1 name2"
        }
      }
    ]
  }
}

6.10 查询分页

备注:分页问题,效率会很低,尽量避免深分页。

分页:如果要查询出每页100条,第100页数据数据(9900-10000),如果是去5个节点查询,那么会在每个节点查询出第9900-10000条数据,然后做汇总,最后排序取出9900-10000条,这样做非常占资源。

GET index_name/_search
{
    "from":1,
    "size":2
}

结果

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id2",
        "_score" : 1.0,
        "_source" : {
          "name" : "name2",
          "weight" : 2,
          "code" : "code2"
        }
      },
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id3",
        "_score" : 1.0,
        "_source" : {
          "name" : "name1 name2",
          "weight" : 3,
          "code" : "code1 code2"
        }
      }
    ]
  }
}

6.11 查询最新记录【排序】

POST index_name/_search?pretty
{
  "sort":{
    "weight":{
      "order":"desc"
    }
  },
  "size": 1
}

结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id3",
        "_score" : null,
        "_source" : {
          "name" : "name1 name2",
          "weight" : 3,
          "code" : "code1 code2"
        },
        "sort" : [
          3
        ]
      }
    ]
  }
}

6.12 求平均值和总数量

GET  index_name/_search
{
  "aggs": {
    "total_count": {
      "value_count": {
        "field": "weight"
      }
    },
    "pjz":{
      "avg": {
        "field": "weight"
      }
    }
  }
}

结果

{
  "took" : 78,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id1",
        "_score" : 1.0,
        "_source" : {
          "name" : "name1",
          "weight" : 1,
          "code" : "code1"
        }
      },
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id2",
        "_score" : 1.0,
        "_source" : {
          "name" : "name2",
          "weight" : 2,
          "code" : "code2"
        }
      },
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id3",
        "_score" : 1.0,
        "_source" : {
          "name" : "name1 name2",
          "weight" : 3,
          "code" : "code1 code2"
        }
      }
    ]
  },
  "aggregations" : {
    "pjz" : {
      "value" : 2.0
    },
    "total_count" : {
      "value" : 3
    }
  }
}

6.13 分组

GET  index_name/_search
{
  "aggs": {
    "fz": {
      "terms": {
        "field": "weight"
      }
    }
  }
}

结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id1",
        "_score" : 1.0,
        "_source" : {
          "name" : "name1",
          "weight" : 1,
          "code" : "code1"
        }
      },
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id2",
        "_score" : 1.0,
        "_source" : {
          "name" : "name2",
          "weight" : 2,
          "code" : "code2"
        }
      },
      {
        "_index" : "index_name",
        "_type" : "_doc",
        "_id" : "id3",
        "_score" : 1.0,
        "_source" : {
          "name" : "name1 name2",
          "weight" : 3,
          "code" : "code1 code2"
        }
      }
    ]
  },
  "aggregations" : {
    "fz" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 1,
          "doc_count" : 1
        },
        {
          "key" : 2,
          "doc_count" : 1
        },
        {
          "key" : 3,
          "doc_count" : 1
        }
      ]
    }
  }
}

6.14 scoll游标查询【验证失败。。。】

scoll游标查询,指定scroll=时间,指定保存的分钟数,第一次发起请求放回的不是数据,而是_scroll_id,后面通过_scroll_id去请求数据,非常适合大批量查询。

备注:游标查询是在es里面缓存了结果,然后一次一次的去取,所以发起第一次请求的时候只有size,没有from,后面的请求只有scroll_id和scroll时间。

保持游标查询窗口1分钟,关键字为_doc

GET index_name/_search?scroll=1m
{
  "sort" : ["_doc"],
  "size":2
}
GET _search/scroll
{
  "scroll": "1m",
  "scroll_id" : "XXX"
}

7 其他命令

7.1 查询所有索引

GET /_cat/indices

结果【GET /_cat/indices?help】

health status name uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open index_name lWIDNTFgRVW7duDWd_XqRg 3 2 4 0 24.6kb 24.6kb
yellow open index_name1 yy62UUwjTo-cVVaKB7N_AA 1 1 166 0 204.3kb 204.3kb
yellow open index_name2 7cHS493OT_qXeBS6QbKVow 1 1 29 0 115.2kb 115.2kb
yellow open index_name3 pzz2E75SRBqyVtDH14ZFRA 1 1 31 0 92.7kb 92.7kb
yellow open index_name4 dWmzRZEdT9uo4vqj3NkVag 1 1 147 0 257.8kb 257.8kb
yellow open index_name5 LrOutVUcQoiqDrMYlLNluQ 1 1 1236 0 289.9kb 289.9kb
yellow open index_name6 tPu4hjQlRJu1dV5ufrIcYg 1 1 591 0 379.7kb 379.7kb

7.2 查看节点健康

GET /_cat/health?v

结果

epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1585207426 07:23:46 docker-cluster yellow 1 1 34 34 0 0 30 0 - 53.1%

7.3 查看索引设置

GET index_name/_settings

结果

{
  "index_name" : {
    "settings" : {
      "index" : {
        "creation_date" : "1585209049905",
        "number_of_shards" : "3",
        "number_of_replicas" : "2",
        "uuid" : "mYpHG1HkSuy2gWKXoowHug",
        "version" : {
          "created" : "7060099"
        },
        "provided_name" : "index_name"
      }
    }
  }
}

你可能感兴趣的:(ES,es)