es 的基操

1、index基本操作

1)创建索引:PUT /index?pretty
(2)查询索引:GET _cat/indices?v
(3)删除索引:DELETE /index?pretty
(4)插入数据:
PUT /index/_doc/id
{
    Json数据
}51) 全量替换
2) 指定字段更新
(6)删除数据 DELETE /index/type/id

2、mapping基本操作

# Dynamic mapping
DELETE product_mapping
GET product_mapping/_mapping
PUT /product_mapping/_doc/1
{
  "name": "xiaomi phone",
  "desc": "shouji zhong de zhandouji",
  "count": 123456,
  "price": 123.123,
  "date": "2020-05-20",
  "isdel": false,
  "tags": [
    "xingjiabi",
    "fashao",
    "buka"
  ]
}

#手工创建mapping(fields的mapping只能创建,无法修改)
#语法
GET product/_mapping
PUT /product
{
  "mappings": {
    "properties": {
      "date": {
        "type": "text"
      }
    }
  }
}

GET product/_mapping
#1 index

#案例
PUT /product
{
  "mappings": {
    "properties": {
      "date": {
        "type": "text"
      },
      "desc": {
        "type": "text",
        "analyzer": "english"
      },
      "name": {
        "type": "text",
        "index": "false"
      },
      "price": {
        "type": "long"
      },
      "tags": {
        "type": "text",
        "index": "true"
      },
      "parts": {
        "type": "object"
      },
      "partlist": {
        "type": "nested"
      }
    }
  }
}
#插入数据
GET product/_mapping
PUT /product/_doc/1
{
  "name": "xiaomi phone",
  "desc": "shouji zhong de zhandouji",
  "count": 123456,
  "price": 3999,
  "date": "2020-05-20",
  "isdel": false,
  "tags": [
    "xingjiabi",
    "fashao",
    "buka"
  ],
  "parts": {
    "name": "adapter",
    "desc": "5V 2A"
  },
  "partlist": [
    {
      "name": "adapter",
      "desc": "5V 2A"
    },
    {
      "name": "USB-C",
      "desc": "5V 2A 1.5m"
    },
    {
      "name": "erji",
      "desc": "boom"
    }
  ]
}
#查看
GET /product/_search
{
  "query": {
    "match_all": {}
  }
}
#验证
GET /product/_search
{
  "query": {
    "match": {
      "name": "xiaomi"
    }
  }
}

#copy_to
PUT copy_to
{
  "mappings": {
    "properties": {
      "field1": {
        "type": "text",
        "copy_to": "field_all" 
      },
      "field2": {
        "type": "text",
        "copy_to": "field_all" 
      },
      "field_all": {
        "type": "text"
      }
    }
  }
}

PUT copy_to/_doc/1
{
  "field1": "field1",
  "field2": "field2"
}
GET copy_to/_search
GET copy_to/_search
{
  "query": {
    "match": {
      "field_all": { 
        "query": "field1 field2"
      }
    }
  }
}

#coerce:是否允许强制类型转换
PUT coerce
{
  "mappings": {
    "properties": {
      "number_one": {
        "type": "integer"
      },
      "number_two": {
        "type": "integer",
        "coerce": false
      }
    }
  }
}
PUT coerce/_doc/1
{
  "number_one": "10" 
}
#//拒绝,因为设置了false
PUT coerce/_doc/2
{
  "number_two": "10" 
}  

DELETE coerce
PUT coerce
{
  "settings": {
    "index.mapping.coerce": false
  },
  "mappings": {
    "properties": {
      "number_one": {
        "type": "integer",
        "coerce": true
      },
      "number_two": {
        "type": "integer"
      }
    }
  }
}
PUT coerce/_doc/1
{ 
  "number_one": "10" 
} 
#拒绝,因为设置了false
PUT coerce/_doc/2
{
  "number_two": "10" 
  
} 

PUT /product/_mapping
{
  "properties": {
    "date": {
      "type": "text"
    }
  }
}

#7- 7 
PUT dynamic
{
  "mappings": {
    "dynamic": false,
    "properties": {
      "user": {
        "properties": {
          "date": {
            "type": "text"
          },
          "desc": {
            "type": "text",
            "analyzer": "english"
          },
          "name": {
            "type": "text",
            "index": "false"
          },
          "price": {
            "type": "long"
          }
        }
      }
    }
  }
}
PUT /product/_mapping
{
  "properties": {
    "date": {
      "type": "text"
    }
  }
}

#7-11 
GET /product/_mapping
#给city创建一个keyword
PUT fields_test
{
  "mappings": {
    "properties": {
      "city": {
        "type": "text",
        "fields": {
          "raw": { 
            "type":  "keyword"
          }
        }
      }
    }
  }
}

PUT fields_test/_doc/1
{
  "city": "New York"
}

PUT fields_test/_doc/2
{
  "city": "York"
}
GET fields_test/_mapping
GET fields_test/_search
{
  "query": {
    "match": {
      "city": "york" 
    }
  },
  "sort": {
    "city.raw": "asc" 
  },
  "aggs": {
    "cities": {
      "terms": {
        "field": "city.raw" 
      }
    }
  }
}

#忽略类型错误-常用于数据同步
PUT ignore_malformed
{
  "mappings": {
    "properties": {
      "number_one": {
        "type": "integer",
        "ignore_malformed": true
      },
      "number_two": {
        "type": "integer"
      }
    }
  }}
PUT ignore_malformed/_doc/1
{
  "text":       "Some text value",
  "number_one": "foo" 
  
}   
#//虽然有异常 但是不抛出
PUT ignore_malformed/_doc/2
{
  "text":       "Some text value",
  "number_two": "foo" 
  
}  
GET my_index/_search
#//数据格式不对	


#fielddata
#每个tag产品的数量   "size":0, 不显示原始结果
GET /product/_search
{
  "aggs": {
    "tag_agg_group": {
      "terms": {
        "field": "tags"
      }
    }
  },
  "size":0
}
GET /product/_mapping
#将文本field的fielddata属性设置为true
PUT /product/_mapping
{
  "properties": {
    "tags": {
      "type": "text",
      "fielddata": true
    }
  }
}

3、搜索和查询的基操

DELETE product
PUT /product/_doc/1
{
    "name" : "xiaomi phone",
    "desc" :  "shouji zhong de zhandouji",
    "date": "2021-06-01",
    "price" :  3999,
    "tags": [ "xingjiabi", "fashao", "buka" ]
}
PUT /product/_doc/2
{
    "name" : "xiaomi nfc phone",
    "desc" :  "zhichi quangongneng nfc,shouji zhong de jianjiji",
    "date": "2021-06-02",
    "price" :  4999,
    "tags": [ "xingjiabi", "fashao", "gongjiaoka" ]
}
PUT /product/_doc/3
{
    "name" : "nfc phone",
    "desc" :  "shouji zhong de hongzhaji",
    "date": "2021-06-03",
    "price" :  2999,
    "tags": [ "xingjiabi", "fashao", "menjinka" ]
}
PUT /product/_doc/4
{
    "name" : "xiaomi erji",
    "desc" :  "erji zhong de huangmenji",
    "date": "2021-04-15",
    "price" :  999,
    "tags": [ "low", "bufangshui", "yinzhicha" ]
}
PUT /product/_doc/5
{
    "name" : "hongmi erji",
    "desc" :  "erji zhong de kendeji 2021-06-01",
    "date": "2021-04-16",
    "price" :  399,
    "tags": [ "lowbee", "xuhangduan", "zhiliangx" ]
}
GET product/_search
GET product/_search
{
  "_source": false, 
  "query": {
    "match_all": {}
  }
}
PUT /product2/_doc/1
{
  "owner":{
    "name":"zhangsan",
    "sex":"男",
    "age":18
  },
  "name": "hongmi erji",
  "desc": "erji zhong de kendeji",
  "price": 399,
  "tags": [
    "lowbee",
    "xuhangduan",
    "zhiliangx"
  ]
}
PUT product2
{
  "mappings": {
    "_source": {
      "includes": [
        "name",
        "price"
      ],
      "excludes": [
        "desc",
        "tags"
      ]
    }
  }
}
DELETE product2
GET product2/_search
{
  "_source": ["owner.name","owner.sex"], 
  "query": {
    "match_all": {}
  }
}
GET product2/_search
GET product2/_search
{
  "_source": {
    "includes": [
      "owner.*",
      "name"
    ],
    "excludes": [
      "name", 
      "desc",
      "price"
    ]
  },
  "query": {
    "match_all": {}
  }
}
GET _search

# DSL

# match 单个字段匹配

GET product/_search
{
  "query": {
    "match": {
      "name": "xiaomi nfc phone"
    }
  }
}

# match_all 查询所有

GET product/_search
{
  "query": {
    "match_all": {}
  }
}

# select field1,field2 from table where a=xx and b=xx

# multi_match 多个字段匹配

GET product/_search
{
  "query": {
    "multi_match": {
      "query": "phone huangmenji",
      "fields": ["name","desc"]
    }
  }
}

# match_phrase 

