Elasticsearch入门 - Mac上Elasticsearch和Kibana的安装运行与简单使用

news2024/10/6 5:59:27

文章目录

  • 一,Mac上Elasticsearch和Kibana的安装
    • 1.1 环境与下载
    • 1.2 安装与运行
    • 1.3 问题
      • 1.3.1 elasticsearch安装后其他机器不能访问
      • 1.3.2 kibana安装后其他机器不能访问
  • 二,Elasticsearch在Kibana的常见命令
    • 2.1 查看集群的健康状态
    • 2.2 索引
      • 2.2.1 查看所有索引
      • 2.2.2 新增索引
      • 2.2.3 查看单个索引
      • 2.2.4 删除单个索引
    • 2.3 查看节点列表
    • 2.4 文档的增删查改
      • 2.4.1 新增文档
      • 2.4.2 查询文档
        • 查询单条
        • 查询所有
      • 2.4.3 修改文档
        • PUT
        • POST
      • 2.4.4 删除文档
    • 2.5 查询
      • 2.5.1 单条/全表查询
      • 2.5.2 分词查询
      • 2.5.3 子属性分词查询
      • 2.5.4 短句查询
      • 2.5.5 模糊查询
      • 2.5.6 排序
      • 2.5.7 分页查询
      • 2.5.8 指定字段查询
      • 2.5.9 多条件查询
      • 2.5.10 高亮显示
    • 2.6 聚合分析
      • 2.6.1 简单分组
      • 2.6.2 分组统计
      • 2.6.3 区间分组
    • 2.7 Mapping
      • 2.7.1 查看所有类型type的mapping
      • 2.7.2 查看单个类型type的mapping
      • 2.7.3 修改mapping

一,Mac上Elasticsearch和Kibana的安装

Elasticsearch是一个基于Apache Lucene的搜索服务器,适用于所有类型的数据,包括文本、数字、地理空间、结构化和非结构化数据,是是ELK的一个组成部分(ELK代表的是:E就是ElasticSearch,L就是Logstach,K就是kibana)。

它提供了分布式可扩展的实时搜索和分析引擎,它以其简单的 REST 风格 API、分布式特性、速度和可扩展性而闻名,是一个非常强大的搜索引擎全文检索。

Elasticsearch 是由Elastic公司创建并开源维护的,该 公司也拥有 Logstash 及 Kibana 开源项目。

三个开源项目共同形成了一个强大的生态圈。简单地说,Logstash 负责数据的采集,处理(丰富数据,数据转型等),Kibana 负责数据展示,分析及管理。Elasticsearch 处于最核心的位置,它可以帮我们对数据进行快速地搜索及分析。

1.1 环境与下载

在安装之前,提前了解本地PC的java版本,因为java版本和的elasticsearch,kibana的对应关系是有严格要求的

我本地Mac使用的是:

java version “1.8.0_121”

elasticsearch-6.8.2 下载地址:https://www.elastic.co/cn/downloads/elasticsearch

kibana-6.8.23 下载地址:https://www.elastic.co/cn/downloads/kibana

1.2 安装与运行

下载完成之后,在Mac上找个目录解压以上两个压缩包

然后进入各自的bin目录下

elasticsearch的启动命令:

./elasticsearch

kibana的启动命令:

./kibana

输出日志运行完毕后,分别访问 http://localhost:9200(返回json格式的数据)和http://localhost:5601(返回一个页面),若两个页面都显示正常,则运行成功

注意:kibana启动花费时间较长,当执行命令后没有立即看到日志输出为正常情况

1.3 问题

1.3.1 elasticsearch安装后其他机器不能访问

在Mac上运行成功后,同一网段的Windows访问不了时,到Mac上安装目录下的config/elasticsearch.yml下,添加或修改一行

network.bind_host: 0.0.0.0

重新启动,验证

http://xx.xx.xx.xx:9200

1.3.2 kibana安装后其他机器不能访问

同上,到安装目录下的config/kibana.yml下,添加或修改两行

server.port: 5602
server.host: 0.0.0.0

重新启动,验证

http://xx.xx.xx.xx:5601

二,Elasticsearch在Kibana的常见命令

首先,在使用命令之前,需要知道以下的命令可以在哪里运行

打开kibana的首页,点击左边栏的【Dev Tools】,右边栏下面的【Console】分为左右两栏,在左边栏输入命令,然后点击三角形绿色按钮,就可以在右边栏呈现结果,如下所示:

在这里插入图片描述

2.1 查看集群的健康状态

