kibana平台对es的DSL使用案例
直接上代码:
#添加 新的分词字典
POST /_analyze
{
"analyzer": "ik_max_word",
"text": "我爱吃饭白嫖奥利给"
}
#创建 名为xuexi1 的索引库
PUT /xuexi1
{
"mappings": {
"properties": {
"info": {
"type": "text",
"analyzer": "ik_smart"
},
"emali": {
"type": "keyword",
"index": false
},
"name": {
"type": "object",
"properties": {
"firstname": {
"type": "keyword"
},
"lastname": {
"type": "keyword"
}
}
}
}
}
}
# 查询指定 索引库
GET /xuexi1
#修改索引库
PUT /xuexi1/_mapping
{
"properties": {
"age": {
"type": "integer"
}
}
}
#删除 索引库
DELETE /xuexi1
#插入文档 POST /索引库名/_doc/文档id
POST /xuexi1/_doc/1
{
"info": "麻辣烫肠粉肥肠螺蛳粉",
"email": "xc@xc.cn",
"name": {
"firstName": "关",
"lastName": "赵"
}
}
#查询文档 GET /{索引库名称}/_doc/{id
}
GET /xuexi1/_doc/1
#删除 文档DELETE /{索引库名}/_doc/id值
# 根据id删除数据
DELETE /xuexi1/_doc/1
#全量修改 也就是 根据id 删除 再 新增 如果id没有直接新增
PUT /xuexi1/_doc/1
{
"info": "麻辣烫肠粉肥肠螺蛳粉",
"email": "xc@xc.cn",
"name": {
"firstName": "关",
"lastName": "颖儿"
}
}
#局部修改文档字段
#POST /{索引库名}/_update/文档id
#{
# "doc": {
# "字段名": "新的值",
#
}
#}
POST /xuexi1/_update/1
{
"doc": {
"email": "xiugai@xc.cn"
}
}
#酒店mapping
PUT /hotel
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "text",
"analyzer": "ik_max_word",
"copy_to": "all"
},
"address": {
"type": "keyword",
"index": false
},
"price": {
"type": "integer"
},
"score": {
"type": "integer"
},
"brand": {
"type": "keyword",
"copy_to": "all"
},
"city": {
"type": "keyword"
},
"starName": {
"type": "keyword"
},
"business": {
"type": "keyword",
"copy_to": "all"
},
"location": {
"type": "geo_point"
},
"pic": {
"type": "keyword",
"index": false
},
"all": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
GET /hotel/_doc/61083
#查询所有
GET /hotel/_search
{
"query": {
"match_all": {}
}
}
#全文检索查询 match 查询 这里的all 是copyto定义的,支持分词 君, 君悦 均可搜索
GET /hotel/_search
{
"query": {
"match": {
"all": "君"
}
}
}
#当全文检索查询 match 查询 用在 keyword类型的字段时,不支持分词,除非输入的值是完整的例如 君悦
#输入君
GET /hotel/_search
{
"query": {
"match": {
"brand": "君"
}
}
}
#全文检索查询 mutil_match 查询 ,fields为 多个字段名,query为要搜索内容
#搜索字段名越多 ,效率越低 建议 使用copyto
GET /hotel/_search
{
"query": {
"multi_match": {
"query": "外滩如家",
"fields": [
"brand",
"name",
"business"
]
}
}
}
#精准查询 term查询 基于不能分词的类型
GET /hotel/_search
{
"query": {
"term": {
"city": {
"value": "上海"
}
}
}
}
#范围查询 price 为要查范围的字段名,这里要查价格范围
#gte 大于等于 lte 小于等于
#gt 大于 lt 小于
GET /hotel/_search
{
"query": {
"range": {
"price": {
"gte": 1000,
"lte": 2000
}
}
}
}
#地理查询 矩形坐标范围查询geo_bounding_box查询
#"top_left": { # 左上点
#"bottom_right": { // 右下点
#location 为自定义字段的地理位置
GET /indexName/_search
{
"query": {
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 31.1,
"lon": 121.5
},
"bottom_right": {
"lat": 30.9,
"lon": 121.7
}
}
}
}
}
#地理查询 附近查询 距离查询(geo_distance)查询
#"distance": "15km", // 半径
#"location": "31.21,121.5" // 圆心 "纬度,经度"
GET/indexName/_search
{
"query": {
"geo_distance": {
"distance": "15km",
"location": "31.21,121.5"
}
}
}
#相关性查询 相关性算分 让某些 值增加排名 得高分 ,排名靠前
#query 放入原始的query 也即是上面的案例, functions 就是 调整算分的
# 其中三个要素 , filter 就是 确定要加分的 查询类型和查询字段值
# 函数表达式 这里案例是 weight ,也可能是其他值如random_score等 就是对结果 经过该函数的得分
#boost_mode 加权模式 也就是 把query_score 和function_score 经过 某种加权后的最终得分,默认是相乘
GET /hotel/_search
{
"query": {
"function_score": {
"query": {
"match": {
"all": "外滩"
}
},
"functions": [
{
"filter": {
"term": {
"brand": "如家"
}
},
"weight": 8
}
],
"boost_mode": "multiply"
}
}
}
#布尔查询 多条件的查询 里面可以使 全文检索查询,精准查询 等 是一个或多个查询子句的组合,每一个子句就是一个子查询。子查询的组合方式有:
#- must:必须匹配每个子查询,类似“与”
#- should:选择性匹配子查询,类似“或”
#- must_not:必须不匹配,不参与算分,类似“非”
#- filter:必须匹配,不参与算分
GET /hotel/_search
{
"query": {
"bool": {
"must": [
{
"mathc": {
"name": "如家"
}
}
],
"must_not": [
{
"range": {
"price": {
"gt": 400
}
}
}
],
"filter": [
{
"geo_distance": {
"distance": "10km",
"location": {
"lat": 31.21,
"lon": 121.5
}
}
}
]
}
}
}
#小结 符合查询 从层级来看 应该 相关性查询可以包含 以上各种查询
#从第二个query字段开始 为bool 查询时 可以组合多种类型的查询 如 全文,精准,范围,地理
#而bool 查询 又在 function_score中
GET /hotel/_search
{
"query": {
"function_score": {
"query": {},
"functions": [
{}
],
"boost_mode": "multiply"
}
}
}
#从查询 层级看 query 在 第一层级 与他同级的还有 高亮 ,分页,和排序
#排序 普通排序
#需求描述:酒店数据按照用户评价(score)降序排序,评价相同的按照价格(price)升序排序
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"score": {
"order": "desc"
},
"price": {
"order": "asc"
}
}
]
}
#地理坐标排序
#location 自定义的地理位置字段
#lat 经度 lon 纬度 以此为圆心 半径距离排序
#order排序方式
#unit 单位 为 km
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"_geo_distance": {
"location": {
"lat": 23,
"lon": 120
},
"order": "asc",
"unit": "km"
}
}
]
}
#查询分页 与 query同级
# "from": 990, //分页开始的位置,默认为0
# "size": 10, //期望获取的文档总数
GET/hotel/_search
{
"query": {
"match_all": {}
},
"from": 990,
"size": 10,
"sort": [
{
"price": "asc"
}
]
}
#- 高亮是对关键字高亮,因此**搜索条件必须带有关键字**,而不能是范围这样的查询。
#- 默认情况下,**高亮的字段,必须与搜索指定的字段一致**,否则无法高亮
#- 如果要对非搜索字段高亮,则需要添加一个属性:required_field_match=false
#这里是对自定义 name 字段高亮
GET /hotel/_search
{
"query": {
"match_all": {
"all": "喜来登"
}
},
"highlight": {
"fields": {
"name": {
"require_field_match": "false"
}
}
}
}
#聚合三要素 聚合名称(自定义),聚合类型,要聚合的字段
#数据聚合 根据term 桶聚合
# "size": 0 表示不显示doc文档信息,只要聚合结果
#自定一个聚合名为 "brandAgg"
#AGG_TYPE 聚合类型 为terms
#field 聚合字段 填写 要聚合的字段 ,这里根据品牌名brand 聚合
# "size": 10 显示前十个聚合结果
GET /hotel/_search
{
"size": 0,
"aggs": {
"brandAgg": {
"terms": {
"field": "brand",
"size": 10
}
}
}
}
#数据聚合 自定义排序规则 根据term 桶聚合
# "size": 0 表示不显示doc文档信息,只要聚合结果
#自定一个聚合名为 "brandAgg"
#AGG_TYPE 聚合类型 为terms
#field 聚合字段 填写 要聚合的字段 ,这里根据品牌名brand 聚合
# "size": 10 显示前十个聚合结果
# "_count": "asc" 品牌数升序排序
GET /hotel/_search
{
"size": 0,
"aggs": {
"brandAgg": {
"terms": {
"field": "brand",
"size": 10,
"order": {
"_count": "asc"
}
}
}
}
}
#数据聚合 自定义排序规则 范围查询 根据term 桶聚合
#对自定义字段price 价格 作为范围查询后 在数据聚合 ,价格小于200的
# "size": 0 表示不显示doc文档信息,只要聚合结果
#自定一个聚合名为 "brandAgg"
#AGG_TYPE 聚合类型 为terms
#field 聚合字段 填写 要聚合的字段 ,这里根据品牌名brand 聚合
# "size": 10 显示前十个聚合结果
# "_count": "asc" 品牌数升序排序
GET /hotel/_search
{
"query": {
"range": {
"price": {
"lte": 200
}
}
},
"size": 0,
"aggs": {
"brandAgg": {
"terms": {
"field": "brand",
"size": 10,
"order": {
"_count": "asc"
}
}
}
}
}
#聚合三要素 聚合名称(自定义),聚合类型,要聚合的字段
#嵌套聚合 根据聚合结果再聚合 metric聚合
# "size": 0 表示不显示doc文档信息,只要聚合结果
#自定一个聚合名为 "brandAgg"
#AGG_TYPE 聚合类型 为terms
#field 聚合字段 填写 要聚合的字段 ,这里根据品牌名brand 聚合,field 的value值为 要聚合的字段
# "size": 20 显示前20个聚合结果
#"order" 是根据桶里的字段结果进行排序
#scoreAgg 自定义子聚合的聚合名,stats为聚合类型,
#scoreAgg.avg 根据子聚合的平均分 作了降序排名
#根据品牌聚合之后 再对每个品牌 进行 度量聚合
GET /hotel/_search
{
"size": 0,
"aggs": {
"brandAgg": {
"terms": {
"field": "brand",
"size": 20,
"order": {
"scoreAgg.avg": "desc"
}
},
"aggs": {
"scoreAgg": {
"stats": {
"field": "score"
}
}
}
}
}
}