GET product/_mapping
GET product/_search
{
  "query": {
    "match_phrase": {
      "name": "xiaomi nfc"
    }
  }
}

GET _analyze
{
  "analyzer": "standard",
  "text": "xiaomi nfc phone"
}

# Term

GET product/_search
{
  "query": {
    "match": {
      "name": "xiaomi phone"
    }
  }
}
GET product/_search
{
  "query": {
    "term": {
      "name": "xiaomi phone"
    }
  }
}
GET product/_search
{
  "query": {
    "term": {
      "name": "xiaomi phone"
    }
  }
}
#term和match_phrase区别
GET product/_search
{
  "query": {
    "match_phrase": {
      "name": "xiaomi phone"
    }
  }
}
#term和keyword的区别
GET product/_mapping
GET product/_search
{
  "query": {
    "term": {
      "name": "xiaomi phone"
    }
  }
}
GET product/_search
{
  "query": {
    "term": {
      "name.keyword": "xiaomi phone"
    }
  }
}

# terms

GET product/_search
{
  "query": {
    "terms": {
      "tags": [ "lowbee", "gongjiaoka" ],
      "boost": 1.0
    }
  }
}

# range

GET /_search
{
  "query": {
    "range": {
      "age": {
        "gte": 10,
        "lte": 20,
        "boost": 2.0
      }
    }
  }
}
GET product/_search
GET product/_search
{
  "query": {
    "range": {
      "date": {
        "gte": "2021-04-15",
        "lt": "2021-04-16"
      }
    }
  }
}
GET product/_search
{
  "query": {
    "range": {
      "date": {
        "gte": "now-1d/d",
        "lt": "now/d"
      }
    }
  }
}
GET product/_search
{
  "query": {
    "range": {
      "date": {
        "time_zone": "+08:00",        
        "gte": "2021-04-15T08:00:00", 
        "lt": "now"                  
      }
    }
  }
}

# filter

GET product/_search
GET product/_search
{
  "query": {
    "match": {
      "name": "phone"
    }
  }
}
GET product/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "name": "phone"
        }
      },
      "boost": 1.2
    }
  }
}
GET product/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "name": "phone"
        }
      }
    }
  }
}

# bool query 组合查询

#must 计算相关度得分
#条件1:包含"xiaomi""phone"
#条件2:包含"shouji zhong"


GET product/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "xiaomi phone"
          }
        },
        {
          "match_phrase": {
            "desc": "shouji zhong"
          }
        }
      ]
    }
  }
}
#filter 不计算相关度得分
GET product/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "match": {
            "name": "xiaomi phone"
          }
        },
        {
          "match_phrase": {
            "desc": "shouji zhong"
          }
        }
      ]
    }
  }
}
#must not 不计算相关度得分
#条件1: 排除包含xiaomi的和包含nfc的(不能包含xiaomi和nfc中的任意一个)
#条件2: 排除价格大于等于500GET product/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "name": "xiaomi nfc"
          }
        },
        {
          "range": {
            "price": {
              "gte": "500"
            }
          }
        }
      ]
    }
  }
}
#should
GET product/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "name": "xiaomi nfc"
          }
        },
        {
          "range": {
            "price": {
              "lte": "500"
            }
          }
        }
      ]
    }
  }
}
#组合查询
GET product/_search
{
  "_source": false,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "xiaomi"
          }
        }
      ]
    }
  }
}
GET product/_search
{
  "_source": false, 
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "price": {
              "lte": "1000"
            }
          }
        }
      ]
    }
  }
}
#filter和must组合
GET product/_search
{
  "_source": false, 
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "price": {
              "lte": "1000"
            }
          }
        }
      ],
      "must": [
        {
          "match": {
            "name": "xiaomi"
          }
        }
      ]
    }
  }
}
GET product/_search
{
  "_source": false, 
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "xiaomi"
          }
        },
        {
          "range": {
            "price": {
              "lte": "1000"
            }
          }
        }
      ]
    }
  }
}
#(must或者filter)和should组合
#条件1:价格小于10000
#条件2:name中包含"hongmi"或者"xiaomi nfc phone"
GET product/_search
{
  "_source": false,
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "price": {
              "lte": "10000"
            }
          }
        }
      ],
      "should": [
        {
          "match_phrase": {
            "name": "nfc phone"
          }
        },
        {
          "match": {
            "name": "erji"
          }
        },
        {
          "bool": {
            "must": [
              {
                "range": {
                  "price": {
                    "gte": 900,
                    "lte": 3000
                  }
                }
              }
            ]
          }
        }
      ],
      "minimum_should_match": 2
    }
  }
}

#filter 不计算相关度得分
GET product/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "match": {
            "name": "xiaomi phone"
          }
        },
        {
          "match_phrase": {
            "desc": "shouji zhong"
          }
        }
      ]
    }
  }
}
#must not 不计算相关度得分
#条件1: 排除包含xiaomi的和包含nfc的(不能包含xiaomi和nfc中的任意一个)
#条件2: 排除价格大于等于500GET product/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "name": "xiaomi nfc"
          }
        },
        {
          "range": {
            "price": {
              "gte": "500"
            }
          }
        }
      ]
    }
  }
}
#should
GET product/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "name": "xiaomi nfc"
          }
        },
        {
          "range": {
            "price": {
              "lte": "500"
            }
          }
        }
      ]
    }
  }
}
#组合查询
GET product/_search
{
  "_source": false,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "xiaomi"
          }
        }
      ]
    }
  }
}
GET product/_search
{
  "_source": false, 
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "price": {
              "lte": "1000"
            }
          }
        }
      ]
    }
  }
}
#filter和must组合
GET product/_search
{
  "_source": false, 
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "price": {
              "lte": "1000"
            }
          }
        }
      ],
      "must": [
        {
          "match": {
            "name": "xiaomi"
          }
        }
      ]
    }
  }
}
GET product/_search
{
  "_source": false, 
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "xiaomi"
          }
        },
        {
          "range": {
            "price": {
              "lte": "1000"
            }
          }
        }
      ]
    }
  }
}
#(must或者filter)和should组合
#条件1:价格小于10000
#条件2:name中包含"hongmi"或者"xiaomi nfc phone"
GET product/_search
{
  "_source": false,
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "price": {
              "lte": "10000"
            }
          }
        }
      ],
      "should": [
        {
          "match_phrase": {
            "name": "nfc phone"
          }
        },
        {
          "match": {
            "name": "erji"
          }
        },
        {
          "bool": {
            "must": [
              {
                "range": {
                  "price": {
                    "gte": 900,
                    "lte": 3000
                  }
                }
              }
            ]
          }
        }
      ],
      "minimum_should_match": 2
    }
  }
}

4、分词器

# 分词器
#normalization
GET _analyze
{
  "text": "Mr. Ma is an excellent teacher",
  "analyzer": "english"
}

#character filter
##HTML Strip Character Filter
###测试数据<p>I&apos;m so <a>happy</a>!</p>
DELETE my_index
PUT my_index
{
  "settings": {
    "analysis": {
      "char_filter": {
        "my_char_filter":{
          "type":"html_strip",
          "escaped_tags":["a"]
        }
      },
      "analyzer": {
        "my_analyzer":{
          "tokenizer":"keyword",
          "char_filter":["my_char_filter"]
        }
      }
    }
  }
}
GET my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "<p>I&apos;m so <a>happy</a>!</p>"
}
##Mapping Character Filter 
DELETE my_index
PUT my_index
{
  "settings": {
    "analysis": {
      "char_filter": {
        "my_char_filter":{
          "type":"mapping",
          "mappings":[
            "滚 => *",
            "垃 => *",
            "圾 => *"
            ]
        }
      },
      "analyzer": {
        "my_analyzer":{
          "tokenizer":"keyword",
          "char_filter":["my_char_filter"]
        }
      }
    }
  }
}
GET my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "你就是个垃圾!滚"
}
##Pattern Replace Character Filter 
#17611001200
DELETE my_index
PUT my_index
{
  "settings": {
    "analysis": {
      "char_filter": {
        "my_char_filter":{
          "type":"pattern_replace",
          "pattern":"(\\d{3})\\d{4}(\\d{4})",
          "replacement":"$1****$2"
        }
      },
      "analyzer": {
        "my_analyzer":{
          "tokenizer":"keyword",
          "char_filter":["my_char_filter"]
        }
      }
    }
  }
}
GET my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "您的手机号是17611001200"
}