GET _cat/health
================================ 结果 ================================
1673923769 02:49:29 elasticsearch yellow 1 1 7 7 0 0 5 0 - 58.3%

若想知道每个值的含义

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
1673923848 02:50:48  elasticsearch yellow          1         1      7   7    0    0        5             0                  -                 58.3%

常见属性解读:

  • epoch:当前时间的时间戳(默认与东八区差八个小时)
  • timestamp:当前时间
  • cluster:集群名称
  • status:集群状态,green代表健康,yellow代表当前为单机,没有副本
  • node.total:在线节点个数
  • node.data:在线数据节点个数

获取更加详细的内容

GET _all
================================ 结果 ================================
#! Deprecation: [types removal] The parameter include_type_name should be explicitly specified in get indices requests to prepare for 7.0. In 7.0 include_type_name will default to 'false', which means responses will omit the type name in mapping definitions.
{
  ".kibana_1" : {
    "aliases" : {
      ".kibana" : { }
    },
    "mappings" : {
      "doc" : {
        "dynamic" : "strict",
        "properties" : {
        ......

2.2 索引

2.2.1 查看所有索引

GET _cat/indices
================================ 结果 ================================
yellow open human_index          Mf-9YNYrSdyiLZFgZCP7ow 5 1 4 0 22.6kb 22.6kb
green  open .kibana_task_manager J9YFrgfOS1W2N3dvqXwxOg 1 0 2 0 12.5kb 12.5kb
green  open .kibana_1            hgDx6B-6QmC0KjWLWB3wgQ 1 0 5 1 26.5kb 26.5kb

若想知道每个值的含义

GET _cat/indices?v
================================ 结果 ================================
health status index                uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   human_index          Mf-9YNYrSdyiLZFgZCP7ow   5   1          4            0     22.6kb         22.6kb
green  open   .kibana_task_manager J9YFrgfOS1W2N3dvqXwxOg   1   0          2            0     12.5kb         12.5kb
green  open   .kibana_1            hgDx6B-6QmC0KjWLWB3wgQ   1   0          5            1     26.5kb         26.5kb

常见属性解读:

  • health:索引健康状态
  • status:索引启动状态
  • index:索引名称
  • uuid:索引的唯一标识
  • pri:索引主分片数
  • rep:索引副本分片数
  • docs.count:索引中文档数
  • docs.deleted:索引中删除状态的文档

2.2.2 新增索引

PUT /human_index1
================================ 结果 ================================
#! Deprecation: the default number of shards will change from [5] to [1] in 7.0.0; if you wish to continue using the default of [5] shards, you must manage this on the create index request or with an index template
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "human_index1"
}

2.2.3 查看单个索引

GET /human_index1
================================ 结果 ================================
#! Deprecation: [types removal] The parameter include_type_name should be explicitly specified in get indices requests to prepare for 7.0. In 7.0 include_type_name will default to 'false', which means responses will omit the type name in mapping definitions.
{
  "human_index1" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1673926295232",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "i9ESnW6ETN2n5C6V5PLZ8Q",
        "version" : {
          "created" : "6082399"
        },
        "provided_name" : "human_index1"
      }
    }
  }
}

2.2.4 删除单个索引

DELETE /human_index1
================================ 结果 ================================
{
  "acknowledged" : true
}

2.3 查看节点列表

GET _cat/nodes
================================ 结果 ================================
10.197.29.203 21 45 9 2.11   mdi * 2FgJQbJ

GET _cat/nodes?v
================================ 结果 ================================
ip            heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
10.197.29.203           23          45   9    1.99                  mdi       *      2FgJQbJ

常见属性解读:

  • ip:部署的ip地址
  • heap.percent:堆内存占用百分比
  • ram.percent:内存占用百分比
  • cup:CPU占用百分比
  • load_1m:1分钟的系统负载
  • node.role:节点的角色
  • master:是否为master节点
  • name:节点名称

2.4 文档的增删查改

2.4.1 新增文档

put /human_index/user/1
{
  "name": "hh",
  "desc": "my name is hh",
  "age": 25,
  "country": "China GuangDong",
  "sex": "female"
}
================================ 结果 ================================
{
  "_index" : "human_index",
  "_type" : "user",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "created": true,
  "_seq_no" : 1,
  "_primary_term" : 2
}

在以上的新增方式中,已经指定了该文档的id(1),如果不需要自定义id的话,可以使用以下方式:

POST /human_index/user
{
  "name": "id_test",
  "desc": "test no id"
}
================================ 结果 ================================
{
  "_index" : "human_index",
  "_type" : "user",
  "_id" : "MnS2woUBq_u6VYKKJjno",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 10,
  "_primary_term" : 3
}

可以看到,默认随机生成的id为MnS2woUBq_u6VYKKJjno

在创建document的时候,如果命令行的索引index(human_index)和类型type(user)不存在,默认会自动创建。

2.4.2 查询文档

查询单条

get /human_index/user/1
================================ 结果 ================================
{
  "_index" : "human_index",
  "_type" : "user",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 1,
  "_primary_term" : 2,
  "found" : true,
  "_source" : {
    "name" : "hh",
    "desc" : "my name is hh",
    "age" : 25,
    "country" : "China GuangDong",
    "sex" : "female"
  }
}

查询所有

get /human_index/user/_search
================================ 结果 ================================
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 4,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "sb",
          "desc" : "my name is sb",
          "age" : 25,
          "country" : "China GuangDong Jieyang",
          "sex" : "female"
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "doc" : {
            "name" : "lmc hh",
            "country" : "China GuangDong Jieyang",
            "sex" : "male",
            "desc" : "my name is leemon",
            "age" : 11
          }
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "hh",
          "desc" : "my name is hh",
          "age" : 25,
          "country" : "China GuangDong",
          "sex" : "female"
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "age" : 24,
          "country" : "China GuangDong Shenzhen",
          "sex" : "male",
          "name" : "ln",
          "desc" : "my name is lee nai"
        }
      }
    ]
  }
}

