初步检索

  • 初步检索
  • 1._cat
  • 查看所有节点
  • 查看ES健康状况
  • 查看主节点
  • 查看所有索引
  • 查看映射
  • 2. 索引一个文档
  • PUT
  • POST
  • POST和PUT区别:
  • 3.查询文档
  • GET customer/external/1
  • 如果id不存在
  • 如果type不存在
  • 如果索引不存在、
  • 4.更新文档
  • 1.使用POST _update
  • 2.使用 POST
  • 3.使用 PUT
  • POST和POST(_update) 区别
  • 5.删除文档&索引
  • 删除文档
  • 删除索引
  • 删除之后再次查询 返回 404
  • 6.批量API
  • 批量添加
  • 复杂批量
  • 导入官方测试数据
  • 进阶检索
  • 基于URI检索
  • 基于Rest检索
  • 查询所有 match_all
  • 基于balance排序
  • 基于balance排序 分页
  • 返回部分字段
  • 精确匹配 match
  • 匹配 balance 为 20 的数据
  • 区别:
  • 全文匹配
  • match_phrase [短语匹配]
  • multi_match [多字段匹配]
  • must [必须同时满足]
  • must_not [必须同时不满足]
  • should[应该满足]
  • filter[过滤]
  • term [精确值使用term 比如数值]
  • 聚合查询
  • 搜索address包含mill所有人的年龄分布以及平均工资
  • 按照年龄聚合,并且请求这些年龄段的这些人的平均薪资
  • 查出所有年龄分布,并且这些年龄段中 M 的平均薪资和 F 的平均薪资以及这个年龄段的总体平均薪资
  • 创建映射
  • 新增属性
  • 更新属性
  • 数据迁移
  • 分词
  • 自定义词库


初步检索

1._cat

查看所有节点

GET _cat/nodes
127.0.0.1 28 39 5 dilm * XXXXXXX
带 * 表示主节点

查看ES健康状况

GET _cat/health

查看主节点

GET _cat/master

查看所有索引

GET _cat/indices

查看映射

GET bank/_mapping

2. 索引一个文档

PUT

PUT customer/external/1
 {
 “name”:“张三”}
保存一个数据,保存在哪个索引下,指定用哪个唯一标识
 {
 “_index” : “customer”, //索引
 “_type” : “external”, //类型 +》 MySQL Table
 “_id” : “1”, //唯一主键
 “_version” : 1, //版本 乐观锁
 “result” : “created”, //结果 显示操作
 “_shards” : { //分片
 “total” : 2,
 “successful” : 1,
 “failed” : 0
 },
 “_seq_no” : 0,
 “_primary_term” : 1
 }
 原请求再次PUT
 {
 “_index” : “customer”,
 “_type” : “external”,
 “_id” : “1”,
 “_version” : 2, //版本变为2
 “result” : “updated”, //就会变成更新
 “_shards” : {
 “total” : 2,
 “successful” : 1,
 “failed” : 0
 },
 “_seq_no” : 1,
 “_primary_term” : 1
 }POST
POST customer/external/1
 {
 “name”:“张三”
 }
 //结果
 {
 “_index” : “customer”,
 “_type” : “external”,
 “_id” : “1”,
 “_version” : 3,
 “result” : “updated”,
 “_shards” : {
 “total” : 2,
 “successful” : 1,
 “failed” : 0
 },
 “_seq_no” : 2,
 “_primary_term” : 1
 }
 之前已经有主键为1的数据,所以此时做更新操作
 如果没有主键,或者主键不存在,则做新增操作POST customer/external/2
 {
 “name”:“张三”}
//结果
 {
 “_index” : “customer”,
 “_type” : “external”,
 “_id” : “2”,
 “_version” : 1,
 “result” : “created”,
 “_shards” : {
 “total” : 2,
 “successful” : 1,
 “failed” : 0
 },
 “_seq_no” : 3,
 “_primary_term” : 1
 }POST和PUT区别:
PUT的主键为空,则报400错误
3.查询文档
GET customer/external/1
{
 “_index” : “customer”, //哪个索引
 “_type” : “external”, //哪个类型
 “_id” : “1”, //主键
 “_version” : 3, //版本
 “_seq_no” : 2, //并发控制字段 每次更新 加 1 乐观锁
 “_primary_term” : 1, //同上 主分片重新分配 比如重启
 “found” : true, //是否找到
 “_source” : { //真正的数据
 “name” : “张三”
 }
 }如果id不存在
