【谷粒商城】Elasticsearch学习记录

简介

Mysql用于持久化存储,ES主要用于搜索
基本概念:index库>type表>document文档

1. Index库

动词:相当于mysql中的insert
名词:相当于mysql中的db

2. Type表

在index中可以定义一个或者多个类型
类似于mysql中的table,每一种类型都放在一起

3. Document文档

保存在某个index下,某种type的一个数据document,文档数据格式是json格式的,document就像mysql中某个table里面的内容,每一行对应的列叫属性

在这里插入图片描述

安装

1.使用docker安装镜像

docker pull elasticsearch:7.4.2
docker pull kibana:7.4.2
版本要统一

2. 配置

# 将docker里的目录挂载到linux的/mydata目录中
# 修改/mydata就可以改掉docker里的
mkdir -p /mydata/elasticsearch/config
mkdir -p /mydata/elasticsearch/data

# es可以被远程任何机器访问
echo "http.host: 0.0.0.0" >/mydata/elasticsearch/config/elasticsearch.yml

# 递归更改权限,es需要访问
chmod -R 777 /mydata/elasticsearch/

3. 启动Elastic search

# 9200是用户交互端口 9300是集群心跳端口
# -e指定是单阶段运行
# -e指定占用的内存大小,生产时可以设置32G
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
-e  "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx512m" \
-v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-v  /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-d elasticsearch:7.4.2 


# 设置开机启动elasticsearch
docker update elasticsearch --restart=always

4. 启动kibana

# kibana指定了了ES交互端口9200  # 5600位kibana主页端口
docker run --name kibana -e ELASTICSEARCH_HOSTS=http://192.168.56.10:9200 -p 5601:5601 -d kibana:7.4.2
# 设置开机启动kibana
docker update kibana  --restart=always

elasticsearch 绑定的是自己的elasticsearch地址 http://192.168.56.10:9200

5. 初步检索

1)检索es信息

(1)GET /_cat/nodes:查看所有节点

127.0.0.1 16 95 1 0.03 0.07 0.11 dilm * 66470631f4b2
66470631f4b2代表节点
 * 代表是主节点

(2)GET /_cat/health:查看es健康状况

1654048575 01:56:15 elasticsearch green 1 1 3 3 0 0 0 0 - 100.0%
注:green表示健康值正常

(3)GET /_cat/master:查看主节点
如: http://192.168.56.10:9200/_cat/master

B4Os7Pu7RAqzvaPsxCF8Bg 127.0.0.1 127.0.0.1 66470631f4b2
主节点唯一编号
虚拟机地址

(4)GET/_cat/indices:查看所有索引 ,等价于mysql数据库的show databases;
如:http://192.168.56.10:9200/_cat/indices

green open .kibana_task_manager_1   7zpUGPdgSVaWNzYfWUpaLw 1 0 2 0 46.2kb 46.2kb
green open .apm-agent-configuration GddMwZvXTcCmQhE06-mrkQ 1 0 0 0   283b   283b
green open .kibana_1                qBx9BiwMRb2JuUmsLBypFA 1 0 9 9 29.9kb 29.9kb
这3个索引是kibana创建的

2)新增文档

保存一个数据,保存在哪个索引的哪个类型下(哪张数据库哪张表下),保存时用唯一标识指定

# # 在customer 索引下的external类型下保存1号数据
PUT customer/external/1
## 在postman中输入
http://192.168.56.10:9200/customer/external/1
# # 在body中加入需要插入的数据
{
 "name":"John Doe"
}

PUT和POST的区别
POST 新增。如果不指定id,会自动生成id ,指定id会修改这个数据,并新增版本号

  • 可以不指定id,不指定id时永远为创建
  • 指定不存在的id为创建
  • 指定存在的id为更新,而版本号会根据内容变没变而觉得版本号递增与否
    PUT可以新增也可以修改。PUT必须指定id;由于PUT需要指定id,我们一般用来做修改操作,不指定id会报错。
  • 必须指定id
  • 版本号总会增加

seq_no和version的区别:
每个文档的版本号"_version" 起始值都为1 每次对当前文档成功操作后都加1
而序列号"_seq_no"则可以看做是索引的信息 在第一次为索引插入数据时为0,每对索引内数据操作成功一次sqlNO加1, 并且文档会记录是第几次操作使它成为现在的情况的
可以参考https://www.cnblogs.com/Taeso/p/13363136.html

在这里插入图片描述

3)查看文档

GET http://192.168.140.181:9200/customer/external/1

{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "John Doe"
    }
}

乐观锁用法:通过“if_seq_no=1&if_primary_term=1”,当序列号匹配的时候,才进行修改,否则不修改。

第一次请求成功。

在这里插入图片描述

第二次请求英文seq_no的值已经发生了改变,所以请求不成功

在这里插入图片描述

4)更新文档_update