#************************************************
#token filter
DELETE test_index
PUT /test_index
{
  "settings": {
      "analysis": {
        "filter": {
          "my_synonym": {
            "type": "synonym_graph",
            "synonyms_path": "analysis/synonym.txt"
          }
        },
        "analyzer": {
          "my_analyzer": {
            "tokenizer": "ik_max_word",
            "filter": [ "my_synonym" ]
          }
        }
      }
  }
}
GET test_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": ["蒙丢丢,大G,霸道,daG"]
}
GET test_index/_analyze
{
  "analyzer": "ik_max_word",
  "text": ["奔驰G级"]
}
DELETE test_index
PUT /test_index
{
  "settings": {
      "analysis": {
        "filter": {
          "my_synonym": {
            "type": "synonym",
            "synonyms": ["赵,钱,孙,李=>吴","周=>王"]
          }
        },
        "analyzer": {
          "my_analyzer": {
            "tokenizer": "standard",
            "filter": [ "my_synonym" ]
          }
        }
      }
  }
}
GET test_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": ["赵,钱,孙,李","周"]
}
#大小写
GET test_index/_analyze
{
  "tokenizer": "standard",
  "filter": ["lowercase"], 
  "text": ["AASD ASDA SDASD ASDASD"]
}
GET test_index/_analyze
{
  "tokenizer": "standard",
  "filter": ["uppercase"], 
  "text": ["asdasd asd asg dsfg gfhjsdf asfdg g"]
}

GET test_index/_analyze
{
  "tokenizer": "standard",
  "filter": {
    "type": "condition",
    "filter":"uppercase",
    "script": {
      "source": "token.getTerm().length() < 5"
    }
  }, 
  "text": ["asdasd asd asg dsfg gfhjsdf asfdg g"]
}

#停用词
DELETE test_index
PUT /test_index
{
  "settings": {
      "analysis": {
        "analyzer": {
          "my_analyzer": {
            "type": "standard",
            "stopwords":["me","you"]
          }
        }
      }
  }
}
GET test_index/_analyze
{
  "analyzer": "my_analyzer", 
  "text": ["Teacher me and you in the china"]
}


#分词器 tokenizer
GET test_index/_analyze
{
  "tokenizer": "ik_max_word",
  "text": ["我爱北京天安门","天安门上太阳升"]
}

#自定义分词器
DELETE custom_analysis
PUT custom_analysis
{
  "settings": {
    "analysis": {
      "char_filter": {
        "my_char_filter": {
          "type": "mapping",
          "mappings": [
            "& => and",
            "| => or"
          ]
        },
        "html_strip_char_filter":{
          "type":"html_strip",
          "escaped_tags":["a"]
        }
      },
      "filter": {
        "my_stopword": {
          "type": "stop",
          "stopwords": [
            "is",
            "in",
            "the",
            "a",
            "at",
            "for"
          ]
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "pattern",
          "pattern": "[ ,.!?]"
        }
      }, 
      "analyzer": {
        "my_analyzer":{
          "type":"custom",
          "char_filter":["my_char_filter","html_strip_char_filter"],
          "filter":["my_stopword","lowercase"],
          "tokenizer":"my_tokenizer"
        }
      }
    }
  }
}

GET custom_analysis/_analyze
{
  "analyzer": "my_analyzer",
  "text": ["What is ,<a>as.df</a>  ss<p> in ? &</p> | is ! in the a at for "]
}

GET product/_mapping
GET product/_search

GET custom_analysis/_analyze
{
  "analyzer": "ik_max_word",
  "text": ["我爱中华人民共和国"]
}

GET custom_analysis/_analyze
{
  "analyzer": "ik_max_word",
  "text": ["蒙丢丢","大G","霸道","渣男","渣女","奥巴马"]
}

GET custom_analysis/_analyze
{
  "analyzer": "ik_max_word",
  "text": ["吴磊","美国","日本","澳大利亚"]
}

5、聚合查询

# 聚合查询
DELETE product
## 数据
PUT product
{
  "mappings" : {
      "properties" : {
        "createtime" : {
          "type" : "date"
        },
        "date" : {
          "type" : "date"
        },
        "desc" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "analyzer":"ik_max_word"
        },
        "lv" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "name" : {
          "type" : "text",
          "analyzer":"ik_max_word",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "price" : {
          "type" : "long"
        },
        "tags" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "type" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
}
PUT /product/_doc/1
{
    "name" : "小米手机",
    "desc" :  "手机中的战斗机",
    "price" :  3999,
    "lv":"旗舰机",
    "type":"手机",
    "createtime":"2020-10-01T08:00:00Z",
    "tags": [ "性价比", "发烧", "不卡顿" ]
}
PUT /product/_doc/2
{
    "name" : "小米NFC手机",
    "desc" :  "支持全功能NFC,手机中的滑翔机",
    "price" :  4999,
        "lv":"旗舰机",
    "type":"手机",
    "createtime":"2020-05-21T08:00:00Z",
    "tags": [ "性价比", "发烧", "公交卡" ]
}
PUT /product/_doc/3
{
    "name" : "NFC手机",
    "desc" :  "手机中的轰炸机",
    "price" :  2999,
        "lv":"高端机",
    "type":"手机",
    "createtime":"2020-06-20",
    "tags": [ "性价比", "快充", "门禁卡" ]
}
PUT /product/_doc/4
{
    "name" : "小米耳机",
    "desc" :  "耳机中的黄焖鸡",
    "price" :  999,
        "lv":"百元机",
    "type":"耳机",
    "createtime":"2020-06-23",
    "tags": [ "降噪", "防水", "蓝牙" ]
}
PUT /product/_doc/5
{
    "name" : "红米耳机",
    "desc" :  "耳机中的肯德基",
    "price" :  399,
    "type":"耳机",
        "lv":"百元机",
    "createtime":"2020-07-20",
    "tags": [ "防火", "低音炮", "听声辨位" ]
}
PUT /product/_doc/6
{
    "name" : "小米手机10",
    "desc" :  "充电贼快掉电更快,超级无敌望远镜,高刷电竞屏",
    "price" :  "",
        "lv":"旗舰机",
    "type":"手机",
    "createtime":"2020-07-27",
    "tags": [ "120HZ刷新率", "120W快充", "120倍变焦" ]
}
PUT /product/_doc/7
{
    "name" : "挨炮 SE2",
    "desc" :  "除了CPU,一无是处",
    "price" :  "3299",
        "lv":"旗舰机",
    "type":"手机",
    "createtime":"2020-07-21",
    "tags": [ "割韭菜", "割韭菜", "割新韭菜" ]
}
PUT /product/_doc/8
{
    "name" : "XS Max",
    "desc" :  "听说要出新款12手机了,终于可以换掉手中的4S了",
    "price" :  4399,
        "lv":"旗舰机",
    "type":"手机",
    "createtime":"2020-08-19",
    "tags": [ "5V1A", "4G全网通", "大" ]
}
PUT /product/_doc/9
{
    "name" : "小米电视",
    "desc" :  "70寸性价比只选,不要一万八,要不要八千八,只要两千九百九十八",
    "price" :  2998,
        "lv":"高端机",
    "type":"耳机",
    "createtime":"2020-08-16",
    "tags": [ "巨馍", "家庭影院", "游戏" ]
}
PUT /product/_doc/10
{
    "name" : "红米电视",
    "desc" :  "我比上边那个更划算,我也2998,我也70寸,但是我更好看",
    "price" :  2999,
    "type":"电视",
        "lv":"高端机",
    "createtime":"2020-08-28",
    "tags": [ "大片", "蓝光8K", "超薄" ]
}
PUT /product/_doc/11
{
  "name": "红米电视",
  "desc": "我比上边那个更划算,我也2998,我也70寸,但是我更好看",
  "price": 2998,
  "type": "电视",
  "lv": "高端机",
  "createtime": "2020-08-28",
  "tags": [
    "大片",
    "蓝光8K",
    "超薄"
  ]
}

## 语法
GET product/_search
{
  "aggs": {
    "<aggs_name>": {
      "<agg_type>": {
        "field": "<field_name>"
      }
    }
  }
}
## 桶聚合 例:统计不同标签的商品数量
GET product/_search
{
  
  "aggs": {
    "tag_bucket": {
      "terms": {
        "field": "tags.keyword"
      }
    }
  }
}
## 不显示hits数据:size:0
GET product/_search
{
  "size": 0, 
  "aggs": {
    "tag_bucket": {
      "terms": {
        "field": "tags.keyword"
      }
    }
  }
}
## 排序
GET product/_search
{
  "size": 0, 
  "aggs": {
    "tag_bucket": {
      "terms": {
        "field": "tags.keyword",
        "size": 3,
        "order": {
          "_count": "desc"
        }
      }
    }
  }
}

## doc_values和field_data
GET product/_search
{
  "size": 0, 
  "aggs": {
    "tag_bucket": {
      "terms": {
        "field": "name"
      }
    }
  }
}
GET product/_search
{
  "size": 0, 
  "aggs": {
    "tag_bucket": {
      "terms": {
        "field": "name.keyword"
      }
    }
  }
}
POST product/_mapping
{
  "properties": {
    "name": {
      "type": "text",
      "analyzer": "ik_max_word",
      "fielddata": true
    }
  }
}
GET product/_search
{
  "size": 0,
  "aggs": {
    "tag_bucket": {
      "terms": {
        "size": 20,
        "field": "name"
      }
    }
  }
}

#*****************************************
## 指标聚合 
## 例:最贵、最便宜和平均价格三个指标
GET product/_search
{
  "size": 0, 
  "aggs": {
    "max_price": {
      "max": {
        "field": "price"
      }
    },
    "min_price": {
      "min": {
        "field": "price"
      }
    },
    "avg_price": {
      "avg": {
        "field": "price"
      }
    }
  }
}
## 单个聚合查询所有指标
GET product/_search
{
  "size": 0, 
  "aggs": {
    "price_stats": {
      "stats": {
        "field": "price"
      }
    }
  }
}
##按照name去重的数量
GET product/_search
{
  "size": 0, 
  "aggs": {
    "type_count": {
      "cardinality": {
        "field": "name"
      }
    }
  }
}
GET product/_search
{
  "size": 0, 
  "aggs": {
    "type_count": {
      "cardinality": {
        "field": "name.keyword"
      }
    }
  }
}
##对type计算去重后数量
GET product/_search
{
  "size": 0, 
  "aggs": {
    "type_count": {
      "cardinality": {
        "field": "lv.keyword"
      }
    }
  }
}
##*********************************************
## 管道聚合 二次聚合
## 例:统计平均价格最低的商品分类
GET product/_search
{
  "size": 0, 
  "aggs": {
    "type_bucket": {
      "terms": {
        "field": "type.keyword"
      },
      "aggs": {
        "price_bucket": {
          "avg": {
            "field": "price"
          }
        }
      }
    },
    "min_bucket":{
      "min_bucket": {
        "buckets_path": "type_bucket>price_bucket"
      }
    }
  }
}




##=============================================
## 嵌套聚合
## 语法
GET product/_search
{
  "size": 0,
  "aggs": {
    "<agg_name>": {
      "<agg_type>": {
        "field": "<field_name>"
      },
      "aggs": {
        "<agg_name_child>": {
          "<agg_type>": {
            "field": "<field_name>"
          }
        }
      }
    }
  }
}
# 例:统计不同类型商品的不同级别的数量
GET product/_search
{
  "size": 0, 
  "aggs": {
    "type_lv": {
      "terms": {
        "field": "type.keyword"
      },
      "aggs": {
        "lv": {
          "terms": {
            "field": "lv.keyword"
          }
        }
      }
    }
  }
}
#按照lv分桶 输出每个桶的具体价格信息
GET product/_search
{
  "size": 0, 
  "aggs": {
    "lv_price": {
      "terms": {
        "field": "lv.keyword"
      },
      "aggs": {
        "price": {
          "stats": {
            "field": "price"
          }
        }
      }
    }
  }
}

##结合了上面两个例子
##统计不同类型商品 不同档次的 价格信息 标签信息
GET product/_search
{
  "size": 0, 
  "aggs": {
    "type_agg": {
      "terms": {
        "field": "type.keyword"
      },
      "aggs": {
        "lv_agg": {
          "terms": {
            "field": "lv.keyword"
          },
          "aggs": {
            "price_stats": {
              "stats": {
                "field": "price"
              }
            },
            "tags_buckets": {
              "terms": {
                "field": "tags.keyword"
              }
            }
          }
        }
      }
    }
  }
}

## 统计每个商品类型中 不同档次分类商品中 平均价格最低的档次
GET product/_search
{
  "size": 0,
  "aggs": {
    "type_bucket": {
      "terms": {
        "field": "type.keyword"
      },
      "aggs": {
        "lv_bucket": {
          "terms": {
            "field": "lv.keyword"
          },
          "aggs": {
            "price_avg": {
              "avg": {
                "field": "price"
              }
            }
          }
        },
        "min_bucket": {
          "min_bucket": {
            "buckets_path": "lv_bucket>price_avg"
          }
        }
      }
    }
  }
}

#======================================================
#基于查询结果的聚合
GET product/_search
{
  "size": 0, 
  "query": {
    "range": {
      "price": {
        "gte": 5000
      }
    }
  }, 
  "aggs": {
    "tags_bucket": {
      "terms": {
        "field": "tags.keyword"
      }
    }
  }
}

#基于filter的aggs
GET product/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "price": {
            "gte": 5000
          }
        }
      }
    }
  },
  "aggs": {
    "tags_bucket": {
      "terms": {
        "field": "tags.keyword"
      }
    }
  }
}