get /human_index/user/_search
{
  "query":{
    "match_all": {}
  }
}

由于我是把流程走过一遍了,因此存在多条记录

字段解释:

  • took:耗费时间(毫秒)
  • _shards:分片情况
  • hits:获取到的数据情况
    • total:数据总条数
    • max_score:数据里面打分最高的分数

2.4.3 修改文档

修改可以通过POST和PUT来处理,但两者有区别

  • PUT的修改是全局的修改,会丢数据
  • POST的修改是局部更新,别的数据不变;请求体文档内容要包裹在键doc内,

PUT

使用put时,如果原document已存在,则会直接替换成新的

put /human_index/user/1
{
  "sex": "female"
}
================================ 结果 ================================
{
  "_index" : "human_index",
  "_type" : "user",
  "_id" : "1",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 2
}

再继续查看:

get /human_index/user/1
================================ 结果 ================================
{
  "_index" : "human_index",
  "_type" : "user",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 2,
  "_primary_term" : 2,
  "found" : true,
  "_source" : {
    "sex" : "female"
  }
}

可以发现,除了sex字段外,其他都不见了

POST

将该文档重新还原

put /human_index/user/1
{
  "name": "hh",
  "desc": "my name is hh",
  "age": 25,
  "country": "China GuangDong",
  "sex": "female"
}

然后通过POST进行修改

post /human_index/user/1/_update
{
  "doc": {
    "sex": "male"
  }
}
================================ 结果 ================================
{
  "_index" : "human_index",
  "_type" : "user",
  "_id" : "1",
  "_version" : 7,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 6,
  "_primary_term" : 2
}

再重新查看

get /human_index/user/1
================================ 结果 ================================
{
  "_index" : "human_index",
  "_type" : "user",
  "_id" : "1",
  "_version" : 7,
  "_seq_no" : 6,
  "_primary_term" : 2,
  "found" : true,
  "_source" : {
    "name" : "hh",
    "desc" : "my name is hh",
    "age" : 25,
    "country" : "China GuangDong",
    "sex" : "male"
  }
}

这个时候,除了sex的其他属性都在存在,为局部修改

2.4.4 删除文档

DELETE /human_index/user/1
================================ 结果 ================================
{
  "_index" : "human_index",
  "_type" : "user",
  "_id" : "1",
  "_version" : 8,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 7,
  "_primary_term" : 2
}

再继续查看

get /human_index/user/1
================================ 结果 ================================
{
  "_index" : "human_index",
  "_type" : "user",
  "_id" : "1",
  "found" : false
}

已经删除成功

2.5 查询