POST customer/externel/1/_update
{
    "doc":{
        "name":"111"
    }
}
或者
POST customer/externel/1
{
    "doc":{
        "name":"222"
    }
}
或者
PUT customer/externel/1
{
    "doc":{
        "name":"222"
    }
}

不同:带有update情况下

POST带有update的情况下,会对文档数据进行判断,如果没更新,则版本号不会进行增加
PUT操作总会重新保存并增加version版本
POST时带_update对比元数据如果一样就不进行任何操作。
看场景:

  • 对于大并发更新,不带update
  • 对于大并发查询偶尔更新,带update;对比更新,重新计算分配规则

在这里插入图片描述

5)删除文档或索引

Api请求:192.168.140.181:9200/customer/external/1
在这里插入图片描述

DELETE customer/external/1
DELETE customer

注:elasticsearch并没有提供删除类型的操作,只提供了删除索引和文档的操作。

删除完请求获取操作

{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "found": false
}

删除索引
实例:删除整个costomer索引数据
uri:192.168.140.181:9200/_cat/indices

green  open .kibana_task_manager_1   7zpUGPdgSVaWNzYfWUpaLw 1 0 2 0 30.5kb 30.5kb
green  open .apm-agent-configuration GddMwZvXTcCmQhE06-mrkQ 1 0 0 0   283b   283b
green  open .kibana_1                qBx9BiwMRb2JuUmsLBypFA 1 0 9 0 10.6kb 10.6kb
yellow open customer                 DTebFZ_cTBaXxrRHxDCfCg 1 1 0 0   250b   250b

删除“ customer ”索引
DELTE http://192.168.56.10:9200/customer

{
    "acknowledged": true
}

6)ES的批量操作——bulk

POST http://192.168.56.10:9200/customer/external/_bulk

两行为一个整体
{"index":{"_id":"1"}}
{"name":"a"}
{"index":{"_id":"2"}}
{"name":"b"}
注意格式json和text均不可,要去kibana里Dev Tools

语法格式:

{action:{metadata}}\n
{request body  }\n
{action:{metadata}}\n
{request body  }\n

这里的批量操作,当发生某一条执行发生失败时,其他的数据仍然能够接着执行,也就是说彼此之间是独立的。

bulk api以此按顺序执行所有的action(动作)。如果一个单个的动作因任何原因失败,它将继续处理它后面剩余的动作。当bulk api返回时,它将提供每个动作的状态(与发送的顺序相同),所以您可以检查是否一个指定的动作是否失败了。

实际请求:

POST customer/external/_bulk
{"index":{"_id":"1"}}
{"name":"John Doe"}
{"index":{"_id":2}}
{"name":"gek"}

返回:

#! Deprecation: [types removal] Specifying types in bulk requests is deprecated.
{
##请求花费了多少时间
  "took" : 204,
  "errors" : false,##没有发生任何错误
  "items" : [##每个数据的结果
    {
      "index" : {
        "_index" : "customer",
        "_type" : "external",
        "_id" : "1",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "index" : {
        "_index" : "customer",
        "_type" : "external",
        "_id" : "2",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 1,
        "_primary_term" : 1,
        "status" : 201
      }
    }
  ]
}

实例2:对于整个索引执行批量操作

POST /_bulk
{"delete":{"_index":"website","_type":"blog","_id":"123"}}
{"create":{"_index":"website","_type":"blog","_id":"123"}}
{"title":"my first blog post"}
{"index":{"_index":"website","_type":"blog"}}
{"title":"my second blog post"}
{"update":{"_index":"website","_type":"blog","_id":"123"}}
{"doc":{"title":"my updated blog post"}}

返回:
··

7)ES-进阶-QueryDSL基本使用

GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "balance": {
        "order": "desc"
      }
    }
  ],
  "from": 5,
  "size": 5,
  "_source": ["balance","firstname"]
  
}

from和size类似于mysql中的limit,用于分页查询
_source 类似于mysql中的field ,就是查询指定字段的值

match全文检索
match_all:匹配全部
match:全文检索

match 检索的字段如果是字符串类型就是模糊匹配,基本类型的话是精确匹配

GET bank/_search
{
  "query": {
    "match": {
      "address": "mill lane"
    }
  }
}

返回值:

