ES基本操作

0. 补充

  • 下划线开头的是es中自带的关键字

1. 操作索引

  • GET /_all:查看所有索引
  • PUT /goods_index:添加索引goods_index
  • GET /goods_index1,goods_index2:查询多个索引
  • DELETE /goods_index:删除索引
  • POST /goods_index/_close:关闭索引
  • POST /goods_index/_open:打开索引

2. 映射操作

2.1 添加映射

向已存在的索引中添加映射字段(增量)

PUT person/_mapping
{
  "properties":{
    "name":{
      "type":"keyword"
    },
    "age":{
      "type":"integer"
    },
    "address":{
      "type":"text"
    }
  }
}

创建索引时直接添加映射

PUT person
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "age": {
        "type": "integer"
      }
    }
  }
}

GET /person/_mapping :查询映射

3. 文档操作

3.1 添加文档

指定id方式添加文档,也是修改文档

PUT person/_doc/1
{
  "name":"张三",
  "age":20,
  "address":"北京海淀区"
}

不指定id添加文档,只能用post方式

POST person/_doc
{
  "name":"张三",
  "age":20,
  "address":"北京海淀区"
}

3.2 删除文档

DELETE person/_doc/1:删除指定id的文档

4. 文档查询操作

4.1 简单查询

GET /person/_doc/1:查询指定id的文档
GET /person/_search:查询指定索引下的所有文档

4.2 term词条查询

term为词条等值匹配

Get person/_search
{
  "query":{
    "term": {
      "address": {
        "value": "北京"
      }
    }
  }
}
  • address有词条"北京"的就算命中,即不会对查询条件"北京"进行分词

4.3 match查询

match为分词后等值匹配

Get person/_search
{
  "query":{
    "match": {
      "address": "北京昌平"
    }
  }
}
  • 先会对查询的条件字符串进行分词,再查询。默认取并集
Get person/_search
{
  "query":{
    "match": {
      "address": {
        "query": "北京昌平", # 查询条件,会进行分词
        "operator": "and"   # or / and
      }
    }
  }
}

4.4 matchAll查询

  • 查询所有文档
Get goods/_search
{
  "query":{
    "match_all": {}
  },
  "from":0,
  "size":100
}

4.5 分词后模糊匹配

模糊查询

Get person/_search
{
  "query":{
    "wildcard": {
      "address": {
        "value": "*京?"			# 包含京
      }
    }
  }
}

正则匹配

Get person/_search
{
  "query":{
    "regexp": {
      "address": {
        "value": "\\w+(.)*"
      }
    }
  }
}

前缀查询

Get person/_search
{
  "query":{
    "prefix": {
      "address": {
        "value": "三星"
      }
    }
  }
}

5.6 范围查询

Get goods/_search
{
  "query": {			# 查询
    "range": {
      "price": {
        "gte": 10,
        "lte": 20
      }
    }
  },
  "sort": {				# 排序
    "price": {
      "order": "desc"
    }
  }
}

5.7 queryString

queryString

  • 会对查询条件进行分词
  • 然后将分词后的查询条件和词条进行等值匹配
  • 默认去并集
  • 可以指定多个查询字段
GET goods/_msearch
{
  "query":{
    "query_string":{
      "fields":["title","categoryName","brandName"],
      "query":"华为手机"
    }
  }
}

GET goods/_msearch
{
  "query":{
    "query_string":{
      "fields":["title","categoryName","brandName"],
      "query":"华为 AND 手机"   # 支持AND OR 连接符
    }
  }
}

GET goods/_msearch
{
  "query":{
    "simple_query_string":{			 # 不支持AND OR 连接符
      "fields":["title","categoryName","brandName"],
      "query":"华为手机"  
    }
  }
}

5.8 布尔查询

  • 其实时多个条件的联合查询
  • boolQuery:对多个查询条件连接,连接方式有如下
    • must(and):多个条件必须同时成立
    • must_not(not):多个条件必须都不成立
    • shoud(or):多个条件至少一个成立
    • filter:条件必须成立,性能比must高,不会计算得分
Get 索引名称/_search
{
  "query": {
    "bool": {
      "must": [{}],			
      "filter": [{}],
      "must_not": [{}],
      "should": [{}]
    }
  }
}

# 实例
Get person/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "address": {
              "query": "北京昌平",
              "operator": "or"
            }
          }
        }
      ]
    }
  }
}

5.9 聚合查询

  • 指标聚合:相当于mysql中的聚合函数,如max,min,avg,sum等
  • 桶聚合:相当于mysql的group by 操作。不要对text类型的数据进行分组,会失败

指标聚合

GET goods/_search
{
  "query": {
    "match": {
      "title": "华为"
    }
  },
  "aggs":{
    "max_price":{		# 聚合后结果名称,类似mysql的as取别名
      "max":{			# 聚合方式:指标聚合
        "field": "price"
      }
    }
  }
}

桶聚合


GET goods/_search
{
  "query": {
    "match": {
      "title": "手机"
    }
  },
  "aggs":{
    "goods_brands":{				# 聚合别名
      "terms": {					# 聚合方式:桶聚合	
        "field": "brandName",		# 聚合字段
        "size": 100				
      }
    }
  }
}

5.10 高亮查询

高亮三要素

  • 高亮字段
  • 前缀
  • 后缀
GET goods/_search
{
  "query": {
    "match": {
      "title": "电视"
    }
  },
  "highlight": {
    "fields": {
      "title": {
        "pre_tags": "<font color='red'>",
        "post_tags": "</font>"
      }
    }
  }
}

6. 批量操作

POST _bulk
{"delete":{"_index":"person","_id":"1"}}   	# 删除
{"create":{"_index":"person","_id":"8"}}	#添加
{"name":"八号","age":80,"address":"北京"}	#添加的数据
{"update":{"_index":"person","_id":"2"}}	#修改
{"doc":{"name":"二号"}}						#修改的数据
  • json不能换行

7. 分词操作

GET _analyze
{
  "analyzer": "standard",
  "text": ["北京欢迎你"]
}
GET _analyze
{
  "analyzer": "ik_smart",
  "text": ["北京欢迎你"]
}
GET _analyze
{
  "analyzer": "ik_smart",
  "text": ["北京欢迎你"]
}

创建映射时指定分词器

PUT person
{
  "mappings": {
    "properties": {
      "name":{
        "type": "keyword"
      },
      "address":{
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}

8. 重建索引

  • 其实是重新创建一个索引,然后将原索引中文档移动到新的索引中

将v1索引中的文档移动到v2

POST _reindex
{
  "source": {
    "index": "student_index_v1"
  },
  "dest": {
    "index": "student_index_v2"
  }
}

给v2的索引取一个别名POST student_index_v2/_alias/student_index_v1

  • 需要保证es中原先不存在student_index_v1这个索引或者别名
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值