GET customer/external/3
 //结果
 {
 “_index” : “customer”,
 “_type” : “external”,
 “_id” : “3”,
 “found” : false
 }如果type不存在
GET customer/external121/3
 //结果
 {
 “_index” : “customer”,
 “_type” : “external121”,
 “_id” : “3”,
 “found” : false
 }如果索引不存在、
GET customer1021/external/3
 //结果
 {
 “error” : {
 “root_cause” : [
 {
 “type” : “index_not_found_exception”,
 “reason” : “no such index [customer1021]”,
 “resource.type” : “index_expression”,
 “resource.id” : “customer1021”,
 “index_uuid” : “na”,
 “index” : “customer1021”
 }
 ],
 “type” : “index_not_found_exception”,
 “reason” : “no such index [customer1021]”,
 “resource.type” : “index_expression”,
 “resource.id” : “customer1021”,
 “index_uuid” : “na”,
 “index” : “customer1021”
 },
 “status” : 404
 }4.更新文档
1.使用POST _update
POST customer/external/1/_update
 {
 “doc”:{
 “name”:“王五”
 }
 }
 //结果
 {
 “_index” : “customer”,
 “_type” : “external”,
 “_id” : “1”,
 “_version” : 4,
 “result” : “updated”,
 “_shards” : {
 “total” : 2,
 “successful” : 1,
 “failed” : 0
 },
 “_seq_no” : 4,
 “_primary_term” : 1
 }2.使用 POST
POST customer/external/1
 {"name":"张三"}
 //结果
 {
 “_index” : “customer”,
 “_type” : “external”,
 “_id” : “1”,
 “_version” : 5,
 “result” : “updated”,
 “_shards” : {
 “total” : 2,
 “successful” : 1,
 “failed” : 0
 },
 “_seq_no” : 5,
 “_primary_term” : 1
 }3.使用 PUT
PUT customer/external/1
 {"name":"张三"}
 //结果
 {
 “_index” : “customer”,
 “_type” : “external”,
 “_id” : “1”,
 “_version” : 11,
 “result” : “updated”,
 “_shards” : {
 “total” : 2,
 “successful” : 1,
 “failed” : 0
 },
 “_seq_no” : 11,
 “_primary_term” : 1
 }POST和POST(_update) 区别
运行两次
 POST customer/external/1/_update
 {
 “doc”:{
 “name”:“王五”
 }
 }
 //结果
 {
 “_index” : “customer”,
 “_type” : “external”,
 “_id” : “1”,
 “_version” : 6, //当数据一样时,版本并不会新增
 “result” : “noop”, //当数据一样时,并不会进行任何操作
 “_shards” : {
 “total” : 0,
 “successful” : 0,
 “failed” : 0
 },
 “_seq_no” : 6, //序列号也不会新增
 “_primary_term” : 1
 }
 运行两次
 POST customer/external/1
 {
 “name”:“张三”
 }
 //结果
 {
 “_index” : “customer”,
 “_type” : “external”,
 “_id” : “1”,
 “_version” : 8, //无论是否一致 都会新增
 “result” : “updated”, //一直是更新
 “_shards” : {
 “total” : 2,
 “successful” : 1,
 “failed” : 0
 },
 “_seq_no” : 8, //一直新增
 “_primary_term” : 1
 }
 PUT和POST基本一样
 POST(_update) 可以新增type的属性(也会对比属性)
 POST customer/external/1/_update
 {
 “doc”:{
 “name”:“王五”,
 “age”:“15”
 }
 }
 //结果
 {
 “_index” : “customer”,
 “_type” : “external”,
 “_id” : “1”,
 “_version” : 12,
 “result” : “updated”,
 “_shards” : {
 “total” : 2,
 “successful” : 1,
 “failed” : 0
 },
 “_seq_no” : 12,
 “_primary_term” : 1
 }
 //查看主键为1 的数据 //参考上面的GET 语句
 {
 “_index” : “customer”,
 “_type” : “external”,
 “_id” : “1”,
 “_version” : 12,
 “_seq_no” : 12,
 “_primary_term” : 1,
 “found” : true,
 “_source” : {
 “name” : “王五”,
 “age” : “15” //已经多个age
 }
 }5.删除文档&索引