{
  "took" : 187,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 19,
      "relation" : "eq"
    },
    "max_score" : 9.507477,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "136",
        "_score" : 9.507477,
        "_source" : {
          "account_number" : 136,
          "balance" : 45801,
          "firstname" : "Winnie",
          "lastname" : "Holland",
          "age" : 38,
          "gender" : "M",
          "address" : "198 Mill Lane",
          "employer" : "Neteria",
          "email" : "winnieholland@neteria.com",
          "city" : "Urie",
          "state" : "IL"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "970",
        "_score" : 5.4032025,
        "_source" : {
          "account_number" : 970,
          "balance" : 19648,
          "firstname" : "Forbes",
          "lastname" : "Wallace",
          "age" : 28,
          "gender" : "M",
          "address" : "990 Mill Road",
          "employer" : "Pheast",
          "email" : "forbeswallace@pheast.com",
          "city" : "Lopezo",
          "state" : "AK"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "345",
        "_score" : 5.4032025,
        "_source" : {
          "account_number" : 345,
          "balance" : 9812,
          "firstname" : "Parker",
          "lastname" : "Hines",
          "age" : 38,
          "gender" : "M",
          "address" : "715 Mill Avenue",
          "employer" : "Baluba",
          "email" : "parkerhines@baluba.com",
          "city" : "Blackgum",
          "state" : "KY"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "472",
        "_score" : 5.4032025,
        "_source" : {
          "account_number" : 472,
          "balance" : 25571,
          "firstname" : "Lee",
          "lastname" : "Long",
          "age" : 32,
          "gender" : "F",
          "address" : "288 Mill Street",
          "employer" : "Comverges",
          "email" : "leelong@comverges.com",
          "city" : "Movico",
          "state" : "MT"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "1",
        "_score" : 4.1042743,
        "_source" : {
          "account_number" : 1,
          "balance" : 39225,
          "firstname" : "Amber",
          "lastname" : "Duke",
          "age" : 32,
          "gender" : "M",
          "address" : "880 Holmes Lane",
          "employer" : "Pyrami",
          "email" : "amberduke@pyrami.com",
          "city" : "Brogan",
          "state" : "IL"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "70",
        "_score" : 4.1042743,
        "_source" : {
          "account_number" : 70,
          "balance" : 38172,
          "firstname" : "Deidre",
          "lastname" : "Thompson",
          "age" : 33,
          "gender" : "F",
          "address" : "685 School Lane",
          "employer" : "Netplode",
          "email" : "deidrethompson@netplode.com",
          "city" : "Chestnut",
          "state" : "GA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "556",
        "_score" : 4.1042743,
        "_source" : {
          "account_number" : 556,
          "balance" : 36420,
          "firstname" : "Collier",
          "lastname" : "Odonnell",
          "age" : 35,
          "gender" : "M",
          "address" : "591 Nolans Lane",
          "employer" : "Sultraxin",
          "email" : "collierodonnell@sultraxin.com",
          "city" : "Fulford",
          "state" : "MD"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "568",
        "_score" : 4.1042743,
        "_source" : {
          "account_number" : 568,
          "balance" : 36628,
          "firstname" : "Lesa",
          "lastname" : "Maynard",
          "age" : 29,
          "gender" : "F",
          "address" : "295 Whitty Lane",
          "employer" : "Coash",
          "email" : "lesamaynard@coash.com",
          "city" : "Broadlands",
          "state" : "VT"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "715",
        "_score" : 4.1042743,
        "_source" : {
          "account_number" : 715,
          "balance" : 23734,
          "firstname" : "Tammi",
          "lastname" : "Hodge",
          "age" : 24,
          "gender" : "M",
          "address" : "865 Church Lane",
          "employer" : "Netur",
          "email" : "tammihodge@netur.com",
          "city" : "Lacomb",
          "state" : "KS"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "449",
        "_score" : 4.1042743,
        "_source" : {
          "account_number" : 449,
          "balance" : 41950,
          "firstname" : "Barnett",
          "lastname" : "Cantrell",
          "age" : 39,
          "gender" : "F",
          "address" : "945 Bedell Lane",
          "employer" : "Zentility",
          "email" : "barnettcantrell@zentility.com",
          "city" : "Swartzville",
          "state" : "ND"
        }
      }
    ]
  }
}

match_phrase短语匹配
与match不同的是,不进行一个分词匹配,当作一个整体进行匹配

GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "mill lane"
    }
  }
}

返回值:

{
  "took" : 84,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 9.507477,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "136",
        "_score" : 9.507477,
        "_source" : {
          "account_number" : 136,
          "balance" : 45801,
          "firstname" : "Winnie",
          "lastname" : "Holland",
          "age" : 38,
          "gender" : "M",
          "address" : "198 Mill Lane",
          "employer" : "Neteria",
          "email" : "winnieholland@neteria.com",
          "city" : "Urie",
          "state" : "IL"
        }
      }
    ]
  }
}

multi_match多字段匹配

多字段匹配会进行一个分词匹配,会进行分词

GET bank/_search
{
  "query": {
    "multi_match": {
      "query": "mill movico",
      "fields": [
        "address",
        "city"
      ]
    }
  }
}