GET product/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "price": {
            "gte": 5000
          }
        }
      }
    }
  }, 
  "aggs": {
    "tags_bucket": {
      "terms": {
        "field": "tags.keyword"
      }
    }
  }
}


#基于聚合的查询
GET product/_search
{
  "aggs": {
    "tags_bucket": {
      "terms": {
        "field": "tags.keyword"
      }
    }
  },
  "post_filter": {
    "term": {
      "tags.keyword": "性价比"
    }
  }
}

#取消查询条件&&查询条件嵌套
## 例:最贵、最便宜和平均价格三个指标
GET product/_search
{
  "size": 10,
  "query": {
    "range": {
      "price": {
        "gte": 4000
      }
    }
  },
  "aggs": {
    "max_price": {
      "max": {
        "field": "price"
      }
    },
    "min_price": {
      "min": {
        "field": "price"
      }
    },
    "avg_price": {
      "avg": {
        "field": "price"
      }
    },
    "all_avg_price": {
      "global": {},
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    },
    "muti_avg_price": {
      "filter": {
        "range": {
          "price": {
            "lte": 4500
          }
        }
      }, 
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}


#===============================================
#聚合排序_count _key _term
GET product/_search
{
  "size": 0,
  "aggs": {
    "type_agg": {
      "terms": {
        "field": "tags",
        "order": {
          "_count": "desc"
        },
        "size": 10
      }
    }
  }
}
#多级排序
GET product/_search?size=0
{
  "aggs": {
    "first_sort": {
      "terms": {
        "field": "type.keyword",
        "order": {
          "_count": "desc"
        }
      },
      "aggs": {
        "second_sort": {
          "terms": {
            "field": "lv.keyword",
            "order": {
              "_count": "asc"
            }
          }
        }
      }
    }
  }
}


#多层排序
GET product/_search
{
  "size": 0,
  "aggs": {
    "tag_avg_price": {
      "terms": {
        "field": "type.keyword",
        "order": {
          "agg_stats>stats.sum": "desc"
        }
      },
      "aggs": {
        "agg_stats": {
          "filter": {
            "terms": {
              "type.keyword": [
                "耳机","手机","电视"
              ]
            }
          },
          "aggs": {
            "stats": {
              "extended_stats": {
                "field": "price"
              }
            }
          }
        }
      }
    }
  }
}


#===========================================================
# 常用的查询函数
## histogram 直方图 或者 柱状图 
GET product/_search
{
  "aggs": {
    "price_range": {
      "range": {
        "field": "price",
        "ranges": [
          {
            "from": 0,
            "to": 1000
          },
          {
            "from": 1000,
            "to": 2000
          },
          {
            "from": 3000,
            "to": 4000
          },
          {
            "from": 4000,
            "to": 5000
          }
        ]
      }
    }
  }
}
GET product/_search?size=0
{
  "aggs": {
    "price_range": {
      "range": {
        "field": "createtime",
        "ranges": [
          {
            "from": "2020-05-01", 
            "to": "2020-05-31"
          },
          {
            "from": "2020-06-01",
            "to": "2020-06-30"
          },
          {
            "from": "2020-07-01",
            "to": "2020-07-31"
          },
          {
            "from": "2020-08-01"
          }
        ]
      }
    }
  }
}
#空值的处理逻辑 对字段的空值赋予默认值
GET product/_search?size=0
{
  "aggs": {
    "price_histogram": {
      "histogram": {
        "field": "price",
        "interval": 1000,
        "keyed": true,
        "min_doc_count": 0,
        "missing": 1999
      }
    }
  }
}
#date-histogram
#ms s m h d
GET product/_search?size=0
{
  "aggs": {
    "my_date_histogram": {
      "date_histogram": {
        "field": "createtime",
        "calendar_interval": "month",
        "min_doc_count": 0,
        "format": "yyyy-MM", 
        "extended_bounds": {
          "min": "2020-01",
          "max": "2020-12"
        },
        "order": {
          "_count": "desc"
        }
      }
    }
  }
}
GET product/_search?size=0
{
  "aggs": {
    "my_auto_histogram": {
      "auto_date_histogram": {
        "field": "createtime",
        "format": "yyyy-MM-dd",
        "buckets": 180
      }
    }
  }
}
#cumulative_sum
GET product/_search?size=0
{
  "aggs": {
    "my_date_histogram": {
      "date_histogram": {
        "field": "createtime",
        "calendar_interval": "month",
        "min_doc_count": 0,
        "format": "yyyy-MM", 
        "extended_bounds": {
          "min": "2020-01",
          "max": "2020-12"
        }
      },
      "aggs": {
        "sum_agg": {
          "sum": {
            "field": "price"
          }
        },
        "my_cumulative_sum":{
          "cumulative_sum": {
            "buckets_path": "sum_agg"
          }
        }
      }
    }
  }
}
## percentile 百分位统计 或者 饼状图
## https://www.elastic.co/guide/en/elasticsearch/reference/7.10/search-aggregations-metrics-percentile-aggregation.html

GET product/_search?size=0
{
  "aggs": {
    "price_percentiles": {
      "percentiles": {
        "field": "price",
        "percents": [
          1,
          5,
          25,
          50,
          75,
          95,
          99
        ]
      }
    }
  }
}
#percentile_ranks
#TDigest
GET product/_search?size=0
{
  "aggs": {
    "price_percentiles": {
      "percentile_ranks": {
        "field": "price",
        "values": [
          1000,
          2000,
          3000,
          4000,
          5000,
          6000
        ]
      }
    }
  }
}

6、脚本查询

# ES脚本
##语法:ctx._source.<field-name>
GET product/_search
GET product/_doc/2
POST product/_update/2
{
  "script": {
    "source": "ctx._source.price-=1"
  }
}

POST product/_update/2
{
  "script": {
    "source": "ctx._source.price-=ctx._version"
  }
}

#简写
POST product/_update/2
{
  "script": "ctx._source.price-=1"
}
#====================================================
# Scripting的CRUD
POST _reindex
{
  "source": {
    "index": "product"
  },
  "dest": {
    "index": "product2"
  }
}
# 举个例子:小米10出了新款 新增了tag 叫做“无线充电”
POST product/_update/6
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.tags.add('无线充电')"
  }
}
GET product/_doc/6
GET product/_search
{
  "size": 20,
  "query": {
    "match": {
      "_id": 10
    }
  }
}
#delete
POST product/_update/10
{
  "script": {
    "lang": "painless",
    "source": "ctx.op='delete'"
  }
}
#upsert update + insert 
DELETE product/_doc/15
GET product/_doc/15
POST product/_update/15
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.price += 100"
  },
  "upsert": {
    "name" : "小米手机10",
    "desc" : "充电贼快掉电更快,超级无敌望远镜,高刷电竞屏",
    "price" : 1999
  }
}