删除文档
DELETE customer/external/1
 //结果
 {
 “_index” : “customer”,
 “_type” : “external”,
 “_id” : “1”,
 “_version” : 14,
 “result” : “deleted”,
 “_shards” : {
 “total” : 2,
 “successful” : 1,
 “failed” : 0
 },
 “_seq_no” : 14,
 “_primary_term” : 1
 }删除索引
DELETE customer
 //结果
 {
 “acknowledged” : true
 }删除之后再次查询 返回 404
{
 “error” : {
 “root_cause” : [
 {
 “type” : “index_not_found_exception”,
 “reason” : “no such index [customer]”,
 “resource.type” : “index_expression”,
 “resource.id” : “customer”,
 “index_uuid” : “na”,
 “index” : “customer”
 }
 ],
 “type” : “index_not_found_exception”,
 “reason” : “no such index [customer]”,
 “resource.type” : “index_expression”,
 “resource.id” : “customer”,
 “index_uuid” : “na”,
 “index” : “customer”
 },
 “status” : 404
 }6.批量API
批量添加
POST /customer/external/_bulk
 {“index”:{"_id":“1”}}
 {“name”:“张三”}
 {“index”:{"_id":“2”}}
 {“name”:“李四”}//结果
 {
 “took” : 72, //耗时
 “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
 }
 }
 ]
 }复杂批量
POST /_bulk
 {“delete”:{"_index":“test”,"_type":“blog”,"_id":“123”}}
 {“create”:{"_index":“test”,"_type":“blog”,"_id":“123”}}
 {“title”:“测试”}
 {“index”:{"_index":“test”,"_type":“blog”}}
 {“title”:“测试添加”}
 {“update”:{"_index":“test”,"_type":“blog”,"_id":“123”}}
 {“doc”:{“title”:“测试修改”}}//结果
 {
 “took” : 103,
 “errors” : false,
 “items” : [
 {
 “delete” : {
 “_index” : “test”,
 “_type” : “blog”,
 “_id” : “123”,
 “_version” : 1,
 “result” : “not_found”,
 “_shards” : {
 “total” : 2,
 “successful” : 1,
 “failed” : 0
 },
 “_seq_no” : 0,
 “_primary_term” : 1,
 “status” : 404
 }
 },
 {
 “create” : {
 “_index” : “test”,
 “_type” : “blog”,
 “_id” : “123”,
 “_version” : 2,
 “result” : “created”,
 “_shards” : {
 “total” : 2,
 “successful” : 1,
 “failed” : 0
 },
 “_seq_no” : 1,
 “_primary_term” : 1,
 “status” : 201
 }
 },
 {
 “index” : {
 “_index” : “test”,
 “_type” : “blog”,
 “_id” : “hRqChX4B-JhnWdV4pbVG”,
 “_version” : 1,
 “result” : “created”,
 “_shards” : {
 “total” : 2,
 “successful” : 1,
 “failed” : 0
 },
 “_seq_no” : 2,
 “_primary_term” : 1,
 “status” : 201
 }
 },
 {
 “update” : {
 “_index” : “test”,
 “_type” : “blog”,
 “_id” : “123”,
 “_version” : 3,
 “result” : “updated”,
 “_shards” : {
 “total” : 2,
 “successful” : 1,
 “failed” : 0
 },
 “_seq_no” : 3,
 “_primary_term” : 1,
 “status” : 200
 }
 }
 ]
 }导入官方测试数据
导入语句: POST /bank/account/_bulk
 下载地址: https://download.elastic.co/demos/kibana/gettingstarted/accounts.zip进阶检索
基于URI检索
GET bank/_search?q=*&sort=account_number:asc
基于Rest检索
查询所有 match_all
GET _search
 {
 “query”: {
 “match_all”: {}
 }
 }基于balance排序
GET _search
 {
 “query”: {
 “match_all”: {}
 },
 “sort”: [
 {
 “balance”: {
 “order”: “desc”
 }
 }
 ]
 }基于balance排序 分页
GET _search
 {
 “query”: {
 “match_all”: {}
 },
 “from”: 0, //开始
 “size”: 5, //每页条数
 “sort”: [
 {
 “balance”: {
 “order”: “desc”
 }
 }
 ]
 }返回部分字段