查询结果

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 6.505949,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "472",
        "_score" : 6.505949,
        "_source" : {
          "account_number" : 472,
          "balance" : 25571,
          "firstname" : "Lee",
          "lastname" : "Long",
          "age" : 32,
          "gender" : "F",
          "address" : "288 Mill Street",
          "employer" : "Comverges",
          "email" : "leelong@comverges.com",
          "city" : "Movico",
          "state" : "MT"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "970",
        "_score" : 5.4032025,
        "_source" : {
          "account_number" : 970,
          "balance" : 19648,
          "firstname" : "Forbes",
          "lastname" : "Wallace",
          "age" : 28,
          "gender" : "M",
          "address" : "990 Mill Road",
          "employer" : "Pheast",
          "email" : "forbeswallace@pheast.com",
          "city" : "Lopezo",
          "state" : "AK"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "136",
        "_score" : 5.4032025,
        "_source" : {
          "account_number" : 136,
          "balance" : 45801,
          "firstname" : "Winnie",
          "lastname" : "Holland",
          "age" : 38,
          "gender" : "M",
          "address" : "198 Mill Lane",
          "employer" : "Neteria",
          "email" : "winnieholland@neteria.com",
          "city" : "Urie",
          "state" : "IL"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "345",
        "_score" : 5.4032025,
        "_source" : {
          "account_number" : 345,
          "balance" : 9812,
          "firstname" : "Parker",
          "lastname" : "Hines",
          "age" : 38,
          "gender" : "M",
          "address" : "715 Mill Avenue",
          "employer" : "Baluba",
          "email" : "parkerhines@baluba.com",
          "city" : "Blackgum",
          "state" : "KY"
        }
      }
    ]
  }
}

must must_not should

must表示一定需要匹配,must_not一定不匹配,should表示可以匹配可以不匹配,匹配的话,score会高一点


GET bank/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "gender": "M"
          }
        },
        {
          "match": {
            "address": "mill"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "age": 18
          }
        }
      ],
      "should": [
        {
          "match": {
            "lastname": "Wallace"
          }
        }
      ]
    }
  }
}

执行结果

{
  "took" : 37,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 12.585751,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "970",
        "_score" : 12.585751,
        "_source" : {
          "account_number" : 970,
          "balance" : 19648,
          "firstname" : "Forbes",
          "lastname" : "Wallace",
          "age" : 28,
          "gender" : "M",
          "address" : "990 Mill Road",
          "employer" : "Pheast",
          "email" : "forbeswallace@pheast.com",
          "city" : "Lopezo",
          "state" : "AK"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "136",
        "_score" : 6.0824604,
        "_source" : {
          "account_number" : 136,
          "balance" : 45801,
          "firstname" : "Winnie",
          "lastname" : "Holland",
          "age" : 38,
          "gender" : "M",
          "address" : "198 Mill Lane",
          "employer" : "Neteria",
          "email" : "winnieholland@neteria.com",
          "city" : "Urie",
          "state" : "IL"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "345",
        "_score" : 6.0824604,
        "_source" : {
          "account_number" : 345,
          "balance" : 9812,
          "firstname" : "Parker",
          "lastname" : "Hines",
          "age" : 38,
          "gender" : "M",
          "address" : "715 Mill Avenue",
          "employer" : "Baluba",
          "email" : "parkerhines@baluba.com",
          "city" : "Blackgum",
          "state" : "KY"
        }
      }
    ]
  }
}

Filter

filter过滤不会进行 _score值排序,score的值都是0

GET bank/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "age": {
            "gte": 18,
            "lte": 30
          }
        }
      }
    }
  }
}

执行结果

