天天看点

二、ElasticSearch相关查询及问题解决

1、相关查询语句

1.检测集群是否健康

集群状态

[[email protected]-01 ~]#  curl -XGET -u elastic:123456  'localhost:9200/_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
1592817451 17:17:31  cluster-es   green           2         2     60  30    0    0        0             0                  -                100.0%
           

当前es集群状态为green

注:有的会由于加载得索引数据比较多索引显示为yellow,正常情况下,集群得健康状态分为三种:

green 最健康得状态,说明所有的分片包括备份都可用

yellow 基本的分片可用,但是备份不可用(或者是没有备份)

red 部分的分片可用,表明分片有一部分损坏。此时执行查询部分数据仍然可以查到,遇到这种情况,还是赶快解决比较好

从上边查看的集群状态中还包括一些其他信息:

集群名字时elasticsearch,集群的节点个数为2,活动分片百分比为100%

2.查看分片状态

[[email protected]-01 ~]# curl -X GET -u elastic:123456  'http://localhost:9200/_cluster/health?pretty'
{
  "cluster_name" : "cluster-es",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "active_primary_shards" : 30,
  "active_shards" : 60,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}
           

3.查看许可证使用期限

[[email protected]-01 ~]# curl -XGET -u elastic:123456  'http://localhost:9200/_license' 
{
  "license" : {
    "status" : "active",
    "uid" : "5227c16c-b7c8-4cd0-ad80-ab3d6200bca7",
    "type" : "trial",
    "issue_date" : "2020-06-22T08:18:57.356Z",
    "issue_date_in_millis" : 1592813937356,
    "expiry_date" : "2020-07-22T08:18:57.356Z",
    "expiry_date_in_millis" : 1595405937356,
    "max_nodes" : 1000,
    "issued_to" : "es_cluster",
    "issuer" : "elasticsearch",
    "start_date_in_millis" : -1
  }
}
           

经结果可知,x-pack插件许可证试用期一个月到期,将不可使用;

4.查看版本号(这里的版本:5.2.2)

[[email protected]-01 ~]# curl -XGET -u elastic:123456  'localhost:9200'
{
  "name" : "node-01",
  "cluster_name" : "es_cluster",
  "cluster_uuid" : "4usMS_xdSyaA91AMUl734A",
  "version" : {
    "number" : "5.2.2",
    "build_hash" : "f9d9b74",
    "build_date" : "2017-02-24T17:26:45.835Z",
    "build_snapshot" : false,
    "lucene_version" : "6.4.1"
  },
  "tagline" : "You Know, for Search"
}
           

5.获取集群的节点列表

[[email protected]-01 ~]# curl -XGET -u elastic:123456 'localhost:9200/_cat/nodes?v'
ip            heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.0.164           50          96   8    0.40    0.41     0.41 mdi       *      192.168.0.164
192.168.0.165           27          96  23    0.55    0.54     0.57 di        -      192.168.0.165
           

6.列出所有索引

[[email protected]-01 ~]# curl -XGET -u elastic:123456  'localhost:9200/_cat/indices?v'
health status index                       uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .security                   qsSmPcwOTQ6SLnYDsj-Pkw   1   0          1            0      2.8kb          2.8kb
green  open   mytest                      TXHPYUPHTpuj3Q0Ww8Lj-g   5   0          1            0      5.4kb          5.4kb
green  open   .monitoring-es-2-2020.06.22 EzdBgDbqTuChat-dFk5kig   1   0       4896           56      3.3mb          3.3mb
green  open   .monitoring-data-2          RNfUinAYQICA9I5KNkJ2HQ   1   0          2            0       37kb           37kb
           

7.创建新索引

[[email protected]-01 ~]# curl -XPUT -u elastic:123456  'localhost:9200/customer?pretty'
{
  "acknowledged" : true,
  "shards_acknowledged" : true
}
           

8.插入和获取数据

现在我么插入一些数据到集群索引。我们必须给ES指定所以的类型。如下语句:“external” type, ID:1:

主体为JSON格式的语句: { “name”: “Bertram” }

[[email protected]-01 ~]# curl -XPUT -u elastic:123456 'localhost:9200/customer/external/1?pretty' -d '{  "name":"Bertram" }'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : true
}
           

返回结果为:create:true 表示插入成功。

获取GET,语句如下:

[[email protected]-01 ~]# curl -XGET -u elastic:123456 'localhost:9200/customer/external/1?pretty'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "name" : "Bertram"
  }
}
           

其中含义为:获取customer索引下类型为external,id为1的数据,pretty参数表示返回结果格式美观。

9.删除索引 DELETE

[[email protected]-01 ~]# curl -XDELETE -u elastic:123456 'localhost:9200/customer?pretty'
{
  "acknowledged" : true
}
           

10.修改数据

[[email protected]-01 ~]# curl -XPUT -u elastic:123456 'localhost:9200/customer/external/1?pretty' -d '{  "name":"Bertram修改" }'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created" : false
}
           

11.更新数据

[[email protected]-01 ~]# curl -XPOST -u elastic:123456 'localhost:9200/customer/external/1/_update?pretty' -d '{"doc": {"name": "Bertram修改", "age": 20 }}'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}
           

12.根据关键词搜索数据

[[email protected]-01 ~]# curl -u elastic:123456 'localhost:9200/customer/_search?q=result:updated&pretty'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}
           

13.查询某个索引中的所有数据

[[email protected]-01 ~]# curl -u elastic:123456 'localhost:9200/customer/_search?q=*&pretty'
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "customer",
        "_type" : "external",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "Bertram修改",
          "age" : 20
        }
      }
    ]
  }
}
等价于
[[email protected]-01 ~]# curl -XPOST  -u elastic:123456 http://localhost:9200/customer/_search?pretty -d '{"query":{ "match_all":{}}}'
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "customer",
        "_type" : "external",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "Bertram修改"
        }
      },
      {
        "_index" : "customer",
        "_type" : "external",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "chj"
        }
      }
    ]
  }
}
           

14.删除数据

[[email protected]-01 ~]# curl -XDELETE -u elastic:123456 'localhost:9200/customer/external/1?pretty'
{
  "found" : true,
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 5,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  }
}
           

注:customer索引名,external mappings类型,1为id

15.清空某索引下的全部数据

curl:清理如下

注:customer索引名,external mappings类型

web界面清理:

POST customer/external/_delete_by_query
{
  "query": {"match_all": {}}
}
           

16.number_of_shards

number_of_shards

只能在创建 index 指定,后期无法修改

创建 testindex

curl -XPUT 'http://192.168.1.128:9200/testindex' -H 'Content-Type: application/json' -d ' 
{
    "settings": {
     "number_of_shards": 5,
	 "number_of_replicas" : 2
    }
}'
           

查看 testindex 索引

curl -XGET 'http://192.168.1.128:9200/testindex/_settings?pretty'
{
  "testindex" : {
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "5",
        "provided_name" : "testindex",
        "creation_date" : "1629957135326",
        "number_of_replicas" : "2",
        "uuid" : "JGdxP7HcRkCEGDtVFmdMrA",
        "version" : {
          "created" : "7130299"
        }
      }
    }
  }
}
           

17.number_of_replicas

number_of_replicas

可以在线修改

curl -XPUT 'http://192.168.1.128:9200/testindex/_settings' -H 'Content-Type: application/json' -d ' 
{
    "index" : {
        "number_of_replicas" : 0
    }
}'
           

遇到问题解决方法:

1.查看分片,集群状态为yellow

[[email protected] elasticsearch-head]# curl -X GET -u elastic:123456  'http://localhost:9200/_cluster/health?pretty'
{
  "cluster_name" : "es_cluster",
  "status" : "yellow",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 8,
  "active_shards" : 8,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 7,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 53.333333333333336
}
           

由于我们是单节点部署elasticsearch,而默认的分片副本数目配置为1,而相同的分片不能在一个节点上,所以就存在副本分片指定不明确的问题,所以显示为yellow,我们可以通过在elasticsearch集群上添加一个节点来解决问题,如果你不想这么做,你可以删除那些指定不明确的副本分片(当然这不是一个好办法)但是作为测试和解决办法还是可以尝试的,下面我们试一下删除副本分片的办法

解决办法

这个时候再次查看集群的状态状态变成了green

[[email protected] elasticsearch-head]# curl -X GET -u elastic:123456  'http://localhost:9200/_cluster/health?pretty'
{
  "cluster_name" : "es_cluster",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 8,
  "active_shards" : 8,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}
           

参考:文章

继续阅读