再进行查询之前,该索引类型user下的所有记录如下所示

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "name" : "ln-1",
          "country" : "China GuangDong Jieyang",
          "sex" : "male",
          "desc" : "my name is leemon-1",
          "age" : 21
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "sb",
          "desc" : "my name is sb",
          "age" : 25,
          "country" : "China GuangDong Jieyang",
          "sex" : "female"
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "doc" : {
            "name" : "lmc hh",
            "country" : "China GuangDong Jieyang",
            "sex" : "male",
            "desc" : "my name is leemon",
            "age" : 11
          }
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "name" : "ln sb",
          "country" : "China GuangDong Jieyang",
          "sex" : "male",
          "desc" : "my name is sb leemon",
          "age" : 27
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "hh",
          "desc" : "my name is hh",
          "age" : 25,
          "country" : "China GuangDong",
          "sex" : "female"
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "age" : 24,
          "country" : "China GuangDong Shenzhen",
          "sex" : "male",
          "name" : "ln",
          "desc" : "my name is lee nai"
        }
      }
    ]
  }
}

2.5.1 单条/全表查询

详见2.4.2

2.5.2 分词查询

get /human_index/user/_search
{
  "query": {
    "match": {
      "name": "ln"
    }
  }
}

结果会查出三条记录(省略部分结果)

{
    "_index" : "human_index",
    "_type" : "user",
    "_id" : "6",
    "_score" : 0.6099695,
    "_source" : {
        "name" : "ln sb",
        "country" : "China GuangDong Jieyang",
        "sex" : "male",
        "desc" : "my name is sb leemon",
        "age" : 27
    }
},
{
    "_index" : "human_index",
    "_type" : "user",
    "_id" : "5",
    "_score" : 0.2876821,
    "_source" : {
        "name" : "ln-1",
        "country" : "China GuangDong Jieyang",
        "sex" : "male",
        "desc" : "my name is leemon-1",
        "age" : 21
    }
},
{
    "_index" : "human_index",
    "_type" : "user",
    "_id" : "3",
    "_score" : 0.2876821,
    "_source" : {
        "age" : 24,
        "country" : "China GuangDong Shenzhen",
        "sex" : "male",
        "name" : "ln",
        "desc" : "my name is lee nai"
    }
}

可以看到,通过match查询时,当从文档中的name属性值中出现ln时,满足条件

2.5.3 子属性分词查询

get /human_index/user/_search
{
  "query": {
    "match": {
      "doc.name": "hh"
    }
  }
}

结果查出一条记录

{
    "_index" : "human_index",
    "_type" : "user",
    "_id" : "4",
    "_score" : 0.2876821,
    "_source" : {
        "doc" : {
            "name" : "lmc hh",
            "country" : "China GuangDong Jieyang",
            "sex" : "male",
            "desc" : "my name is leemon",
            "age" : 11
        }
    }
}

2.5.4 短句查询

前面的是对单个词进行查询,短句指的是多个词组合形成的句子

get /human_index/user/_search
{
  "query": {
    "match_phrase": {
      "country": "GuangDong Jieyang"
    }
  }
}

结果查出3条记录,id分别为:2,5,6

如果将match_phrase改成match,相当于只要country中出现GuangDong或者Jieyang,都会被查出来,相当于查询条件会先被分词,然后返回分词后查询的并集

2.5.5 模糊查询

这里的模糊查询跟关系型数据库的模糊查询有较大的差异,关系型的模糊查询与上面的分词,短句查询类似,Elasticsearch的模糊查询是指查询出参数内容和实际内容的编辑距离在2以内的文档

get /human_index/user/_search
{
  "query": {
    "fuzzy": {
      "country": "Jieyank"
    }
  }
}

get /human_index/user/_search
{
  "query": {
    "fuzzy": {
      "country": "Jieyamg"
    }
  }
}

等等。

由于JieyangJieyankJieyamg的编辑距离都在2以内,因此能够通过模糊查询得到。结果查出的记录id分别为:2,5,6

2.5.6 排序

get /human_index/user/_search
{
  "query": {
    "match": {
      "country": "Jieyang"
    }
  },
  "sort":[
    {
      "_id":{
        "order": "desc"
      }
    }
  ]
}

查询出来的文档数量和2.5.5一样,只不过根据id进行降序排序

2.5.7 分页查询

get /human_index/user/_search
{
  "query": {
    "match_all": {}
  },
  "sort":[
    {
      
      "age": {
        "order": "asc"
      }
    }
  ],
  "from": 0,
  "size": 3
}

查找age最小的三个文档记录,返回结果的记录id按顺序为:5,3,2

2.5.8 指定字段查询

get /human_index/user/_search
{
  "query": {
    "match": {
      "country": "Jieyang"
    }
  },
  "_source": ["name"]
}