{
  "took" : 18,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 498,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "13",
        "_score" : 0.0,
        "_source" : {
          "account_number" : 13,
          "balance" : 32838,
          "firstname" : "Nanette",
          "lastname" : "Bates",
          "age" : 28,
          "gender" : "F",
          "address" : "789 Madison Street",
          "employer" : "Quility",
          "email" : "nanettebates@quility.com",
          "city" : "Nogal",
          "state" : "VA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "49",
        "_score" : 0.0,
        "_source" : {
          "account_number" : 49,
          "balance" : 29104,
          "firstname" : "Fulton",
          "lastname" : "Holt",
          "age" : 23,
          "gender" : "F",
          "address" : "451 Humboldt Street",
          "employer" : "Anocha",
          "email" : "fultonholt@anocha.com",
          "city" : "Sunriver",
          "state" : "RI"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "63",
        "_score" : 0.0,
        "_source" : {
          "account_number" : 63,
          "balance" : 6077,
          "firstname" : "Hughes",
          "lastname" : "Owens",
          "age" : 30,
          "gender" : "F",
          "address" : "510 Sedgwick Street",
          "employer" : "Valpreal",
          "email" : "hughesowens@valpreal.com",
          "city" : "Guilford",
          "state" : "KS"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "68",
        "_score" : 0.0,
        "_source" : {
          "account_number" : 68,
          "balance" : 44214,
          "firstname" : "Hall",
          "lastname" : "Key",
          "age" : 25,
          "gender" : "F",
          "address" : "927 Bay Parkway",
          "employer" : "Eventex",
          "email" : "hallkey@eventex.com",
          "city" : "Shawmut",
          "state" : "CA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "75",
        "_score" : 0.0,
        "_source" : {
          "account_number" : 75,
          "balance" : 40500,
          "firstname" : "Sandoval",
          "lastname" : "Kramer",
          "age" : 22,
          "gender" : "F",
          "address" : "166 Irvington Place",
          "employer" : "Overfork",
          "email" : "sandovalkramer@overfork.com",
          "city" : "Limestone",
          "state" : "NH"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "87",
        "_score" : 0.0,
        "_source" : {
          "account_number" : 87,
          "balance" : 1133,
          "firstname" : "Hewitt",
          "lastname" : "Kidd",
          "age" : 22,
          "gender" : "M",
          "address" : "446 Halleck Street",
          "employer" : "Isologics",
          "email" : "hewittkidd@isologics.com",
          "city" : "Coalmont",
          "state" : "ME"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "94",
        "_score" : 0.0,
        "_source" : {
          "account_number" : 94,
          "balance" : 41060,
          "firstname" : "Brittany",
          "lastname" : "Cabrera",
          "age" : 30,
          "gender" : "F",
          "address" : "183 Kathleen Court",
          "employer" : "Mixers",
          "email" : "brittanycabrera@mixers.com",
          "city" : "Cornucopia",
          "state" : "AZ"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "102",
        "_score" : 0.0,
        "_source" : {
          "account_number" : 102,
          "balance" : 29712,
          "firstname" : "Dena",
          "lastname" : "Olson",
          "age" : 27,
          "gender" : "F",
          "address" : "759 Newkirk Avenue",
          "employer" : "Hinway",
          "email" : "denaolson@hinway.com",
          "city" : "Choctaw",
          "state" : "NJ"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "107",
        "_score" : 0.0,
        "_source" : {
          "account_number" : 107,
          "balance" : 48844,
          "firstname" : "Randi",
          "lastname" : "Rich",
          "age" : 28,
          "gender" : "M",
          "address" : "694 Jefferson Street",
          "employer" : "Netplax",
          "email" : "randirich@netplax.com",
          "city" : "Bellfountain",
          "state" : "SC"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "119",
        "_score" : 0.0,
        "_source" : {
          "account_number" : 119,
          "balance" : 49222,
          "firstname" : "Laverne",
          "lastname" : "Johnson",
          "age" : 28,
          "gender" : "F",
          "address" : "302 Howard Place",
          "employer" : "Senmei",
          "email" : "lavernejohnson@senmei.com",
          "city" : "Herlong",
          "state" : "DC"
        }
      }
    ]
  }
}

term

term值的查询一般用于值是数字的情况下,精准匹配
全文检索用match ,非全文检索用term

GET bank/_search
{
  "query": {
    "term": {
      "age": 28
    }
  }
}

执行结果

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 51,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "13",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 13,
          "balance" : 32838,
          "firstname" : "Nanette",
          "lastname" : "Bates",
          "age" : 28,
          "gender" : "F",
          "address" : "789 Madison Street",
          "employer" : "Quility",
          "email" : "nanettebates@quility.com",
          "city" : "Nogal",
          "state" : "VA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "107",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 107,
          "balance" : 48844,
          "firstname" : "Randi",
          "lastname" : "Rich",
          "age" : 28,
          "gender" : "M",
          "address" : "694 Jefferson Street",
          "employer" : "Netplax",
          "email" : "randirich@netplax.com",
          "city" : "Bellfountain",
          "state" : "SC"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "119",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 119,
          "balance" : 49222,
          "firstname" : "Laverne",
          "lastname" : "Johnson",
          "age" : 28,
          "gender" : "F",
          "address" : "302 Howard Place",
          "employer" : "Senmei",
          "email" : "lavernejohnson@senmei.com",
          "city" : "Herlong",
          "state" : "DC"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "176",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 176,
          "balance" : 18607,
          "firstname" : "Kemp",
          "lastname" : "Walters",
          "age" : 28,
          "gender" : "F",
          "address" : "906 Howard Avenue",
          "employer" : "Eyewax",
          "email" : "kempwalters@eyewax.com",
          "city" : "Why",
          "state" : "KY"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "359",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 359,
          "balance" : 29927,
          "firstname" : "Vanessa",
          "lastname" : "Harvey",
          "age" : 28,
          "gender" : "F",
          "address" : "679 Rutledge Street",
          "employer" : "Zentime",
          "email" : "vanessaharvey@zentime.com",
          "city" : "Williston",
          "state" : "IL"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "506",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 506,
          "balance" : 43440,
          "firstname" : "Davidson",
          "lastname" : "Salas",
          "age" : 28,
          "gender" : "M",
          "address" : "731 Cleveland Street",
          "employer" : "Sequitur",
          "email" : "davidsonsalas@sequitur.com",
          "city" : "Lloyd",
          "state" : "ME"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "669",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 669,
          "balance" : 16934,
          "firstname" : "Jewel",
          "lastname" : "Estrada",
          "age" : 28,
          "gender" : "M",
          "address" : "896 Meeker Avenue",
          "employer" : "Zilla",
          "email" : "jewelestrada@zilla.com",
          "city" : "Goodville",
          "state" : "PA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "708",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 708,
          "balance" : 34002,
          "firstname" : "May",
          "lastname" : "Ortiz",
          "age" : 28,
          "gender" : "F",
          "address" : "244 Chauncey Street",
          "employer" : "Syntac",
          "email" : "mayortiz@syntac.com",
          "city" : "Munjor",
          "state" : "ID"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "746",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 746,
          "balance" : 15970,
          "firstname" : "Marguerite",
          "lastname" : "Wall",
          "age" : 28,
          "gender" : "F",
          "address" : "364 Crosby Avenue",
          "employer" : "Aquoavo",
          "email" : "margueritewall@aquoavo.com",
          "city" : "Jeff",
          "state" : "MI"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "758",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 758,
          "balance" : 15739,
          "firstname" : "Berta",
          "lastname" : "Short",
          "age" : 28,
          "gender" : "M",
          "address" : "149 Surf Avenue",
          "employer" : "Ozean",
          "email" : "bertashort@ozean.com",
          "city" : "Odessa",
          "state" : "UT"
        }
      }
    ]
  }
}

聚合

通过aggs 执行聚合,ageAgg表示聚合的名字,下面是聚合的字段

GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "ageAgg": {
      "terms": {
        "field": "age",
        "size": 100
      },
      "aggs": {
        "genderAgg": {
          "terms": {
            "field": "gender.keyword",
            "size": 10
          },
          "aggs": {
            "balanceAgg": {
              "avg": {
                "field": "balance"
              }
            }
          }
        },
        "ageBalanceAvg":{
            "avg": {
              "field": "balance"
            }
        }
      }
    }
  }
}

