###################操作索引###########################
#索引的增删-查
#PUT DELETE GET HEAD
#添加
PUT myindex
#删除索引
DELETE myindex
#查询
GET myindex
#关闭索引 不能添加
POST myindex/_close
#开启索引
POST myindex/_open
GET myindex
###################操作映射###########################
#1 添加映射
PUT myindex/_mapping
{
“properties”:{
“name”:{
“type”:“keyword”
},
“age”:{
“type”:“integer”
}
}
}
#查询索引
GET myindex
#创建索引的同时添加映射
PUT myindex
{
“mappings”: {
“properties”:{
“name”:{
“type”:“keyword”
},
“age”:{
“type”:“integer”
}
}
}
}
#查询索引
GET myindex
#修改映射
PUT myindex/_mapping
{
“properties”:{
“address”:{
“type”:“text”
}
}
}
#####################操作文档#########################
#添加文档指定id put
PUT myindex/_doc/1
{
“name”:“李四”,
“age”:10,
“address”:“北京朝阳区”
}
#添加文档不指定id post
POST myindex/_doc
{
“name”:“张三”,
“age”:20,
“address”:“北京朝阳区”
}
#查询索引库的所有文档
GET myindex/_search
#查询指定id的文档
GET myindex/_doc/ZEWdAnIBlaAA6IcdY2gb
#####################中文分词#########################
#ik_max_word 细分,ik_smart 粗分
粗粒度
GET _analyze
{
“analyzer”: “ik_smart”,
“text”: “我爱北京天安门”
}
GET _analyze
{
“analyzer”: “ik_max_word”,
“text”: “我爱北京天安门”
}
DELETE person
#创建索引,添加映射,指定使用ik分词器
PUT person
{
“mappings”: {
“properties”: {
“name”:{
“type”: “keyword”
},
“age”:{
“type”: “integer”
},
“address”:{
“type”: “text”,
“analyzer”: “ik_max_word”
}
}
}
}
查询映射
GET /person/_mapping
#添加文档 ,指定id
PUT person/_doc/1
{
“name”:“张三”,
“age”:20,
“address”:“北京市海淀区”
}
PUT person/_doc/2
{
“name”:“李四”,
“age”:30,
“address”:“北京市朝阳区”
}
PUT person/_doc/3
{
“name”:“王五”,
“age”:40,
“address”:“北京市昌平区”
}
#查询所有文档
GET person/_search
#分词查询 ,term词条查询
GET person/_search
{
“query”: {
“term”: {
“name”: {
“value”: “张三”
}
}
}
}
GET person/_search
{
“query”: {
“match”: {
“address”: “昌平海淀”
}
}
}
GET person/_search
GET person
GET myindex/_mapping
GET itheima
GET itcast/_search
#################java api 操作 es##################
#################ES 高级操作##########################
##批量操作 脚本操作
#json 只能写在一行,不能换行
#语法,action 对应各种操作
POST /_bulk
{“action”: {“metadata”}}
{“data”}
GET person/_search
####demo 需求
#删除1号记录,增加4号记录,修改2号记录,名称为"二号"
POST _bulk
{“delete”:{"_index":“person”,"_id":“1”}}
{“create”:{"_index":“person”,"_id":“4”}}
{“name”:“4号”,“age”:20,“address”:“重庆”}
{“update”:{"_index":“person”,"_id":2}}
{“doc”:{“name”:“李四2号”,“age”:20,“address”:“重庆”}}
##批量操作 java api操作
###########添加goods索引########################
DELETE goods
PUT goods
{
“mappings”: {
“properties”: {
“title”: {
“type”: “text”,
“analyzer”: “ik_smart”
},
“price”: {
“type”: “double”
},
“createTime”: {
“type”: “date”
},
“categoryName”: {
“type”: “keyword”
},
“brandName”: {
“type”: “keyword”
},
"spec": {
"type": "object"
},
"saleNum": {
"type": "integer"
},
"stock": {
"type": "integer"
}
}
}
}
###############添加文档
POST goods/_doc/1
{
“title”:“小米手机”,
“price”:1000,
“createTime”:“2019-12-01”,
“categoryName”:“手机”,
“brandName”:“小米”,
“saleNum”:3000,
“stock”:10000,
“spec”:{
“网络制式”:“移动4G”,
“屏幕尺寸”:“4.5”
}
}
GET goods/_mapping
##默认情况下,es一次展示10条数据,通过from 和size来分页
GET goods/_search
{
“query”: {
“match_all”: {}
},
“from”: 0,
“size”: 20
}
##term 词条查询,对查询条件不分词 等值查询
GET goods/_search
{
“query”: {
“term”: {
“categoryName”: {
“value”: “手机”
}
}
}
}
##match查询,对查询条件进行分词,默认取分词后的并集 [or]作为条件集合去查询,等值匹配
GET goods/_search
{
“query”: {
“match”: {
“title”: “华为手机”
}
}
}
##设置match结果集 对查询条件进行分词,取分词后的交集 [and]作为条件集合去查询,等值匹配
GET goods/_search
{
“query”: {
“match”: {
“title”: {
“query”:“华为手机”,
“operator”: “and”
}
}
}
}
#wildcard 查询,查询条件分词,模糊查询
##全词匹配查询
GET goods/_search
{
“query”: {
“wildcard”: {
“title”: {
“value”: “华为”
}
}
}
}
##模糊查询
GET goods/_search
{
“query”: {
“wildcard”: {
“title”: {
“value”: “华*”
}
}
}
}
##前缀查询
GET goods/_search
{
“query”: {
“prefix”: {
“brandName”: {
“value”: “华”
}
}
}
}
##正则查询
GET goods/_search
{
“query”: {
“regexp”: {
“title”: {
“value”: “\w(.)*”
}
}
}
}
##范围查询
gte >= ,lte<= gt > ,lt <
##默认是asc的排序 升序
GET goods/_search
{
“query”: {
“range”: {
“price”: {
“gte”: 2000,
“lte”: 3000
}
}
},
“from”: 0,
“size”: 200
}
##指定排序
GET goods/_search
{
“query”: {
“range”: {
“price”: {
“gte”: 2000,
“lte”: 3000
}
}
},
“from”: 0,
“size”: 200,
“sort”: [
{
“price”: {
“order”: “desc”
}
}
]
}
##queryString
#对查询条件分词,
#将分词后的查询条件和词条进行等值匹配
#默认取并集
#可以指定多个查询字段
#支持连接符 AND OR
GET goods/_search
{
“query”: {
“query_string”: {
“fields”: [“title”,“categoryName”,“brandName”],
“query”: “华为 AND 手机”
}
}
}
#不支持连接符 AND OR
GET goods/_search
{
“query”: {
“simple_query_string”: {
“fields”: [“title”,“categoryName”,“brandName”],
“query”: “华为 AND 手机”
}
}
}
#布尔查询 类比MySQL 多表查询
#boolQuery :对多个查询条件连接,连接方式有
must (and) :条件必须成立
must_not (not) :条件必须不成立
should (or) :条件可以成立
filter :条件必须成立 ,性能比must高,因为不会计算得分
GET goods/_search
{
“query”: {
“bool”: {
“must”: [
{
“term”: {
“brandName”: {
“value”: “三星”
}
}
}
]
,
“filter”: {
“term”: {
“title”: “电视”
}
}
}
}
}
#filter
GET goods/_search
{
“query”: {
“bool”: {
“filter”: [
{
“term”: {
“brandName”: {
“value”: “华为”
}
}
}
]
}
}
}
查询需求:
#查询品牌为 华为,标题包含手机,价位在2000-3000
GET goods/_search
{
“query”: {
“bool”: {
“must”: [
{
“term”: {
“brandName”: {
“value”: “华为”
}
}
}
],
“filter”: [
{
“term”: {
“title”: “手机”
}
},
{
“range”:{
“price”:{
“gte”:2000,
“lte”:3000
}
}
}
]
}
}
}
#聚合查询
#指标聚合:相当于Mysql的聚合函数,max,min,avg,sum等
#桶聚合:相当于Mysql的Group By操作,不要对text类型的数据进行分组,会失败.对keyword分组
#查询华为品牌goods的最高价格
GET goods/_search
{
“query”: {
“match”: {
“title”: “华为”
}
},
“aggs”: {
“max_price”: {
“max”: {
“field”: “price”
}
}
}
}
GET goods/_search
{
“query”: {
“match”: {
“title”: “电视”
}
},
“aggs”: {
“brands”: {
“terms”: {
“field”: “brandName”,
“size”: 100
}
}
}
}
#高亮查询
GET goods/_search
{
“query”: {
“match”: {
“title”: “电视”
}
},
“highlight”: {
“fields”: {
“title”: {
“pre_tags”: “”,
“post_tags”: “”
}
}
}
}
##########索引别名,和重建索引
#创建索引,添加映射
PUT stu_index_v1
{
“mappings”: {
“properties”: {
“birthday”:{
“type”: “date”
}
}
}
}
#2添加数据
PUT stu_index_v1/_doc/1
{
“birthday”:“2020-05-13”
}
GET stu_index_v1/_search
#创建新的索引
PUT stu_index_v2
{
“mappings”: {
“properties”: {
“birthday”:{
“type”: “text”
}
}
}
}
#将stu_index_v1的数据拷贝到stu_index_v2
#_reindex 拷贝数据
POST _reindex
{
“source”:{
“index”: “stu_index_v1”
},
“dest”: {
“index”: “stu_index_v2”
}
}
GET stu_index_v2/_search
#思考:脚本中更改了索引名,但是java代码中,还是用的旧索引名,
1.改代码(不推荐)
2.索引别名(推荐)
#步骤
#0.先删除stu_index_v1
DELETE stu_index_v1
#1. 给stu_index_v2起别名 stu_index_v1
POST stu_index_v2/_alias/stu_index_v1
GET stu_index_v1/_search