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类似的。

概念英文描述
索引库indicesindices是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)

属性名属性描述
类型typetext、long、short、date、integer、object等
是否索引index默认true
是否存储store默认false
分词器analyzerstandard、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】

healthstatusnameuuidprirepdocs.countdocs.deletedstore.sizepri.store.size
yellowopenindex_namelWIDNTFgRVW7duDWd_XqRg324024.6kb24.6kb
yellowopenindex_name1yy62UUwjTo-cVVaKB7N_AA111660204.3kb204.3kb
yellowopenindex_name27cHS493OT_qXeBS6QbKVow11290115.2kb115.2kb
yellowopenindex_name3pzz2E75SRBqyVtDH14ZFRA1131092.7kb92.7kb
yellowopenindex_name4dWmzRZEdT9uo4vqj3NkVag111470257.8kb257.8kb
yellowopenindex_name5LrOutVUcQoiqDrMYlLNluQ1112360289.9kb289.9kb
yellowopenindex_name6tPu4hjQlRJu1dV5ufrIcYg115910379.7kb379.7kb

7.2 查看节点健康

GET /_cat/health?v

结果

epochtimestampclusterstatusnode.totalnode.datashardsprireloinitunassignpending_tasksmax_task_wait_timeactive_shards_percent
158520742607:23:46docker-clusteryellow11343400300-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"
      }
    }
  }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖毁青春,瘦解百病

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值