查询结果如下所示:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "5",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "ln-1"
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "2",
        "_score" : 0.18232156,
        "_source" : {
          "name" : "sb"
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "6",
        "_score" : 0.18232156,
        "_source" : {
          "name" : "ln sb"
        }
      }
    ]
  }
}

2.5.9 多条件查询

如果需要多个查询条件拼接在一起就需要使用bool

bool 过滤可以用来合并多个过滤条件查询结果的布尔逻辑,它包含以下操作符:

  • must:多个查询条件的完全匹配,相当于 AND
  • must_not:多个查询条件的相反匹配,相当于 NOT
  • should:至少有一个条件符合匹配,相当于 OR

查找country出现Jieyang,name出现sb,age在24-26中的文档

get /human_index/user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "country": "Jieyang"
          }
        },
        {
          "match": {
            "name": "sb"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 24,
            "lte": 26
          }
        }
      }
    }
  }
}

结果只查出id为2的文档

关于范围查询:

  • gte:大于或大于
  • gt:大于
  • lte:小于或等于
  • le:小于

查找country出现Jieyang或name出现sb,并且age在24-26中的文档

get /human_index/user/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "country": "Jieyang"
          }
        },
        {
          "match": {
            "name": "sb"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 24,
            "lte": 26
          }
        }
      }
    }
  }
}

结果查出id为1,2,3的文档

2.5.10 高亮显示

查询返回结果的时候,将查询条件的内容高亮显示

get /human_index/user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "country": "Jieyang"
          }
        },
        {
          "match": {
            "name": "sb"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 24,
            "lte": 26
          }
        }
      }
    }
  },
  "highlight": {
    "fields": {
      "country": {}
    }
  }
}

返回结果

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.39343074,
    "hits" : [
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "2",
        "_score" : 0.39343074,
        "_source" : {
          "name" : "sb",
          "desc" : "my name is sb",
          "age" : 25,
          "country" : "China GuangDong Jieyang",
          "sex" : "female"
        },
        "highlight" : {
          "country" : [
            "China GuangDong <em>Jieyang</em>"
          ]
        }
      }
    ]
  }
}

2.6 聚合分析

2.6.1 简单分组

country的每个词进行分组,统计出现的文档数量(用户user数量)

get /human_index/user/_search
{
  "aggs": {
    "group_by_tag": {
      "terms": {
        "field": "country"
        
      }
    }
  }
}

返回结果

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [country] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "human_index",
        "node": "2FgJQbJ5QhWVXfvoaI2kqQ",
        "reason": {
          "type": "illegal_argument_exception",
          "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [country] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
        }
      }
    ],
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [country] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [country] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
      }
    }
  },
  "status": 400
}

这里发现报错了,但是原因不是执行命令的问题,是因为elasticsearch默认fielddata的值为false,此时先要对分组的字段进行处理,将fielddata值修改为true

get /human_index/_mapping/user
{
  "properties": {
    "country": {
      "type": "text",
      "fielddata": true
    }
  }
}
================================ 结果 ================================
{
  "acknowledged" : true
}

再重新执行一遍统计命令,得到结果:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "name" : "ln-1",
          "country" : "China GuangDong Jieyang",
          "sex" : "male",
          "desc" : "my name is leemon-1",
          "age" : 21
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "sb",
          "desc" : "my name is sb",
          "age" : 25,
          "country" : "China GuangDong Jieyang",
          "sex" : "female"
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "doc" : {
            "name" : "lmc hh",
            "country" : "China GuangDong Jieyang",
            "sex" : "male",
            "desc" : "my name is leemon",
            "age" : 11
          }
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "name" : "ln sb",
          "country" : "China GuangDong Jieyang",
          "sex" : "male",
          "desc" : "my name is sb leemon",
          "age" : 27
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "hh",
          "desc" : "my name is hh",
          "age" : 25,
          "country" : "China GuangDong",
          "sex" : "female"
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "age" : 24,
          "country" : "China GuangDong Shenzhen",
          "sex" : "male",
          "name" : "ln",
          "desc" : "my name is lee nai"
        }
      }
    ]
  },
  "aggregations" : {
    "group_by_tag" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "china",
          "doc_count" : 5
        },
        {
          "key" : "guangdong",
          "doc_count" : 5
        },
        {
          "key" : "jieyang",
          "doc_count" : 3
        },
        {
          "key" : "shenzhen",
          "doc_count" : 1
        }
      ]
    }
  }
}

可以看到aggregations中,对country每个词出现的文档数量

2.6.2 分组统计

sex进行分组,计算每个分组的平均age,再按照平均age降序排序。在查询之前,记得先对sexfielddata进行设置