#GET查询 painless expression
GET product/_search
{
  "script_fields": {
    "my_price": {
      "script": {
        "lang": "expression",
        "source": "doc['price'].value* 0.9"
      }
    }
  }
}
GET product/_search
{
  "script_fields": {
    "my_price": {
      "script": {
        "lang": "painless",
        "source": "doc['price'].value* 0.9"
      }
    }
  }
}
GET product/_doc/6
POST product/_update/6
{
  "doc": {
    "price": 5999
  }
}
#==========================================
#参数化
POST product/_update/6
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.tags.add(params.tag_name)",
    "params": {
      "tag_name":"无线秒充"
    }
  }
}

GET product/_search
{
  "script_fields": {
    "my_price": {
      "script": {
        "lang": "painless",
        "source": "doc['price'].value* params.num",
        "params": {
          "num": 9 
        }
      }
    }
  }
}

GET product/_search
{
  "script_fields": {
    "my_price": {
      "script": {
        "lang": "expression",
        "source": "doc['price']* num",
        "params": {
          "num": 9 
        }
      }
    }
  }
}

#案例: 打8GET product/_search
{
  "script_fields": {
    "price": {
      "script": {
        "lang": "painless",
        "source": "doc['price'].value"
      }
    },
    "discount_price": {
      "script": {
        "lang": "painless",
        "source": "[doc['price'].value* params.discount_8,doc['price'].value* params.discount_7,doc['price'].value* params.discount_6,doc['price'].value* params.discount_5]",
        "params": {
          "discount_8": 0.8,
          "discount_7": 0.7,
          "discount_6": 0.6,
          "discount_5": 0.5
        }
      }
    }
  }
}

# Stored scripts   scripts模板
# /_scripts/{id}
POST _scripts/calculate_discount
{
  "script": {
    "lang": "painless",
    "source": "doc.price.value * params.discount"
  }
}
#查看
GET _scripts/calculate_discount
GET product/_search
{
  "script_fields": {
    "price": {
      "script": {
        "lang": "painless",
        "source": "doc['price'].value"
      }
    },
    "discount_fields": {
      "script": {
        "id": "calculate_discount",
        "params": {
          "discount":0.8
        }
      }
    }
  }
}

#Scripting的函数式编程
GET product/_search
GET product/_doc/1
POST product/_update/1
{
  "script": {
    "lang": "painless",
    "source": "ctx._source.tags.add(params.tag_name)",
    "params": {
      "tag_name":"无线秒充"
    }
  }
}

POST product/_update/1
{
  "script": {
    "lang": "painless",
    "source": """
      ctx._source.tags.add(params.tag_name);
      ctx._source.price-=100;
    """,
    "params": {
      "tag_name":"无线秒充1"
    }
  }
}
GET product/_doc/1
GET product/_search
#正则like %小米% /[\s\S]*小米[\s\S]*/
POST product/_update/3
{
  "script": {
    "lang": "painless",
    "source": """
      if(ctx._source.name ==~ /[\s\S]*小米[\s\S]*/) {
        ctx._source.name+="***|"
      }else{
        ctx.op="noop"
      }
    """
  }
}
#/\d{4}-\d{2}-\d{2}[\s\S]*/
GET product/_doc/1
POST product/_update/1
{
  "script": {
    "lang": "painless",
    "source": """
      if(ctx._source.createtime ==~ /\d{4}-\d{2}-\d{2}[\s\S]*/) {
        ctx._source.name+="|***"
      }else{
        ctx.op="noop"
      }
    """
  }
}
GET product/_search
#统计所有价格小于1000的商品的tag的数量 不考虑重复的情况
GET product/_mapping
GET product/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "price": {
            "lte": 1000
          }
        }
      }
    }
  },
  "aggs": {
    "tag_agg": {
      "sum": {
        "script": {
          "lang": "painless",
          "source": """
            int total = 0;
            for(int i = 0; i <doc['tags.keyword'].length; i++){
              total++;
            }
            return total;
          """
        }
      }
    }
  }
}

#================================================
#本章小结 && 知识点补充.

# 对于一些早期版本 script.painless.regex.enabled: true
# doc['field'].value 和 params['_source']['field']

#以下为案件的嫌疑人信息
PUT test_index/_bulk?refresh
{"index":{"_id":1}}
{"ajbh": "12345","ajmc": "立案案件","lasj": "2020/05/21 13:25:23","jsbax_sjjh2_xz_ryjbxx_cleaning": [{"XM": "张三","NL": "30","SF": "男"},{"XM": "李四","NL": "31","SF": "男"},{"XM": "王五","NL": "30","SF": "女"},{"XM": "赵六","NL": 23,"SF": "男"}]}
{"index":{"_id":2}}
{"ajbh": "563245","ajmc": "结案案件","lasj": "2020/05/21 13:25:23","jsbax_sjjh2_xz_ryjbxx_cleaning": [{"XM": "张三2","NL": "30","SF": "男"},{"XM": "李四2","NL": "31","SF": "男"},{"XM": "王五2","NL": "30","SF": "女"},{"XM": "赵六2","NL": "23","SF": "女"}]}
{"index":{"_id":3}}
{"ajbh": "12345","ajmc": "立案案件","lasj": "2020/05/21 13:25:23","jsbax_sjjh2_xz_ryjbxx_cleaning": [{"XM": "张三3","NL": "30","SF": "男"},{"XM": "李四3","NL": "31","SF": "男"},{"XM": "王五3","NL": "30","SF": "女"},{"XM": "赵六3","NL": "23","SF": "男"}]}

#统计男性嫌疑人的数量
GET test_index/_search
GET product/_search
#Object Nested
GET /test_index/_search
{
  "aggs": {
    "sum_person": {
      "sum": {
        "script": {
          "lang": "painless",
          "source": """
            int total = 0;
            for(int i = 0;i< params['_source']['jsbax_sjjh2_xz_ryjbxx_cleaning'].length;i++){
              if(params['_source']['jsbax_sjjh2_xz_ryjbxx_cleaning'][i]['SF']=='男'){
                total+=1;
              }
            }
            return total;
          """
        }
      }
    }
  }
}

7、索引批量操作

#批量查询
GET product/_search
GET /_mget
{
  "docs": [
    {
      "_index": "product",
      "_id": 2
    },
    {
      "_index": "product",
      "_id": 3
    }
  ]
}

GET product/_mget
{
  "docs": [
    {
      "_id": 2
    },
    {
      "_id": 3
    }
  ]
}
#SELECT * FROM TABLE WHERE id in()
GET product/_mget
{
  "ids": [
    2,
    3,
    4
  ]
}

GET product/_mget
{
  "docs": [
    {
      "_id": 2,
      "_source": [
        "name",
        "price"
      ]
    },
    {
      "_id": 3,
      "_source": {
        "include": [
          "name",
          "price"
        ],
        "exclude": [
          "price",
          "type"
        ]
      }
    }
  ]
}

#======================================================
#对文档的操作类型: op_type
# enum OpType {
#   INDEX(0),
#   CREATE(1),
#   UPDATE(2),
#   DELETE(3)
# }

#create:
GET test_index/_doc/1
PUT test_index/_doc/1
{
  "test_field":"test",
  "test_title":"title"
}
PUT test_index/_doc/2/_create
{
  "test_field":"test",
  "test_title":"title"
}