GET _search
 {
 “query”: {
 “match_all”: {}
 },
 “from”: 0,
 “size”: 5,
 “_source”: “firstname”, //指定要返回的字段
 “sort”: [
 {
 “balance”: {
 “order”: “desc”
 }
 }
 ]
 }精确匹配 match
匹配 balance 为 20 的数据
GET _search
 {
 “query”: {
 “match”: {
 “balance”: “20”
 }
 }
 }
 //结果
 {
 “took” : 1,
 “timed_out” : false,
 “_shards” : {
 “total” : 6,
 “successful” : 6,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 1,
 “relation” : “eq”
 },
 “max_score” : 1.0, //最大得分
 “hits” : [
 {
 “_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”
 }
 }
 ]
 }
 }也可以使用 keyword
 GET _search
 {
 “query”: {
 “match”: {
 “address.keyword”:“789 Madison”
 }
 }}
 //结果
 {
 “took” : 0,
 “timed_out” : false,
 “_shards” : {
 “total” : 6,
 “successful” : 6,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 0,
 “relation” : “eq”
 },
 “max_score” : null,
 “hits” : [ ]
 }
 }区别:
keyword 不分词匹配
 match 分词匹配 a b 会匹配 a /b /ab
 term 适合不是text类型 比如数值全文匹配
会对条件进行分词匹配,只要包含其中一个就会查询出来
 GET _search
 {
 “query”: {
 “match”: {
 “address”: “kings”
 }
 }
 }
 //结果
 {
 “took” : 1,
 “timed_out” : false,
 “_shards” : {
 “total” : 6,
 “successful” : 6,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 2,
 “relation” : “eq”
 },
 “max_score” : 5.9908285,
 “hits” : [
 {
 “_index” : “bank”,
 “_type” : “account”,
 “_id” : “20”,
 “_score” : 5.9908285,
 “_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” : “722”,
 “_score” : 5.9908285,
 “_source” : {
 “account_number” : 722,
 “balance” : 27256,
 “firstname” : “Roberts”,
 “lastname” : “Beasley”,
 “age” : 34,
 “gender” : “F”,
 “address” : “305 Kings Hwy”,
 “employer” : “Quintity”,
 “email” : “robertsbeasley@quintity.com”,
 “city” : “Hayden”,
 “state” : “PA”
 }
 }
 ]
 }
 }match_phrase [短语匹配]
将需要匹配的值当成一个整体单词进行检索
 GET _search
 {
 “query”: {
 “match_phrase”: {
 “address”: “mill road”
 }
 }
 }
 //结果
 {
 “took” : 6,
 “timed_out” : false,
 “_shards” : {
 “total” : 6,
 “successful” : 6,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 1,
 “relation” : “eq”
 },
 “max_score” : 8.926605,
 “hits” : [
 {
 “_index” : “bank”,
 “_type” : “account”,
 “_id” : “970”,
 “_score” : 8.926605,
 “_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”
 }
 }
 ]
 }
 }
 查出address中包含mill road 的所有记录,并给出相关性得分multi_match [多字段匹配]
GET _search
 {
 “query”: {
 “multi_match”: {
 “query”: “mill m”,
 “fields”: [“address”]
 }
 }
 }
 //结果
 {
 “took” : 1,
 “timed_out” : false,
 “_shards” : {
 “total” : 6,
 “successful” : 6,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 4,
 “relation” : “eq”
 },
 “max_score” : 5.4032025,
 “hits” : [
 {
 “_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”
 }
 },
 {
 “_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”
 }
 }
 ]
 }
 }state 或者 address 包含 mill 或者 m 任意一个
must [必须同时满足]
GET _search
 {
 “query”: {
 “bool”: {
 “must”: [
 {
 “match”: {
 “address”: “mill”
 }},
    {
      "match": {
        "age": "10"
      }
    }
  ]
}}
 }
 //结果
 {
 “took” : 4,
 “timed_out” : false,
 “_shards” : {
 “total” : 6,
 “successful” : 6,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 0,
 “relation” : “eq”
 },
 “max_score” : null,
 “hits” : [ ]
 }
 }must_not [必须同时不满足]