get /human_index/user/_search
{
  "aggs": {
    "group_by_tag": {
      "terms": {
        "field": "sex",
        "order": {
          "avg_age": "desc"
        }
      },
      "aggs": {
        "avg_age": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}

结果如下所示:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "name" : "ln-1",
          "country" : "China GuangDong Jieyang",
          "sex" : "male",
          "desc" : "my name is leemon-1",
          "age" : 21
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "sb",
          "desc" : "my name is sb",
          "age" : 25,
          "country" : "China GuangDong Jieyang",
          "sex" : "female"
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "doc" : {
            "name" : "lmc hh",
            "country" : "China GuangDong Jieyang",
            "sex" : "male",
            "desc" : "my name is leemon",
            "age" : 11
          }
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "name" : "ln sb",
          "country" : "China GuangDong Jieyang",
          "sex" : "male",
          "desc" : "my name is sb leemon",
          "age" : 27
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "hh",
          "desc" : "my name is hh",
          "age" : 25,
          "country" : "China GuangDong",
          "sex" : "female"
        }
      },
      {
        "_index" : "human_index",
        "_type" : "user",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "age" : 24,
          "country" : "China GuangDong Shenzhen",
          "sex" : "male",
          "name" : "ln",
          "desc" : "my name is lee nai"
        }
      }
    ]
  },
  "aggregations" : {
    "group_by_tag" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "female",
          "doc_count" : 2,
          "avg_age" : {
            "value" : 25.0
          }
        },
        {
          "key" : "male",
          "doc_count" : 3,
          "avg_age" : {
            "value" : 24.0
          }
        }
      ]
    }
  }
}

2.6.3 区间分组

划分age范围区间,按照年龄区间进行分组,在每个分组内再按照sex进行分组,然后计算每个分组的平均年龄,降序排序

get /human_index/user/_search
{
  "aggs": {
    "group_age_range": {
      "range": {
        "field": "age",
        "ranges": [
            {
              "from": 0,
              "to": 10
            },{
              "from": 11,
              "to": 20
            },{
              "from": 21,
              "to": 25
            },{
              "from": 25,
              "to": 30
            }
          ]
      },
      "aggs": {
        "group_by_sex": {
          "terms": {
            "field": "sex",
            "order": {
              "avg_age": "desc"
            }
          },
          "aggs": {
            "avg_age": {
              "avg": {
                "field": "age"
              }
            }
          }
        }
      }
    }
  }
}

输出结果的aggregations如下所示:

{
    "group_age_range" : {
        "buckets" : [
            {
                "key" : "0.0-10.0",
                "from" : 0.0,
                "to" : 10.0,
                "doc_count" : 0,
                "group_by_sex" : {
                    "doc_count_error_upper_bound" : 0,
                    "sum_other_doc_count" : 0,
                    "buckets" : [ ]
                }
            },
            {
                "key" : "11.0-20.0",
                "from" : 11.0,
                "to" : 20.0,
                "doc_count" : 0,
                "group_by_sex" : {
                    "doc_count_error_upper_bound" : 0,
                    "sum_other_doc_count" : 0,
                    "buckets" : [ ]
                }
            },
            {
                "key" : "21.0-25.0",
                "from" : 21.0,
                "to" : 25.0,
                "doc_count" : 2,
                "group_by_sex" : {
                    "doc_count_error_upper_bound" : 0,
                    "sum_other_doc_count" : 0,
                    "buckets" : [
                        {
                            "key" : "male",
                            "doc_count" : 2,
                            "avg_age" : {
                                "value" : 22.5
                            }
                        }
                    ]
                }
            },
            {
                "key" : "25.0-30.0",
                "from" : 25.0,
                "to" : 30.0,
                "doc_count" : 3,
                "group_by_sex" : {
                    "doc_count_error_upper_bound" : 0,
                    "sum_other_doc_count" : 0,
                    "buckets" : [
                        {
                            "key" : "male",
                            "doc_count" : 1,
                            "avg_age" : {
                                "value" : 27.0
                            }
                        },
                        {
                            "key" : "female",
                            "doc_count" : 2,
                            "avg_age" : {
                                "value" : 25.0
                            }
                        }
                    ]
                }
            }
        ]
    }
}

2.7 Mapping

通过_mapping可以设置和查看每个类型每个字段的数据类型等等

2.7.1 查看所有类型type的mapping