执行结果

{
  "took" : 59,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 1,
          "balance" : 39225,
          "firstname" : "Amber",
          "lastname" : "Duke",
          "age" : 32,
          "gender" : "M",
          "address" : "880 Holmes Lane",
          "employer" : "Pyrami",
          "email" : "amberduke@pyrami.com",
          "city" : "Brogan",
          "state" : "IL"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 6,
          "balance" : 5686,
          "firstname" : "Hattie",
          "lastname" : "Bond",
          "age" : 36,
          "gender" : "M",
          "address" : "671 Bristol Street",
          "employer" : "Netagy",
          "email" : "hattiebond@netagy.com",
          "city" : "Dante",
          "state" : "TN"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "13",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 13,
          "balance" : 32838,
          "firstname" : "Nanette",
          "lastname" : "Bates",
          "age" : 28,
          "gender" : "F",
          "address" : "789 Madison Street",
          "employer" : "Quility",
          "email" : "nanettebates@quility.com",
          "city" : "Nogal",
          "state" : "VA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "18",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 18,
          "balance" : 4180,
          "firstname" : "Dale",
          "lastname" : "Adams",
          "age" : 33,
          "gender" : "M",
          "address" : "467 Hutchinson Court",
          "employer" : "Boink",
          "email" : "daleadams@boink.com",
          "city" : "Orick",
          "state" : "MD"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "20",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 20,
          "balance" : 16418,
          "firstname" : "Elinor",
          "lastname" : "Ratliff",
          "age" : 36,
          "gender" : "M",
          "address" : "282 Kings Place",
          "employer" : "Scentric",
          "email" : "elinorratliff@scentric.com",
          "city" : "Ribera",
          "state" : "WA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "25",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 25,
          "balance" : 40540,
          "firstname" : "Virginia",
          "lastname" : "Ayala",
          "age" : 39,
          "gender" : "F",
          "address" : "171 Putnam Avenue",
          "employer" : "Filodyne",
          "email" : "virginiaayala@filodyne.com",
          "city" : "Nicholson",
          "state" : "PA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "32",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 32,
          "balance" : 48086,
          "firstname" : "Dillard",
          "lastname" : "Mcpherson",
          "age" : 34,
          "gender" : "F",
          "address" : "702 Quentin Street",
          "employer" : "Quailcom",
          "email" : "dillardmcpherson@quailcom.com",
          "city" : "Veguita",
          "state" : "IN"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "37",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 37,
          "balance" : 18612,
          "firstname" : "Mcgee",
          "lastname" : "Mooney",
          "age" : 39,
          "gender" : "M",
          "address" : "826 Fillmore Place",
          "employer" : "Reversus",
          "email" : "mcgeemooney@reversus.com",
          "city" : "Tooleville",
          "state" : "OK"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "44",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 44,
          "balance" : 34487,
          "firstname" : "Aurelia",
          "lastname" : "Harding",
          "age" : 37,
          "gender" : "M",
          "address" : "502 Baycliff Terrace",
          "employer" : "Orbalix",
          "email" : "aureliaharding@orbalix.com",
          "city" : "Yardville",
          "state" : "DE"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "49",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 49,
          "balance" : 29104,
          "firstname" : "Fulton",
          "lastname" : "Holt",
          "age" : 23,
          "gender" : "F",
          "address" : "451 Humboldt Street",
          "employer" : "Anocha",
          "email" : "fultonholt@anocha.com",
          "city" : "Sunriver",
          "state" : "RI"
        }
      }
    ]
  },
  "aggregations" : {
    "ageAgg" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 31,
          "doc_count" : 61,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "M",
                "doc_count" : 35,
                "balanceAgg" : {
                  "value" : 29565.628571428573
                }
              },
              {
                "key" : "F",
                "doc_count" : 26,
                "balanceAgg" : {
                  "value" : 26626.576923076922
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 28312.918032786885
          }
        },
        {
          "key" : 39,
          "doc_count" : 60,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "F",
                "doc_count" : 38,
                "balanceAgg" : {
                  "value" : 26348.684210526317
                }
              },
              {
                "key" : "M",
                "doc_count" : 22,
                "balanceAgg" : {
                  "value" : 23405.68181818182
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 25269.583333333332
          }
        },
        {
          "key" : 26,
          "doc_count" : 59,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "M",
                "doc_count" : 32,
                "balanceAgg" : {
                  "value" : 25094.78125
                }
              },
              {
                "key" : "F",
                "doc_count" : 27,
                "balanceAgg" : {
                  "value" : 20943.0
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 23194.813559322032
          }
        },
        {
          "key" : 32,
          "doc_count" : 52,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "M",
                "doc_count" : 28,
                "balanceAgg" : {
                  "value" : 22941.964285714286
                }
              },
              {
                "key" : "F",
                "doc_count" : 24,
                "balanceAgg" : {
                  "value" : 25128.958333333332
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 23951.346153846152
          }
        },
        {
          "key" : 35,
          "doc_count" : 52,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "M",
                "doc_count" : 28,
                "balanceAgg" : {
                  "value" : 24226.321428571428
                }
              },
              {
                "key" : "F",
                "doc_count" : 24,
                "balanceAgg" : {
                  "value" : 19698.791666666668
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 22136.69230769231
          }
        },
        {
          "key" : 36,
          "doc_count" : 52,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "M",
                "doc_count" : 31,
                "balanceAgg" : {
                  "value" : 20884.677419354837
                }
              },
              {
                "key" : "F",
                "doc_count" : 21,
                "balanceAgg" : {
                  "value" : 24079.04761904762
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 22174.71153846154
          }
        },
        {
          "key" : 22,
          "doc_count" : 51,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "F",
                "doc_count" : 27,
                "balanceAgg" : {
                  "value" : 22152.74074074074
                }
              },
              {
                "key" : "M",
                "doc_count" : 24,
                "balanceAgg" : {
                  "value" : 27631.708333333332
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 24731.07843137255
          }
        },
        {
          "key" : 28,
          "doc_count" : 51,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "F",
                "doc_count" : 31,
                "balanceAgg" : {
                  "value" : 27076.8064516129
                }
              },
              {
                "key" : "M",
                "doc_count" : 20,
                "balanceAgg" : {
                  "value" : 30129.35
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 28273.882352941175
          }
        },
        {
          "key" : 33,
          "doc_count" : 50,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "F",
                "doc_count" : 26,
                "balanceAgg" : {
                  "value" : 26437.615384615383
                }
              },
              {
                "key" : "M",
                "doc_count" : 24,
                "balanceAgg" : {
                  "value" : 23638.291666666668
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 25093.94
          }
        },
        {
          "key" : 34,
          "doc_count" : 49,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "F",
                "doc_count" : 30,
                "balanceAgg" : {
                  "value" : 26039.166666666668
                }
              },
              {
                "key" : "M",
                "doc_count" : 19,
                "balanceAgg" : {
                  "value" : 28027.0
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 26809.95918367347
          }
        },
        {
          "key" : 30,
          "doc_count" : 47,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "F",
                "doc_count" : 25,
                "balanceAgg" : {
                  "value" : 25316.16
                }
              },
              {
                "key" : "M",
                "doc_count" : 22,
                "balanceAgg" : {
                  "value" : 20028.545454545456
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 22841.106382978724
          }
        },
        {
          "key" : 21,
          "doc_count" : 46,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "F",
                "doc_count" : 24,
                "balanceAgg" : {
                  "value" : 28210.916666666668
                }
              },
              {
                "key" : "M",
                "doc_count" : 22,
                "balanceAgg" : {
                  "value" : 25640.18181818182
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 26981.434782608696
          }
        },
        {
          "key" : 40,
          "doc_count" : 45,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "M",
                "doc_count" : 24,
                "balanceAgg" : {
                  "value" : 26474.958333333332
                }
              },
              {
                "key" : "F",
                "doc_count" : 21,
                "balanceAgg" : {
                  "value" : 27992.571428571428
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 27183.17777777778
          }
        },
        {
          "key" : 20,
          "doc_count" : 44,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "M",
                "doc_count" : 27,
                "balanceAgg" : {
                  "value" : 29047.444444444445
                }
              },
              {
                "key" : "F",
                "doc_count" : 17,
                "balanceAgg" : {
                  "value" : 25666.647058823528
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 27741.227272727272
          }
        },
        {
          "key" : 23,
          "doc_count" : 42,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "M",
                "doc_count" : 24,
                "balanceAgg" : {
                  "value" : 27730.75
                }
              },
              {
                "key" : "F",
                "doc_count" : 18,
                "balanceAgg" : {
                  "value" : 26758.833333333332
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 27314.214285714286
          }
        },
        {
          "key" : 24,
          "doc_count" : 42,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "F",
                "doc_count" : 23,
                "balanceAgg" : {
                  "value" : 29414.521739130436
                }
              },
              {
                "key" : "M",
                "doc_count" : 19,
                "balanceAgg" : {
                  "value" : 27435.052631578947
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 28519.04761904762
          }
        },
        {
          "key" : 25,
          "doc_count" : 42,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "M",
                "doc_count" : 23,
                "balanceAgg" : {
                  "value" : 29336.08695652174
                }
              },
              {
                "key" : "F",
                "doc_count" : 19,
                "balanceAgg" : {
                  "value" : 25156.263157894737
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 27445.214285714286
          }
        },
        {
          "key" : 37,
          "doc_count" : 42,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "M",
                "doc_count" : 23,
                "balanceAgg" : {
                  "value" : 25015.739130434784
                }
              },
              {
                "key" : "F",
                "doc_count" : 19,
                "balanceAgg" : {
                  "value" : 29451.21052631579
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 27022.261904761905
          }
        },
        {
          "key" : 27,
          "doc_count" : 39,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "F",
                "doc_count" : 21,
                "balanceAgg" : {
                  "value" : 21618.85714285714
                }
              },
              {
                "key" : "M",
                "doc_count" : 18,
                "balanceAgg" : {
                  "value" : 21300.38888888889
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 21471.871794871793
          }
        },
        {
          "key" : 38,
          "doc_count" : 39,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "F",
                "doc_count" : 20,
                "balanceAgg" : {
                  "value" : 27931.65
                }
              },
              {
                "key" : "M",
                "doc_count" : 19,
                "balanceAgg" : {
                  "value" : 24350.894736842107
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 26187.17948717949
          }
        },
        {
          "key" : 29,
          "doc_count" : 35,
          "genderAgg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "M",
                "doc_count" : 23,
                "balanceAgg" : {
                  "value" : 29943.17391304348
                }
              },
              {
                "key" : "F",
                "doc_count" : 12,
                "balanceAgg" : {
                  "value" : 28601.416666666668
                }
              }
            ]
          },
          "ageBalanceAvg" : {
            "value" : 29483.14285714286
          }
        }
      ]
    }
  }
}