GET _search
 {
 “query”: {
 “bool”: {
 “must”: [
 {
 “match”: {
 “address”: “mill”
 }}
  ],
  "must_not": [
    {"match": {
      "age": "20"
    }}
  ]
}}
 }
 //结果
 {
 “took” : 1,
 “timed_out” : false,
 “_shards” : {
 “total” : 6,
 “successful” : 6,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 4,
 “relation” : “eq”
 },
 “max_score” : 5.4032025,
 “hits” : [
 {
 “_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”
 }
 },
 {
 “_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”
 }
 }
 ]
 }
 }should[应该满足]
满足这个条件的分数会搞
 GET _searchGET _search
 {
 “query”: {
 “bool”: {
 “must”: [
 {
 “match”: {
 “address”: “mill”
 }}
  ],
  "must_not": [
    {"match": {
      "age": "20"
    }}
  ],
  "should": [
    {"match": {
      "firstname": "Winnie"
    }}
  ]
}}
 }
 //结果
 {
 “took” : 1,
 “timed_out” : false,
 “_shards” : {
 “total” : 6,
 “successful” : 6,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 4,
 “relation” : “eq”
 },
 “max_score” : 11.906492, //未加should 5.多
 “hits” : [
 {
 “_index” : “bank”,
 “_type” : “account”,
 “_id” : “136”,
 “_score” : 11.906492,
 “_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”
 }
 }
 ]
 }
 }filter[过滤]
和must 差不多只是没有相关的得分信息
 对比:
 must:
 GET _search
 {
 “query”: {
 “bool”: {
 “must”: [
 {
 “match”: {
 “address”: “mill”
 }
 }
 ],
 “must_not”: [
 {
 “match”: {
 “age”: “20”
 }
 }
 ],
 “should”: [
 {
 “match”: {
 “firstname”: “Winnie”
 }
 }
 ]
 }
 },
 “from”: 0,
 “size”: 1
 }
 //结果
 {
 “took” : 0,
 “timed_out” : false,
 “_shards” : {
 “total” : 6,
 “successful” : 6,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 4,
 “relation” : “eq”
 },
 “max_score” : 11.906492,
 “hits” : [
 {
 “_index” : “bank”,
 “_type” : “account”,
 “_id” : “136”,
 “_score” : 11.906492, //得分
 “_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”
 }
 }
 ]
 }
 }filter:
GET _search
 {
 “query”: {
 “bool”: {
 “must_not”: [
 {
 “match”: {
 “age”: “20”
 }
 }
 ],
 “should”: [
 {
 “match”: {
 “firstname”: “Winnie”
 }
 }
 ],
 “filter”: {
 “term”: {
 “address”: “mill”
 }}
}},
 “from”: 0,
 “size”: 1
 }
 //结果
 {
 “took” : 2,
 “timed_out” : false,
 “_shards” : {
 “total” : 6,
 “successful” : 6,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 4,
 “relation” : “eq”
 },
 “max_score” : 6.5032897,
 “hits” : [
 {
 “_index” : “bank”,
 “_type” : “account”,
 “_id” : “136”,
 “_score” : 6.5032897, //并没有加上分数
 “_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”
 }
 }
 ]
 }
 }term [精确值使用term 比如数值]
GET _search
 {
 “query”: {
 “term”: {
 “age”:28
 }
 },
 “from”: 0
 , “size”: 1
 }
 //结果
 {
 “took” : 0,
 “timed_out” : false,
 “_shards” : {
 “total” : 6,
 “successful” : 6,
 “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”
 }
 }
 ]
 }
 }
 注意:如果使用term查询text类型,查询不到聚合查询
搜索address包含mill所有人的年龄分布以及平均工资
GET _search
 {
 “query”: {
 “match”: {
 “address”: “mill”
 }
 },
 “size”: 0,
 “aggs”: {
 “ageAge”: {
 “terms”: {
 “field”: “age”,
 “size”: 10
 }
 },
 “ageAvg”: {
 “avg”: {
 “field”: “age”
 }
 }
 }
 }
 //结果
 {
 “took” : 2,
 “timed_out” : false,
 “_shards” : {
 “total” : 6,
 “successful” : 6,
 “skipped” : 0,
 “failed” : 0
 },
 “hits” : {
 “total” : {
 “value” : 4,
 “relation” : “eq”
 },
 “max_score” : null,
 “hits” : [ ]
 },
 “aggregations” : {
 “ageAge” : {
 “doc_count_error_upper_bound” : 0,
 “sum_other_doc_count” : 0,
 “buckets” : [
 {
 “key” : 38,
 “doc_count” : 2
 },
 {
 “key” : 28,
 “doc_count” : 1
 },
 {
 “key” : 32,
 “doc_count” : 1
 }
 ]
 },
 “ageAvg” : {
 “value” : 34.0
 }
 }
 }按照年龄聚合,并且请求这些年龄段的这些人的平均薪资
