当前位置:   article > 正文

Elastic Search常用命令_elasticsearch 命令

elasticsearch 命令

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
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

结果

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "index_name"
}
  • 1
  • 2
  • 3
  • 4
  • 5

3.2 查询索引是否存在

HEAD index_name
  • 1

结果

200 - OK
  • 1

3.3 查看索引

GET index_name
  • 1

结果

{
  "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"
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

3.4 删除索引

DELETE index_name
  • 1

结果

{
  "acknowledged" : true
}
  • 1
  • 2
  • 3

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"
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

结果

{
  "acknowledged" : true
}
  • 1
  • 2
  • 3
  • 多次创建,如果能属性不冲突,则合并
PUT index_name/_mapping
{
  "properties": {
    "code": {
      "type": "keyword"
    },
    "sex": {
      "type": "text"
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

结果

{
  "acknowledged" : true
}
  • 1
  • 2
  • 3

4.2 查询映射

GET index_name/_mapping
  • 1

结果

{
  "index_name" : {
    "mappings" : {
      "properties" : {
        "code" : {
          "type" : "keyword"
        },
        "name" : {
          "type" : "text",
          "analyzer" : "whitespace"
        },
        "sex" : {
          "type" : "text"
        },
        "weight" : {
          "type" : "integer"
        }
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

5 文档

5.1 新增文档

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

结果

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

结果

{
  "_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
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

5.2 修改文档

有相同id则会更新

POST index_name/_doc/id1
{
  "name" : "name3",
  "weight" : 3,
  "code": "code3"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

结果

{
  "_index" : "index_name",
  "_type" : "_doc",
  "_id" : "id1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 3,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

5.3 读取文档

GET index_name/_doc/id1
  • 1

结果

{
  "_index" : "index_name",
  "_type" : "_doc",
  "_id" : "id1",
  "_version" : 2,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "name3",
    "weight" : 3,
    "code" : "code3"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

5.4 删除文档

DELETE index_name/_doc/id1
  • 1

结果

{
  "_index" : "index_name",
  "_type" : "_doc",
  "_id" : "id1",
  "_version" : 3,
  "result" : "deleted",
  "_shards" : {
    "total" : 3,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

6 查询

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

6.1 查询总数

GET index_name/_count
  • 1

结果

{
  "count" : 3,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

6.2 查询所有

GET index_name/_search
  • 1

或者

GET index_name/_search
{
  "query":{
    "match_all": {}
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

结果

{
  "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"
        }
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

6.3 条件查询

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

结果

{
  "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"
        }
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 修改为and查询,只会查出“name1 name2”的记录
GET index_name/_search
{
  "query": {
    "match": {
      "name":{
        "query": "name1 name2",
        "operator": "and"
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

结果

{
  "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"
        }
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

6.4 设置匹配度查询

GET index_name/_search
{
  "query": {
    "match": {
      "name":{
        "query": "name1 name2",
        "minimum_should_match": "50%"
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

结果

{
  "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"
        }
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

6.5 精确查询

GET index_name/_search
{
    "query": {
        "terms": {
            "code": [
                "code1",
                "code2"
            ]
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

结果

{
  "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"
        }
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

6.6 范围查询

GET index_name/_search
{
  "query":{
    "range": {
      "weight": {
        "gte": 0.9,
        "lte": 1.5
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

结果

{
  "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"
        }
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

6.7 多字段查询

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

GET index_name/_search
{
    "query": {
        "multi_match": {
            "query": "name1",
            "fields": [
                "name",
                "code"
            ]
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

结果

{
  "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"
        }
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

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
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

结果

{
  "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"
        }
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

6.9 结果展示列设置

  • 只显示name数据
GET index_name/_search
{
    "query": {
        "match": {
            "name":{
                "query": "name1 name2",
                "operator": "and"
            }
        }
    },
    "_source": "name"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

结果

{
  "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"
        }
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 不显示weight
GET index_name/_search
{
    "query": {
        "match": {
            "name":{
                "query": "name1 name2",
                "operator": "and"
            }
        }
    },
    "_source": {
        "excludes": "weight"
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

结果

{
  "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"
        }
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 只显示name和code
GET index_name/_search
{
    "query": {
        "match": {
            "name":{
                "query": "name1 name2",
                "operator": "and"
            }
        }
    },
    "_source": {
        "includes": [
            "name",
            "code"
        ]
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

结果

{
  "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"
        }
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

6.10 查询分页

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

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

GET index_name/_search
{
    "from":1,
    "size":2
}
  • 1
  • 2
  • 3
  • 4
  • 5

结果

{
  "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"
        }
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

6.11 查询最新记录【排序】

POST index_name/_search?pretty
{
  "sort":{
    "weight":{
      "order":"desc"
    }
  },
  "size": 1
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

结果

{
  "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
        ]
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

6.12 求平均值和总数量

GET  index_name/_search
{
  "aggs": {
    "total_count": {
      "value_count": {
        "field": "weight"
      }
    },
    "pjz":{
      "avg": {
        "field": "weight"
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

结果

{
  "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
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

6.13 分组

GET  index_name/_search
{
  "aggs": {
    "fz": {
      "terms": {
        "field": "weight"
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

结果

{
  "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
        }
      ]
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

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
}
  • 1
  • 2
  • 3
  • 4
  • 5
GET _search/scroll
{
  "scroll": "1m",
  "scroll_id" : "XXX"
}
  • 1
  • 2
  • 3
  • 4
  • 5

7 其他命令

7.1 查询所有索引

GET /_cat/indices
  • 1

结果【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
  • 1

结果

epochtimestampclusterstatusnode.totalnode.datashardsprireloinitunassignpending_tasksmax_task_wait_timeactive_shards_percent
158520742607:23:46docker-clusteryellow11343400300-53.1%

7.3 查看索引设置

GET index_name/_settings
  • 1

结果

{
  "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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/349246
推荐阅读
相关标签
  

闽ICP备14008679号