映射 mappings

就是给数据定义数据类型

创建映射
PUT /my_index
{
  "mappings": {
    "properties": {
      "age":{
        "type": "integer"
      },
      "email":{
        "type": "keyword"
      },
      "name":{
        "type": "text"
      }
    }
  }
}

执行结果

{
  "my_index" : {
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "integer"
        },
        "email" : {
          "type" : "keyword"
        },
        "name" : {
          "type" : "text"
        }
      }
    }
  }
}

数据迁移

新建索引

PUT /newbank
{
  "mappings": {
    "properties": { 
      "account_number": {
        "type": "long"
      },
      "address": {
        "type": "text"
      },
      "age": {
        "type": "integer"
      },
      "balance": {
        "type": "long"
      },
      "city": {
        "type": "keyword"
      },
      "email": {
        "type": "keyword"
      },
      "employer": {
        "type": "keyword"
      },
      "firstname": {
        "type": "text"
      },
      "gender": {
        "type": "keyword"
      },
      "lastname": {
        "type": "text"
      },
      "state": {
        "type": "keyword"
      }
  }
  }
}

迁移索引:6.X之后,type没有了,只需要reindex到目标索引就好了

POST _reindex
{
  "source": {
    "index": "bank",
    "type": "account"
  },
  "dest": {
    "index": "newbank"
  }
}

6.0之后

POST _reindex
{
  "source": {
    "index": "bank"
  },
  "dest": {
    "index": "newbank"
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值