get /human_index/_mapping
================================ 结果 ================================
#! Deprecation: [types removal] The parameter include_type_name should be explicitly specified in get mapping requests to prepare for 7.0. In 7.0 include_type_name will default to 'false', which means responses will omit the type name in mapping definitions.
{
  "human_index" : {
    "mappings" : {
      "user" : {
        "properties" : {
          "age" : {
            "type" : "long"
          },
          "country" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            },
            "fielddata" : true
          },
          "desc" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "doc" : {
            "properties" : {
              "age" : {
                "type" : "long"
              },
              "country" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              },
              "desc" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              },
              "name" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              },
              "sex" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              }
            }
          },
          "name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          },
          "sex" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            },
            "fielddata" : true
          },
          "tags" : {
            "type" : "text",
            "fielddata" : true
          }
        }
      }
    }
  }
}

2.7.2 查看单个类型type的mapping

get /human_index/_mapping/user

由于当前只有一个索引human_index,且索引下只有一个类型user,因此结果与2.7.1基本一致

2.7.3 修改mapping

参考2.6.1的修改fielddata属性

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/169872.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Scrum 敏捷开发

什么是敏捷开发 敏捷 开发是一个术语&#xff0c;用于描述迭代软件开发。 迭代软件开发通过在短增量完成工作&#xff08;通常称为 冲刺&#xff0c; Sprint&#xff09;来缩短 DevOps 生命周期。 冲刺通常长达一到四周。 敏捷开发通常与传统或瀑布式开发形成鲜明对比&#xff…

Vue基础9之脚手架的使用、ref属性、props配置项和mixin混入

Vue基础9使用Vue脚手架初始化脚手架说明具体步骤项目文件介绍将前面写好的单文件组件放入这里运行脚手架文件结构render的作用修改默认配置配置项ref属性props配置项简单的传值方法默认的字符串传值使用v-bind对数字类型进行传值限制数据类型接收数据时候只对数据类型进行限制接…

Java 搜索二维矩阵 II

搜索二维矩阵 II中等编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性&#xff1a;每行的元素从左到右升序排列。每列的元素从上到下升序排列。示例 1&#xff1a;输入&#xff1a;matrix [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22]…

v-model表单

1、v-model的基本使用 表单提交是开发中非常常见的功能&#xff0c;也是和用户交互的重要手段&#xff1a; 比如用户在登录、注册时需要提交账号密码&#xff1b;比如用户在检索、创建、更新信息时&#xff0c;需要提交一些数据&#xff1b; 这些都要求我们可以在代码逻辑中获…

【GD32F427开发板试用】+软件IIC(OLED显示)

本篇文章来自极术社区与兆易创新组织的GD32F427开发板评测活动&#xff0c;更多开发板试用活动请关注极术社区网站。作者&#xff1a;四季的温度 gitee开源地址https://gitee.com/sijiwendu/gd32-f427-v/tree/master/module/oled 上一次完成DHT11开发文章https://aijishu.com/a…

什么是EDA?常见的EDA工具有哪些?

大家都知道&#xff0c;芯片设计难度非常大&#xff0c;要把上千亿颗晶体管集成在面积不到指甲盖大小的芯片上。其实能实现这样目标所以靠的是电子设计自动化&#xff08;Electronics Design Automation&#xff09;工具&#xff0c;也就是我们所称的EDA工具。 什么是EDA&…

从零搭建SpringCloud服务

一.微服务基础1.什么是SpringCloud&#xff1f;SpringCloud官网&#xff1a;https://spring.io/projects/spring-cloud&#xff08;个人建议是用谷歌浏览器访问官网打开中文翻译粗略把官网读一遍&#xff09;个人理解&#xff1a;以前的服务器就好像&#xff0c;一个会语数外全…

Elasticsearch:从实例中学习 nested 数据类型的 CRUD 及搜索

nested 数据类型是一个比较高级的话题。在本文中&#xff0c;将介绍 Elasticsearch 中针对嵌套对象的一些高级 CRUD 和搜索查询。 如果你想了解有关 Elasticsearch 基础知识的更多信息&#xff0c;可以查看这些文章以快速入门或复习&#xff1a; Elasticsearch&#xff1a;关于…

koa-router 正解

Koa-Router 之前分析过 Koa/ Koa-Bodyparser 的源码&#xff0c;今天让我们来分析下koa-router的源码&#xff0c;这个插件其实还是挺重要的。毕竟作为路由&#xff0c;我们还是要知道他的工作原理 这里会重申下 其实我是分析了 koa-router 主干流程。一些小众类的方法并没有看…

多步骤复杂 SQL 优化实例