PUT test_index/_create/4?filter_path=items.*.error
{
  "test_field":"test",
  "test_title":"title"
}

POST test_index/_doc
{
  "test_field":"test",
  "test_title":"title"
}
#delete:懒删除
DELETE test_index/_doc/3
#update:
GET test_index/_search
GET test_index/_doc/0APggnkBPdz4eXq223h8
PUT /test_index/_doc/0APggnkBPdz4eXq223h8
{
  "test_field": "test 2",
  "test_title": "title 2"
}
POST /test_index/_update/0APggnkBPdz4eXq223h8
{
  "doc": {
    "test_title": "test 3"
  }
}
#index:可以使创建 也可以使全量替换
#创建     PUT test_index/_create/0APggnkBPdz4eXq223h8
#全量替换 PUT test_index/_doc/0APggnkBPdz4eXq223h8
GET test_index/_doc/0APggnkBPdz4eXq223h8
PUT /test_index/_doc/5?op_type=index&filter_path=items.*.error
{
  "test_field": "test 2",
  "test_title": "title 2",
  "test_name": "title 2"
}

#?filter_path=items.*.error

########################################################
#批量增删改
#POST /_bulk
#POST /<index>/_bulk
#{"action": {"metadata"}}
#{"data"}
PUT /product/_doc/1
{
    "name" : "小米手机",
    "desc" :  "手机中的战斗机",
    "price" :  3999,
    "lv":"旗舰机",
    "type":"手机",
    "createtime":"2020-10-01T08:00:00Z",
    "tags": [ "性价比", "发烧", "不卡顿" ]
}



GET product/_search

POST _reindex
{
  "source": {
    "index": "product"
  },
  "dest": {
    "index": "product2"
  }
}
GET product2/_search
GET product2/_doc/4
GET product/_doc/4
POST /_bulk
{ "create": { "_index": "product2",  "_id": "2" }}
{ "name":    "_bulk create 2" }
{ "create": { "_index": "product2",  "_id": "12" }}
{ "name":    "_bulk create 12" }
{ "index":  { "_index": "product2",  "_id": "3" }}
{ "name":    "index product2 "}
{ "index":  { "_index": "product2",  "_id": "13" }}
{ "name":    "index product2" }
{ "update": { "_index": "product2",  "_id": "4","retry_on_conflict" : "3"} }
{ "doc" : {"test_field2" : "bulk test1"} }

#加?filter_path=items.*.error  只显示失败的
POST /_bulk?filter_path=items.*.error
{ "delete": { "_index": "product2",  "_id": "1" }}
{ "create": { "_index": "product2",  "_id": "2" }}
{ "name":    "_bulk create 2" }
{ "create": { "_index": "product2",  "_id": "12" }}
{ "name":    "_bulk create 12" }
{ "index":  { "_index": "product2",  "_id": "3" }}
{ "name":    "index product2 " }
{ "index":  { "_index": "product2",  "_id": "13" }}
{ "name":    "index product2" }
{ "update": { "_index": "product2",  "_id": "4","retry_on_conflict" : "3"} }
{ "doc" : {"test_field2" : "bulk test1"} }

8、模糊搜索