GET _search
 {
 “aggs”: {
 “ageAge”: {
 “terms”: {
 “field”: “age”,
 “size”: 1000
 },
 “aggs”: {
 “ageAvg”: {
 “avg”: {
 “field”: “balance”
 }
 }
 }
 }
 }
 }查出所有年龄分布,并且这些年龄段中 M 的平均薪资和 F 的平均薪资以及这个年龄段的总体平均薪资
GET _search
 {
 “aggs”: {
 “ageAge”: {
 “terms”: {
 “field”: “age”,
 “size”: 1000
 },
 “aggs”: {
 “fm”: {
 “terms”: {
 “field”: “gender.keyword”,
 “size”: 1000
 },
 “aggs”: {
 “fm”: {
 “terms”: {
 “field”: “gender.keyword”,
 “size”: 1000
 },
 “aggs”: {
 “ageAvg”: {
 “avg”: {
 “field”: “balance”
 }
 }
 }
 }
 }
 }
 }
 }
 }
 }创建映射
PUT /my_index
 {
 “mappings”: {"properties": {
  "age":{"type": "integer"},
  "email":{"type": "keyword"},
  "name":{"type": "text"}
}}
 }
 //结果
 {
 “acknowledged” : true,
 “shards_acknowledged” : true,
 “index” : “my_index”
 }新增属性
PUT /my_index/_mapping
 {
 “properties”: {
 “id”: {
 “type”: “long”
 }
 }
 }
 /结果
 {
 “acknowledged” : true
 }更新属性
只能做数据迁移
数据迁移
6.0之后
 POST _reindex
 {
 “source”: {
 “index”: “bank”
 },
 “dest”: {
 “index”: “newbank”
 }
 }
 6.0之前
 POST _reindex
 {
 “source”: {
 “index”: “bank”,
 “type”:“account”
 },
 “dest”: {
 “index”: “newbank”
 }
 }分词
POST _analyze
 {
 “analyzer”: “standard”,
 “text”: “天选姬牛皮”}
 //结果“tokens” : [
 {
 “token” : “天”,
 “start_offset” : 0,
 “end_offset” : 1,
 “type” : “”,
 “position” : 0
 },
 {
 “token” : “选”,
 “start_offset” : 1,
 “end_offset” : 2,
 “type” : “”,
 “position” : 1
 },
 {
 “token” : “姬”,
 “start_offset” : 2,
 “end_offset” : 3,
 “type” : “”,
 “position” : 2
 },
 {
 “token” : “牛”,
 “start_offset” : 3,
 “end_offset” : 4,
 “type” : “”,
 “position” : 3
 },
 {
 “token” : “皮”,
 “start_offset” : 4,
 “end_offset” : 5,
 “type” : “”,
 “position” : 4
 }
 ]
 }
 对中文不友好所以需要安装ik分词器
 IK分词器地址https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.4.2 放到 plugins /ik 目录下
POST _analyze
 {
 “analyzer”: “ik_smart”,
 “text”: “华硕笔记本天选2”}
 //结果
 {
 “tokens” : [
 {
 “token” : “华硕”,
 “start_offset” : 0,
 “end_offset” : 2,
 “type” : “CN_WORD”,
 “position” : 0
 },
 {
 “token” : “笔记本”,
 “start_offset” : 2,
 “end_offset” : 5,
 “type” : “CN_WORD”,
 “position” : 1
 },
 {
 “token” : “天”,
 “start_offset” : 5,
 “end_offset” : 6,
 “type” : “CN_CHAR”,
 “position” : 2
 },
 {
 “token” : “选”,
 “start_offset” : 6,
 “end_offset” : 7,
 “type” : “CN_CHAR”,
 “position” : 3
 },
 {
 “token” : “2”,
 “start_offset” : 7,
 “end_offset” : 8,
 “type” : “ARABIC”,
 “position” : 4
 }
 ]
 }

自定义词库

es 查询某个字段值不等于0 es判断字段是否存在_elasticsearch