问题先看数据&#xff1a;deliver 表是主表&#xff0c;一个客户会发生多次投递行为&#xff1a;deliverItem 表是从表&#xff0c;一个投递行为有多个投递项&#xff0c;delivered 是投递状态&#xff08;1 表示未完成&#xff0c;2 表示投递完成&#xff09;&#xff1a;需求…

如何了解一个软件的设计?

刚入职&#xff0c;接手新项目&#xff0c;面对一个全新项目&#xff0c;怎么快速研究它&#xff1f; 很多人直接看源码&#xff0c;一头扎入代码&#xff0c;很快就迷失其中&#xff0c;最初那股子探索精神&#xff0c;也会逐渐被迷茫所替。有多少次你满怀激情打开一个开源项…

极光推送REST API与Java后台对接

极光推送官网的web推送页面 因为是对接它的api&#xff0c;所以我参照这这个样式实现了一个&#xff0c;效果如下&#xff1a; 定时任务推送界面&#xff0c;可定制。实现了推送一次和每日定时推送&#xff0c;如果再扩展的话有每周、每月的功能&#xff0c;只是没有这个业务…

银行数字化转型导师坚鹏:银行数字化转型的五大痛点

首先从汇丰银行业绩持续下滑谈起&#xff0c;汇丰银行作为一家国际知名的全球性银行&#xff0c;最近10年左右的时间里&#xff0c;营业收入持续下降&#xff0c;已经从2008年的1400多亿美元到2021年的804.29亿美元; 净利润徘徊不前,2021年比2020年下降29.2%&#xff0c;仅为52…

kafka心得记录

1.为何引入kafka? 削峰填谷,主要还是为了应对上游瞬时大流量的冲击&#xff0c;避免出现流量毛刺现象&#xff0c;保护下游应用和数据库不被大流量打垮。 2.kafka备份机制&#xff0c;主从机制&#xff0c;Leader-Follower&#xff1a; Kafka 定义了两类副本&#xff1a;领导…

C语言文件操作函数详解——将你的代码永久化 ( •̀ ω •́ )✧

&#x1f384;博客主页&#xff1a;&#x1f390;大明超听话 &#x1f38b;欢迎关注&#xff1a;&#x1f44d;点赞&#x1f64c;关注✍评论 &#x1f38d;系列专栏&#xff1a;&#x1f391;从零开始C语言 &#x1f38a;从0开始数据结构与算法详解 &#x1f386;计算机考研——…

JavaScript中的原型链

本文作者为奇舞团前端开发工程师概述JavaScript 是 Web 的编程语言&#xff0c;简单易学&#xff0c;功能强大&#xff0c;但由于过于灵活设计理念&#xff0c;导致初学者经常一脸懵&#xff0c;本文要谈的是JavaScript中难点之一原型链。原型链的前世JavaScript的诞生要理解Ja…

Nessus介绍与安装

Nessus介绍与安装 1.Nessus简介 Nessus号称是世界上最流行的漏洞扫描程序&#xff0c;全世界有超过75000个组织在使用它。该工具提供完整的电脑漏洞扫描服务&#xff0c;并随时更新其漏洞数据库。Nessus不同于传统的漏洞扫描软件&#xff0c;Nessus可同时在本机或远端上遥控&…

测试开发 | Dubbo 接口测试原理及多种方法实践总结

image1080478 86.9 KB 1、什么是 Dubbo&#xff1f; Dubbo 最开始是应用于淘宝网&#xff0c;由阿里巴巴开源的一款优秀的高性能服务框架&#xff0c;由 Java 开发&#xff0c;后来贡献给了 Apache 开源基金会组织。 下面以官网的一个说明来了解一下架构的演变过程&#xff0…

初学Java中的方法,看这篇就够了

本篇介绍了Java中方法的概念以及方法的使用(方法的定义和调用,实参和形参的关系).方法重载的介绍和使用,编译器如何实现方法重载- -方法签名,介绍和使用方法调用自身解决问题的技巧–递归 对比递归和循环的优缺点 掌握Java中的方法一.方法的概念及使用1.什么是方法2.方法的使用…

【C++】AVL树

​&#x1f320; 作者&#xff1a;阿亮joy. &#x1f386;专栏&#xff1a;《吃透西嘎嘎》 &#x1f387; 座右铭&#xff1a;每个优秀的人都有一段沉默的时光&#xff0c;那段时光是付出了很多努力却得不到结果的日子&#xff0c;我们把它叫做扎根 目录&#x1f449;AVL树&am…