#prefix: 前缀搜索
DELETE my_index
# elasticsearch stack
# elasticsearch search
# el
# ela 
# elas elasticsearch
PUT my_index
{
  "mappings": {
    "properties": {
      "text": {
        "analyzer": "ik_max_word",
        "type": "text",
        "index_prefixes":{
          "min_chars":2,
          "max_chars":4
        },
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}
GET my_index/_mapping
POST /my_index/_bulk?filter_path=items.*.error
{"index":{"_id":"1"}}
{"text":"城管打电话喊商贩去摆摊摊"}
{"index":{"_id":"2"}}
{"text":"笑果文化回应商贩老农去摆摊"}
{"index":{"_id":"3"}}
{"text":"老农耗时17年种出椅子树"}
{"index":{"_id":"4"}}
{"text":"夫妻结婚30多年AA制,被城管抓"}
{"index":{"_id":"5"}}
{"text":"黑人见义勇为阻止抢劫反被铐住"}
GET my_index/_search
GET my_index/_mapping
GET _analyze
{
  "text": ["夫妻结婚30多年AA制,被城管抓"]
}
GET my_index/_search
{
  "query": {
    "prefix": {
      "text": {
        "value": "城管"
      }
    }
  }
}

###############################################################
# 通配符
DELETE my_index
POST /my_index/_bulk
{ "index": { "_id": "1"} }
{ "text": "my english" }
{ "index": { "_id": "2"} }
{ "text": "my english is good" }
{ "index": { "_id": "3"} }
{ "text": "my chinese is good" }
{ "index": { "_id": "4"} }
{ "text": "my japanese is nice" }
{ "index": { "_id": "5"} }
{ "text": "my disk is full" }
DELETE product_en
POST /product_en/_bulk
{ "index": { "_id": "1"} }
{ "title": "my english","desc" :  "shouji zhong de zhandouji","price" :  3999, "tags": [ "xingjiabi", "fashao", "buka", "1"]}
{ "index": { "_id": "2"} }
{ "title": "xiaomi nfc phone","desc" :  "zhichi quangongneng nfc,shouji zhong de jianjiji","price" :  4999, "tags": [ "xingjiabi", "fashao", "gongjiaoka" , "asd2fgas"]}
{ "index": { "_id": "3"} }
{ "title": "nfc phone","desc" :  "shouji zhong de hongzhaji","price" :  2999, "tags": [ "xingjiabi", "fashao", "menjinka" , "as345"]}
{ "title": { "_id": "4"} }
{ "text": "xiaomi erji","desc" :  "erji zhong de huangmenji","price" :  999, "tags": [ "low", "bufangshui", "yinzhicha", "4dsg" ]}
{ "index": { "_id": "5"} }
{ "title": "hongmi erji","desc" :  "erji zhong de kendeji","price" :  399, "tags": [ "lowbee", "xuhangduan", "zhiliangx" , "sdg5"]}
GET my_index/_search
GET product_en/_search

GET my_index/_search
{
  "query": {
    "wildcard": {
      "text.keyword": {
        "value": "my eng*ish"
      }
    }
  }
}
GET product_en/_mapping
#exact value
GET product_en/_search
{
  "query": {
    "wildcard": {
      "tags.keyword": {
        "value": "men*inka"
      }
    }
  }
}

#######################################################
#正则
GET product_en/_search
GET product_en/_search
{
  "query": {
    "regexp": {
      "title": "[\\s\\S]*nfc[\\s\\S]*"
    }
  }
}
GET product_en/_search
GET product_en/_search
{
  "query": {
    "regexp": {
      "desc": {
        "value": "zh~dng",
        "flags": "COMPLEMENT"
      }
    }
  }
}
GET product_en/_search
{
  "query": {
    "regexp": {
      "tags.keyword": {
        "value": ".*<2-3>.*",
        "flags": "INTERVAL"
      }
    }
  }
}
#############################################
# fuzzy:模糊查询
GET product_en/_search
GET product_en/_search
{
  "query": {
    "fuzzy": {
      "desc": {
        "value": "quangongneng nfc",
        "fuzziness": "2"
      }
    }
  }
}

GET product_en/_search
{
  "query": {
    "match": {
      "desc": {
        "query": "nfe quasdasdasdasd",
        "fuzziness": 1
      }
    }
  }
}
#####################################
# match_phrase_prefix
GET product_en/_search
{
  "query": {
    "match_phrase": {
      "desc": "shouji zhong de"
    }
  }
}

GET product_en/_search
{
  "query": {
    "match_phrase_prefix": {
      "desc": {
        "query": "de zhong shouji hongzhaji",
        "max_expansions": 50,
        "slop":3
      }
    }
  }
}


GET product_en/_search
{
  "query": {
    "match_phrase_prefix": {
      "desc": {
        "query": "zhong hongzhaji",
        "max_expansions": 50,
        "slop": 3
      }
    }
  }
}


# source: zhong de hongzhaji
# query:  zhong >  hongzhaji

# source: shouji zhong de hongzhaji
# query:  de zhong shouji hongzhaji

# de shouji/zhong  hongzhaji  1次
# shouji/de zhong  hongzhaji  2次
# shouji zhong/de  hongzhaji  3次
# shouji zhong de  hongzhaji  4次

#############################################
# ngram 和 edge-ngram
#ngram   min_gram =1   "max_gram": 2

GET _analyze
{
  "tokenizer": "ik_max_word",
  "filter": [ "edge_ngram" ],
  "text": "reba always loves me"
}

#min_gram =1   "max_gram": 1
#r a l m

#min_gram =1   "max_gram": 2
#r a l m
#re al lo me

#min_gram =2   "max_gram": 3
#re al lo me
#reb alw lov me



PUT my_index
{
  "settings": {
    "analysis": {
      "filter": {
        "2_3_edge_ngram": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 3
        }
      },
      "analyzer": {
        "my_edge_ngram": {
          "type":"custom",
          "tokenizer": "standard",
          "filter": [ "2_3_edge_ngram" ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "analyzer":"my_edge_ngram",
        "search_analyzer": "standard"
      }
    }
  }
}
GET /my_index/_mapping


POST /my_index/_bulk
{ "index": { "_id": "1"} }
{ "text": "my english" }
{ "index": { "_id": "2"} }
{ "text": "my english is good" }
{ "index": { "_id": "3"} }
{ "text": "my chinese is good" }
{ "index": { "_id": "4"} }
{ "text": "my japanese is nice" }
{ "index": { "_id": "5"} }
{ "text": "my disk is full" }


GET /my_index/_search
GET /my_index/_mapping
GET /my_index/_search
{
  "query": {
    "match_phrase": {
      "text": "my eng is goo"
    }
  }
}



PUT my_index2
{
  "settings": {
    "analysis": {
      "filter": {
        "2_3_grams": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 3
        }
      },
      "analyzer": {
        "my_edge_ngram": {
          "type":"custom",
          "tokenizer": "standard",
          "filter": [ "2_3_grams" ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "analyzer":"my_edge_ngram",
        "search_analyzer": "standard"
      }
    }
  }
}
GET /my_index2/_mapping
POST /my_index2/_bulk
{ "index": { "_id": "1"} }
{ "text": "my english" }
{ "index": { "_id": "2"} }
{ "text": "my english is good" }
{ "index": { "_id": "3"} }
{ "text": "my chinese is good" }
{ "index": { "_id": "4"} }
{ "text": "my japanese is nice" }
{ "index": { "_id": "5"} }
{ "text": "my disk is full" }

GET /my_index2/_search
{
  "query": {
    "match_phrase": {
      "text": "my eng is goo"
    }
  }
}

GET _analyze
{
  "tokenizer": "ik_max_word",
  "filter": [ "ngram" ],
  "text": "用心做皮肤,用脚做游戏"
}

9、搜索的推荐

#prefix: 前缀搜索
DELETE my_index
# elasticsearch stack
# elasticsearch search
# el
# ela 
# elas elasticsearch
PUT my_index
{
  "mappings": {
    "properties": {
      "text": {
        "analyzer": "ik_max_word",
        "type": "text",
        "index_prefixes":{
          "min_chars":2,
          "max_chars":4
        },
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}
GET my_index/_mapping
POST /my_index/_bulk?filter_path=items.*.error
{"index":{"_id":"1"}}
{"text":"城管打电话喊商贩去摆摊摊"}
{"index":{"_id":"2"}}
{"text":"笑果文化回应商贩老农去摆摊"}
{"index":{"_id":"3"}}
{"text":"老农耗时17年种出椅子树"}
{"index":{"_id":"4"}}
{"text":"夫妻结婚30多年AA制,被城管抓"}
{"index":{"_id":"5"}}
{"text":"黑人见义勇为阻止抢劫反被铐住"}
GET my_index/_search
GET my_index/_mapping
GET _analyze
{
  "text": ["夫妻结婚30多年AA制,被城管抓"]
}
GET my_index/_search
{
  "query": {
    "prefix": {
      "text": {
        "value": "城管"
      }
    }
  }
}

###############################################################
# 通配符
DELETE my_index
POST /my_index/_bulk
{ "index": { "_id": "1"} }
{ "text": "my english" }
{ "index": { "_id": "2"} }
{ "text": "my english is good" }
{ "index": { "_id": "3"} }
{ "text": "my chinese is good" }
{ "index": { "_id": "4"} }
{ "text": "my japanese is nice" }
{ "index": { "_id": "5"} }
{ "text": "my disk is full" }
DELETE product_en
POST /product_en/_bulk
{ "index": { "_id": "1"} }
{ "title": "my english","desc" :  "shouji zhong de zhandouji","price" :  3999, "tags": [ "xingjiabi", "fashao", "buka", "1"]}
{ "index": { "_id": "2"} }
{ "title": "xiaomi nfc phone","desc" :  "zhichi quangongneng nfc,shouji zhong de jianjiji","price" :  4999, "tags": [ "xingjiabi", "fashao", "gongjiaoka" , "asd2fgas"]}
{ "index": { "_id": "3"} }
{ "title": "nfc phone","desc" :  "shouji zhong de hongzhaji","price" :  2999, "tags": [ "xingjiabi", "fashao", "menjinka" , "as345"]}
{ "title": { "_id": "4"} }
{ "text": "xiaomi erji","desc" :  "erji zhong de huangmenji","price" :  999, "tags": [ "low", "bufangshui", "yinzhicha", "4dsg" ]}
{ "index": { "_id": "5"} }
{ "title": "hongmi erji","desc" :  "erji zhong de kendeji","price" :  399, "tags": [ "lowbee", "xuhangduan", "zhiliangx" , "sdg5"]}
GET my_index/_search
GET product_en/_search

GET my_index/_search
{
  "query": {
    "wildcard": {
      "text.keyword": {
        "value": "my eng*ish"
      }
    }
  }
}
GET product_en/_mapping
#exact value
GET product_en/_search
{
  "query": {
    "wildcard": {
      "tags.keyword": {
        "value": "men*inka"
      }
    }
  }
}

#######################################################
#正则
GET product_en/_search
GET product_en/_search
{
  "query": {
    "regexp": {
      "title": "[\\s\\S]*nfc[\\s\\S]*"
    }
  }
}
GET product_en/_search
GET product_en/_search
{
  "query": {
    "regexp": {
      "desc": {
        "value": "zh~dng",
        "flags": "COMPLEMENT"
      }
    }
  }
}
GET product_en/_search
{
  "query": {
    "regexp": {
      "tags.keyword": {
        "value": ".*<2-3>.*",
        "flags": "INTERVAL"
      }
    }
  }
}
#############################################
# fuzzy:模糊查询
GET product_en/_search
GET product_en/_search
{
  "query": {
    "fuzzy": {
      "desc": {
        "value": "quangongneng nfc",
        "fuzziness": "2"
      }
    }
  }
}

GET product_en/_search
{
  "query": {
    "match": {
      "desc": {
        "query": "nfe quasdasdasdasd",
        "fuzziness": 1
      }
    }
  }
}
#####################################
# match_phrase_prefix
GET product_en/_search
{
  "query": {
    "match_phrase": {
      "desc": "shouji zhong de"
    }
  }
}

GET product_en/_search
{
  "query": {
    "match_phrase_prefix": {
      "desc": {
        "query": "de zhong shouji hongzhaji",
        "max_expansions": 50,
        "slop":3
      }
    }
  }
}


GET product_en/_search
{
  "query": {
    "match_phrase_prefix": {
      "desc": {
        "query": "zhong hongzhaji",
        "max_expansions": 50,
        "slop": 3
      }
    }
  }
}


# source: zhong de hongzhaji
# query:  zhong >  hongzhaji

# source: shouji zhong de hongzhaji
# query:  de zhong shouji hongzhaji

# de shouji/zhong  hongzhaji  1次
# shouji/de zhong  hongzhaji  2次
# shouji zhong/de  hongzhaji  3次
# shouji zhong de  hongzhaji  4次

#############################################
# ngram 和 edge-ngram
#ngram   min_gram =1   "max_gram": 2

GET _analyze
{
  "tokenizer": "ik_max_word",
  "filter": [ "edge_ngram" ],
  "text": "reba always loves me"
}

#min_gram =1   "max_gram": 1
#r a l m

#min_gram =1   "max_gram": 2
#r a l m
#re al lo me

#min_gram =2   "max_gram": 3
#re al lo me
#reb alw lov me



PUT my_index
{
  "settings": {
    "analysis": {
      "filter": {
        "2_3_edge_ngram": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 3
        }
      },
      "analyzer": {
        "my_edge_ngram": {
          "type":"custom",
          "tokenizer": "standard",
          "filter": [ "2_3_edge_ngram" ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "analyzer":"my_edge_ngram",
        "search_analyzer": "standard"
      }
    }
  }
}
GET /my_index/_mapping


POST /my_index/_bulk
{ "index": { "_id": "1"} }
{ "text": "my english" }
{ "index": { "_id": "2"} }
{ "text": "my english is good" }
{ "index": { "_id": "3"} }
{ "text": "my chinese is good" }
{ "index": { "_id": "4"} }
{ "text": "my japanese is nice" }
{ "index": { "_id": "5"} }
{ "text": "my disk is full" }


GET /my_index/_search
GET /my_index/_mapping
GET /my_index/_search
{
  "query": {
    "match_phrase": {
      "text": "my eng is goo"
    }
  }
}



PUT my_index2
{
  "settings": {
    "analysis": {
      "filter": {
        "2_3_grams": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 3
        }
      },
      "analyzer": {
        "my_edge_ngram": {
          "type":"custom",
          "tokenizer": "standard",
          "filter": [ "2_3_grams" ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "text": {
        "type": "text",
        "analyzer":"my_edge_ngram",
        "search_analyzer": "standard"
      }
    }
  }
}
GET /my_index2/_mapping
POST /my_index2/_bulk
{ "index": { "_id": "1"} }
{ "text": "my english" }
{ "index": { "_id": "2"} }
{ "text": "my english is good" }
{ "index": { "_id": "3"} }
{ "text": "my chinese is good" }
{ "index": { "_id": "4"} }
{ "text": "my japanese is nice" }
{ "index": { "_id": "5"} }
{ "text": "my disk is full" }

GET /my_index2/_search
{
  "query": {
    "match_phrase": {
      "text": "my eng is goo"
    }
  }
}

GET _analyze
{
  "tokenizer": "ik_max_word",
  "filter": [ "ngram" ],
  "text": "用心做皮肤,用脚做游戏"
}

10、数据建模

DELETE order
PUT order
{
  "mappings": {
    "properties": {
      "goods_list": {
        "properties": {
          "name": {
            "type": "text",
            "analyzer": "ik_max_word",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

GET order/_mapping
PUT /order/_doc/1
{
  "order_name": "小米10 Pro订单",
  "desc": "shouji zhong de zhandouji",
  "goods_count": 3,
  "total_price": 12699,
  "goods_list": [
    {
      "name": "小米10 PRO MAX 5G",
      "price": 4999
    },
    {
      "name": "钢化膜",
      "price": 19
    },
    {
      "name": "手机壳",
      "price": 1999
    }
  ]
}
PUT /order/_doc/2
{
  "order_name": "扫地机器人订单",
  "desc": "shouji zhong de zhandouji",
  "goods_count": 2,
  "total_price": 12699,
  "goods_list": [
    {
      "name": "小米扫地机器人儿",
      "price": 1999
    },
    {
      "name": "洗碗机",
      "price": 4999
    }
  ]
}
GET _analyze
{
  "analyzer": "ik_max_word",
  "text": ["小米10"]
}
GET order/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "goods_list.name": "小米10"
          }
        },
        {
          "match": {
            "goods_list.price": "1999"
          }
        }
      ]
    }
  }
}
GET order/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "goods_list.name": "洗碗机"
          }
        },
        {
          "match": {
            "goods_list.price": "1999"
          }
        }
      ]
    }
  }
}
GET product_en/_search
DELETE order
PUT order
{
  "mappings": {
    "properties": {
      "goods_list": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "text",
            "analyzer": "ik_max_word",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

PUT /order/_doc/1
{
  "order_name": "小米10 Pro订单",
  "desc": "shouji zhong de zhandouji",
  "goods_count": 3,
  "total_price": 12699,
  "goods_list": [
    {
      "name": "小米10 PRO MAX 5G",
      "price": 4999
    },
    {
      "name": "钢化膜",
      "price": 19
    },
    {
      "name": "手机壳",
      "price": 199
    }
  ]
}
PUT /order/_doc/2
{
  "order_name": "扫地机器人订单",
  "desc": "shouji zhong de zhandouji",
  "goods_count": 2,
  "total_price": 12699,
  "goods_list": [
    {
      "name": "小米扫地机器热儿",
      "price": 1999
    },
    {
      "name": "洗碗机",
      "price": 4999
    }
  ]
}

GET /order/_search
{
  "query": {
    "nested": {
      "path": "goods_list",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "goods_list.name": "小米10"
              }
            },
            {
              "match": {
                "goods_list.price": 4999
              }
            }
          ]
        }
      }
    }
  }
}
GET /order/_search
{
  "query": {
    "nested": {
      "path": "goods_list",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "goods_list.name": "洗碗机"
              }
            },
            {
              "match": {
                "goods_list.price": "1999"
              }
            }
          ]
        }
      }
    }
  }
}

