ES的基础操作
索引管理
创建索引
创建索引不带请求体
PUT http://elk101.oldboyedu.com:9200/python #直接搜索
创建索引带请求体
PUT http://elk101.oldboyedu.com:9200/python-4
{
"settings":{
"index":{
"number_of_replicas":1, #副本
"number_of_shards":5 #分片
}
}
}
查看索引(GET)
查看所有:
GRT http://elk101.oldboyedu.com:9200/_cat/indices?v
判断索引是否存在,返回200存在,反之不存在:
HEAD http://elk101.oldboyedu.com:9200/python
删除索引(DELETE)
删除单个索引:
DELETE http://elk101.oldboyedu.com:9200/health_products
批量删除索引:
DELETE http://elk101.oldboyedu.com:9200/python-*
创建索引别名(aliases)
为一个索引创建别名:
POST http://elk101.oldboyedu.com:9200/_aliases
{
"actions":[
{
"add":{"index":"oldboyedu","alias":"oldboy"}
}
]
}
为多个索引创建别名:
POST http://elk101.oldboyedu.com:9200/_aliases
{
"actions":[
{
"add":{"index":"python","alias":"oldboy"}
},
{
"add":{"index":"linux75","alias":"oldboy2"}
}
]
}
为一个索引创建多个别名:
POST http://elk101.oldboyedu.com:9200/_aliases
{
"actions":[
{
"add":{"index":"oldboyedu","alias":"oldboy"}
},
{
"add":{"index":"oldboyedu","alias":"oldboy213"}
}
]
}
关闭索引(_close)
关闭指定索引:
POST http://elk101.oldboyedu.com:9200/linux75/_close
索引
批量关闭索引:
POST http://elk101.oldboyedu.com:9200/linux75-2020*/_close
打开索引(_open)
打开指定索引:
POST http://elk101.oldboyedu.com:9200/linux75/_open
批量打开索引:
POST http://elk101.oldboyedu.com:9200/linux75-2020*/_open
文档管理
创建文档
添加文档:
POST http://elk101.oldboyedu.com:9200/linux/_doc
{
"title":" 养生堂胶原蛋白肽15条/盒 (深海鳕鱼胶原蛋白肽)",
"price":98.90,
"brand":"养生堂",
"shop":"养生堂京东自营旗舰店",
"weight":"390.00g",
"number":"539378",
"users":"通用",
"item":"https://item.jd.com/539378.html"
}
添加文档(定义id字段)
POST http://elk101.oldboyedu.com:9200/linux/_doc/10101
{
"title":"苹果(Apple)MacBook Air 13.3英寸 笔记本电脑 【2020款商务灰】十代i7 16G 512G 官方标配 19点前付款当天发货",
"price":10498.00,
"brand": "苹果",
"weight": "1.29kg",
"item": "https://item.jd.com/10021130510120.html"
}
删除文档:
DELETE http://elk101.oldboyedu.com:9200/linux/_doc/JVNUrnkBZMse5l4lHyEa
删除文档
DELETE http://elk101.oldboyedu.com:9200/linux/_doc/JVNUrnkBZMse5l4lHyEa
修改文档
全局更新修改文档:
POST http://elk101.oldboyedu.com:9200/linux/_doc/JVNUrnkBZMse5l4lHyEa
{
"title": " 养生堂胶原蛋白肽15条/盒 (深海鳕鱼胶原蛋白肽)",
"price": 1000,
"brand": "养生堂",
"shop": "养生堂京东自营旗舰店",
"weight": "390.00g",
"number": "539378",
"users": "通用",
"item": "https://item.jd.com/539378.html"
}
局部更新修改文档:(_update)
POST http://elk101.oldboyedu.com:9200/linux/_update/JVNUrnkBZMse5l4lHyEa
{
"doc":{
"title": " 养生堂胶原蛋白肽15条/盒 (深海鳕鱼胶原蛋白肽)",
"price": 122
}
}
查看文档
基于文档的id进行查看:
GET http://elk101.oldboyedu.com:9200/shonpping/_doc/2983
全查询:
GET http://elk101.oldboyedu.com:9200/shonpping/_search
ES的DSL操作
条件查询
基于请求路径查询:
GET http://elk101.oldboyedu.com:9200/shonpping/_search?q=brand:DELL
基于请求体查询:
POST/GET http://elk101.oldboyedu.com:9200/shonpping/_search?
{
"query":{
"match":{ #匹配
"brand":"Dell"
}
}
}
分页查询
from: 跳过的信息条数,数据条数的偏移量
size: 每页显示的信息条数
查询前20条数据:(默认查看10条数据)
POST http://elk101.oldboyedu.com:9200/shonpping/_search?
{
"size":20
}
查询后5条数据:
POST http://elk101.oldboyedu.com:9200/shonpping/_search?
{
"from":24 #跳过多少条数据
}
每页显示6条数据查询第5页数据:
POST http://elk101.oldboyedu.com:9200/shonpping/_search?
{
"from":24,
"size":6
}
from算法:
(页码值-1)*每页显示的数量
每页显示3条数据查询第一页数据:
POST http://elk101.oldboyedu.com:9200/shonpping/_search?
{
"from":0,
"size":3
}
每页显示3条数据查询第10页数据:
POST http://elk101.oldboyedu.com:9200/shonpping/_search?
{
"from":27,
"size":3
}
每页显示3条数据查询第5页数据:
http://elk101.oldboyedu.com:9200/shonpping/_search
{
"from":12,
"size":3
}
查看返回数据的指定字段
查看返回数据的指定字段:
{
"from":25,
"size":5,
"_source":["price","brand"]
}
查看返回数据的空字段:
{
"from":25,
"size":5,
"_source":[""]
}
排序
根据商品的价格从小到大排序(升序):
GET http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query":{
"match":{
"brand":"苹果"
}
},
"_source":["title","price","brand"],
"sort":{
"price":{
"order":"asc"
}
}
}
根据商品的价格从大到小的排序(降序):
GET http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query":{
"match":{
"brand":"苹果"
}
},
"_source":["title","price","brand"],
"sort":{
"price":{
"order":"desc"
}
}
}
多条件查询
格式:
{
"query":{ #发起查询
"bool":{ #判断语是否满足,布尔查询,也可以理解为多条件查询
"should" / "must":{ #must必须匹配的条件,类似于and.should查询类似于or
"match":{ #匹配
}
}
}
}
}
must查询.类似于and
GET http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"brand": "华为"
}
},
{
"match": {
"price": 4699.00
}
}
]
}
}
}
should查询,类似于or
GET http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"brand": "华为"
}
},
{
"match": {
"brand":"苹果"
}
}
]
}
}
}
must_not查询,类似取反
GET http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"brand": "华为"
}
},
{
"match": {
"brand":"苹果"
}
}
]
}
}
}
should查询,最少匹配
GET http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"brand": "华为"
}
},
{
"match": {
"brand": "苹果"
}
},
{
"match": {
"brand": "小米"
}
},
{
"match": {
"price": 4699
}
}
],
"minimum_should_match": 2
}
}
}
范围查询
查询低于1000的商品(小于lt,大于gt,小于等于le,大于等ge):
POST http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"brand": "小米"
}
}
],
"filter": {
"range": {
"price": {
"lt": 1000
}
}
}
}
}
}
查询大于3000小于5000的商品:
POST http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"brand": "小米"
}
}
],
"filter": {
"range": {
"price": {
"lt":5000,
"gt":3000
}
}
}
}
}
}
全文检索(全文进行查找)
全文检索:
POST http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query":{
"match":{ #不是完全匹配
"brand":"小苹米"
}
}
}
完全匹配:
POST http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query":{
"match_phrase":{ #必须连体来,不可拆分,完全匹配
"brand":"小米"
}
}
}
完全匹配
使用全文匹配进行对比:
POST http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query":{
"match":{ #匹配的是单个字符,类似[]里边的或者
"title":"英寸"
}
}
}
使用完全匹配进行对比:
POST http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query":{
"match_phrase":{ #完全匹配,必须是"英寸"连在一起
"title":"英寸"
}
}
}
例题:
大于3000小于10000的英短品种:
POST http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query":{
"bool":{
"must":[
{
"match_phrase":{
"brand":"英短"
}
}
],
"filter":{
"range":{
"price":{
"gt":3000,
"lt":10000
}
}
}
}
}
}
语法高亮
语法高亮:
POST http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query":{
"match_phrase":{
"title":"手机"
}
},
"highlight":{
"fields":{
"title":{}
}
}
}
精确匹配查找(term trems)
使用term精确查找(只能匹配单个):
POST http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query":{
"term":{
"price":4699
}
}
}
使用terms精确查找(可以匹配多个):
POST http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query":{
"terms":{
"price":[4699,299,279,479]
}
}
}
查询包含指定字段的文档
查询包含指定字段的文档:
exists #操作符,目标存在
POST http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query":{
"exists":{
"field":"brand"
}
}
}
过滤查询
过滤查询:
filter #过滤
POST http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query":{
"bool":{
"filter":{
"match":{
"brand":"Dell"
}
}
}
}
}
多词搜索
默认基于or操作符对某个字段进行多词搜索:
"operator":"and" #默认是or,
POST http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query":{
"bool":{
"must":{
"match":{
"title":{
"query":"曲面"
"operator":"or" #默认就是or
}
}
}
}
},
"_source":[""],
"highlight":{
"fields":{
"title":{}
}
}
}
基于and操作符对某个字段进行多词搜索:
POST http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query":{
"bool":{
"must":{
"match":{
"title":{
"query":"曲面"
"operator":"and"
}
}
}
}
},
"_source":[""],
"highlight":{
"fields":{
"title":{}
}
}
}
权重案例
权重案例:
boost:10 #类似优先级, 优先显示
POST http://elk101.oldboyedu.com:9200/shonpping/_search
{
"query":{
"bool":{
"must":{
"match":{
"title":{
"query":"小米",
"operator":"and"
}
}
},
"should":[
{
"match":{
"title":{
"query":"防水",
"operator":"and"
}
}
},
{
"match":{
"title":{
"query":"黑色",
"operator":"and",
"boost":10
}
}
}
]
}
}
}
ES的批量操作
批量插入
指定索引批量插入(常用):
POST http://elk101.oldboyedu.com:9200/_bulk
{"create":{"_index":"linux"}}
{"name":"oldboy","address":"aaa"}
{"create":{"_index":"linux"}}
{"name":"oldgil","address":"bbb"}
指定索引及类型批量插入:
POST http://elk101.oldboyedu.com:9200/_bulk
{"create":{"_index":"linux","_type":"_doc"}}
{"name":"oldboy2","address":"aaaw"}
{"create":{"_index":"linux","_type":"_doc"}}
{"name":"oldgil2","address":"bbbs"}
指定id批量插入:
POST http://elk101.oldboyedu.com:9200/_bulk
{"create":{"_index":"linux","_type":"_doc","_id":111}}
{"name":"oldboy2","address":"aaaw"}
{"create":{"_index":"linux","_type":"_doc","_id":222}}
{"name":"oldgil2","address":"bbbs"}
批量查询
批量查询: _mget
基于字段查询:
GET http://elk101.oldboyedu.com:9200/linux/_search
{
"query":{
"match":{
"name":"oldboy"
}
}
}
基于id查询:
GET http://elk101.oldboyedu.com:9200/linux/_doc/111
批量获取数据:
GET http://elk101.oldboyedu.com:9200/linux/_doc/_mget
{
"ids":[111,222]
}
批量删除
批量删除:
POST http://elk101.oldboyedu.com:9200/linux/_bulk
{"delete":{"_index":"linux","_id":111}}
{"delete":{"_index":"linux","_id":222}}
批量修改
批量修改:
POST http://elk101.oldboyedu.com:9200/linux/_bulk
{"delete":{"_index":"linux","_id":111}}
{"create":{"_index":"linux","_type":"_doc","_id":333}}
{"name":"oldboy3","address":"aaaw"}
{"delete":{"_index":"linux","_id":222}}
{"create":{"_index":"linux","_type":"_doc","_id":444}}
{"name":"oldgil5","address":"bbbs"}
聚合查询
按照”price“字段计算所有商品的平均值:
{
"aggs":{ // 聚合操作
"price_avg":{ // 该名称可以自定义,我习惯性基于相关字段起名称。
"avg":{ // 分组
"field":"price" // 分组字段
}
}
},
"size": 0 // 设置显示hits数据的大小,当size的值为0时,表示不查看原始数据!如果设置大于0,则显示指定的数据条数。如果设置为-1,则只显示10条,如果设置小于-1则报错!
}
按照brand字段进行分组:
{
"aggs":{ // 聚合操作
"brand_group":{ // 该名称可以自定义,我习惯性基于相关字段起名称。
"terms":{ // 分组
"field":"brand.keyword" // 指定用于计算的相关字段,此处指定的时brand字段,但brand字段的fielddata默认值为false,因此此处我们需要写brand.keyword"
}
}
},
"size": 0 // 设置显示hits数据的大小,当size的值为0时,表示不查看原始数据!如果设置大于0,则显示指定的数据条数。如果设置为-1,则只显示10条,如果设置小于-1则报错!
}