# score_mode
GET /order/_search
{
  "query": {
    "nested": {
      "path": "goods_list",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "goods_list.name": "小米10"
              }
            },
            {
              "match": {
                "goods_list.price": 4999
              }
            }
          ]
        }
      },
      "score_mode" : "max"
    }
  }
}


PUT /area
{
  "mappings": {
    "properties": {
      "province": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "text",
            "analyzer": "ik_max_word"
          },
          "cities": {
            "type": "nested",
            "properties": {
              "name": {
                "type": "text",
                "analyzer": "ik_max_word"
              },
              "district": {
                "type": "nested",
                "properties": {
                  "name": {
                    "type": "text",
                    "analyzer": "ik_max_word"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

GET area/_mapping
GET area/_search
PUT /area/_doc/1
{
  "province": {
    "name": "北京",
    "cities": [
      {
        "name": "北京市",
        "district": [
          {"name":"丰台区"},
          {"name":"海淀区"},
          {"name":"朝阳区"},
          {"name":"东城区"},
          {"name":"西城区"},
          {"name":"昌平区"}
          ]
      }
    ]
  }
}
PUT /area/_doc/2
{
  "province": {
    "name": "河南省",
    "cities": [
      {
        "name": "郑州市",
        "district": [
          {
            "name": "金水区"
          },
          {
            "name": "高新区"
          },
          {
            "name": "郑东新区"
          },
          {
            "name": "二七区"
          },
          {
            "name": "中原区"
          },
          {
            "name": "惠济区"
          }
        ]
      },
      {
        "name": "鹤壁市",
        "district": [
          {
            "name": "山城区"
          },
          {
            "name": "淇滨区"
          },
          {
            "name": "鹤山区"
          },
          {
            "name": "朝歌"
          },
          {
            "name": "浚县"
          }
        ]
      }
    ]
  }
}
PUT /area/_doc/3
{
  "province": {
    "name": "台湾省",
    "cities": [
      {
        "name": "台北市",
        "district": [
          {
            "name": "中正区"
          },
          {
            "name": "大同区"
          },
          {
            "name": "中山区"
          },
          {
            "name": "万华区"
          },
          {
            "name": "信义区"
          },
          {
            "name": "松山区"
          }
        ]
      },
      {
        "name": "高雄",
        "district": [
          {
            "name": "小港区"
          },
          {
            "name": "鼓山区"
          },
          {
            "name": "三民区"
          }
        ]
      }
    ]
  }
}
#city为包含北京市 或者 包含淇滨区的    省份信息

GET /area/_search
{
  "query": {
    "nested": {
      "path": "province",
      "query": {
        "nested": {
          "path": "province.cities",
          "query": {
            "bool": {
              "should": [
                {
                  "match": {
                    "province.cities.name": "北京市"
                  }
                },
                {
                  "nested": {
                    "path": "province.cities.district",
                    "query": {
                      "bool": {
                        "must": [
                          {
                            "match": {
                              "province.cities.district.name": "淇滨区"
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}

# 父子级关系
DELETE msb_depart
PUT msb_depart
{
  "mappings": {
    "properties": {
      "msb_join_field": {
        "type": "join",
        "relations": {
          "depart": "employee"
        }
      },
      "my_id": {
        "type": "keyword"
      }
    }
  }
}
GET msb_depart/_mapping
#部门
PUT msb_depart/_doc/1
{
  "my_id": 1,
  "name":"教学部",
  "msb_join_field":{
    "name":"depart"
  }
}
PUT msb_depart/_doc/2
{
  "my_id": 2,
  "name":"咨询部",
  "msb_join_field":{
    "name":"depart"
  }
}
# 老师 
# 路由值是强制性的,因为父文档和子文档必须在同一个分片上建立索引
PUT msb_depart/_doc/3?routing=1&refresh
{
  "my_id": 3,
  "name":"马老师",
  "msb_join_field":{
    "name":"employee",
    "parent":1
  }
}
PUT msb_depart/_doc/4?routing=1&refresh
{
  "my_id": 4,
  "name":"周老师",
  "msb_join_field":{
    "name":"employee",
    "parent":1
  }
}
# 咨询
PUT msb_depart/_doc/5?routing=1&refresh
{
  "my_id": 5,
  "name":"静静",
  "msb_join_field":{
    "name":"employee",
    "parent":2
  }
}
PUT msb_depart/_doc/6?routing=1&refresh
{
  "my_id": 6,
  "name":"球球",
  "msb_join_field":{
    "name":"employee",
    "parent":2
  }
}
PUT msb_depart/_doc/7?routing=1&refresh
{
  "my_id": 7,
  "name":"琪琪",
  "msb_join_field":{
    "name":"employee",
    "parent":2
  }
}
# 搜索所有部门
GET msb_depart/_search
{
  "query": {
    "has_child": {
      "type": "employee",
      "query": {
        "match_all": {}
      }
    }
  }
}
# 搜索周老师所在部门
GET msb_depart/_search
{
  "query": {
    "has_child": {
      "type": "employee",
      "query": {
        "match": {
          "name.keyword": "周老师"
        }
      }
    }
  }
}
# 搜索咨询部所有老师
GET msb_depart/_search
{
  "query": {
    "has_parent": {
      "parent_type": "depart",
      "query": {
        "match": {
          "name.keyword": "咨询部"
        }
      }
    }
  }
}
# 搜索部门id为2的部门员工
GET msb_depart/_search
{
  "query": {
    "parent_id":{
      "type":"employee",
      "id":2
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值