操作练习
# kibana
- Dev Tools的 操作 记录
GET _search
{
"query": {
"match_all": {}
}
}
#测试es是否链接
GET /
#测试标准分词器
POST /_analyze
{
"text":"黑马程序员学习java太棒了!",
"analyzer": "standard"
}
#测试ik分词器
POST /_analyze
{
"text":"啊黑马程序员学习java太棒了!",
//"analyzer": "ik_max_word" //最细切分
"analyzer": "ik_smart" //最少切分
}
#测试ik分词器+自定义字典
POST /_analyze
{
"text":"哦嗯雄的凉冰太厉害了,比英雄联盟的皎月女神强啊!",
"analyzer": "ik_smart"
}
#创建索引库
PUT /diana
{
"mappings": {
"properties": {
"info": {
"type": "text",
"analyzer": "ik_smart"
},
"email": {
"type": "keyword",
"index": false
},
"name": {
"type": "object",
"properties": {
"firstName": {
"type": "keyword"
},
"lastName": {
"type": "keyword"
}
}
}
}
}
}
#查询索引库
GET /diana
#修改索引库,只允许添加新字段,不允许修改已经存在的字段
PUT /diana/_mapping
{
"properties": {
"age": {
"type":"integer"
}
}
}
#删除索引库
DELETE /diana
#新增文档
POST /diana/_doc/1
{
"info":"皎月女神",
"email":"123@123.com",
"name": {
"firstName": "冰",
"lastName": "凉"
}
}
#查询文档
GET /diana/_doc/1
#删除文档
DELETE /diana/_doc/1
#修改文档--全量修改
PUT /diana/_doc/2
{
"info":"皎月女神",
"email":"123@1234.com",
"name": {
"firstName": "冰",
"lastName": "凉"
}
}
#修改文档--增量修改
POST /diana/_update/1
{
"doc": {
"info": "diana"
}
}
#创建酒店索引
PUT /hotel
{
"mappings": {
"properties": {
"id":{//keyword 类型,不是long
"type":"keyword"
},
"name": {
"type":"text",
"analyzer": "ik_max_word",
"copy_to": "all"
},
"address":{
"type":"text",
"analyzer": "ik_smart"
},
"price":{
"type":"integer",
"copy_to": "all"
},
"score":{
"type":"integer",
"copy_to": "all"
},
"brand":{
"type": "keyword",
"copy_to": "all"
},
"city":{
"type": "keyword",
"copy_to": "all"
},
"starName":{
"type": "keyword",
"copy_to": "all"
},
"business":{
"type": "keyword",
"copy_to": "all"
},
"location":{//地理位置特殊字段,包括经度和纬度
"type":"geo_point"
},
"pic":{//不分词,不搜索
"type":"keyword",
"index":false
},
"all": {//将许多字段的值聚在一块,方便搜索
"type":"text",
"analyzer": "ik_max_word"
}
}
}
}
#查看酒店索引
GET /hotel
#删除酒店索引
DELETE /hotel
#得到酒店文档信息
GET /hotel/_doc/36934
#批量查询
GET /hotel/_search
#对文档内信息的检索###############
###查询所有
GET /hotel/_search
{
"query": {
"match_all": {}
}
}
###全文检索
#match 单个字段查询 --性能高,推荐
GET /hotel/_search
{
"query": {
"match": {
"all": "外滩如家"
}
}
}
#multi_match 查询多个字段
GET /hotel/_search
{
"query": {
"multi_match": {
"query": "外滩如家",
"fields":["brand","name", "business"]
}
}
}
###精确查询
#term 根据词条精确值查询
GET /hotel/_search
{
"query": {
"term": {
"city": {
"value": "上海"
}
}
}
}
#range 根据值的范围查询
GET /hotel/_search
{
"query": {
"range": {
"price": {
"gte": 100, //带e是等于 大于等于-gte;大于gt
"lte": 300
}
}
}
}
###地理查询
#geo_bounding_box 矩形范围查询,左上,右下坐标
GET /hotel/_search
{
"query": {
"geo_bounding_box": {
"location": {
"top_left": {//左上
"lat": 31.1,
"lon": 121.5
},
"bottom_right": {//右下
"lat": 30.9,
"lon": 121.7
}
}
}
}
}
#geo_distance 距离查询 以坐标为中心画一个圆 --常用
GET /hotel/_search
{
"query": {
"geo_distance": {
"distance": "5km",
"location": "31.21, 121.5"
}
}
}
###复合查询
#默认按相关度打分,现在要修改打分数 function_score
GET /hotel/_search
{
"query": {
"function_score": {
"query": {//正常查询
"match": {
"all": "外滩"
}
},
"functions": [
{
"filter": {//筛选条件
"term": {
"brand": "如家"
}
},
"weight": 10 //算分权值 默认是乘算
}
],
"boost_mode": "sum"//乘法运算
}
}
}
#布尔查询 一个或多个查询子句的组合
GET /hotel/_search
{
"query": {
"bool": {
"must": [//必须是如家品牌
{
"term": {
"brand": {
"value": "如家"
}
}
}
],
"must_not": [//必须不大于400 -- 小于等于400
{
"range": {
"price": {//大于400
"gt": 400
}
}
}
],
"filter": [//筛选范围
{
"geo_distance": {
"distance": "10km",
"location": "31.21, 121.5"
}
}
]
}
}
}
#搜索结果处理###############
###排序
#普通字段排序
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [//先按评分降序,在按价格升序
{
"score": "desc"
},
{
"price": "asc"
}
]
}
#地理坐标排序
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": "asc"
},
{
"_geo_distance": {
"location": {
"lat": 40, //纬度
"lon": -70 //经度
},
"order": "desc",
"unit": "km"
}
}
]
}
###分页
#基本分页
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": "asc"
}
],
"from": 10, //从10开始
"size": 20 //一页数目 20
}
#深度分页
#after search
###高亮
GET /hotel/_search
{
"query": {
"match": {
"all": "如家"
}
},
"highlight": {
"fields": {// 指定要高亮的字段
"name": {//默认就是加 <em></em>
"require_field_match": "false"//默认是要字段匹配,这是设置成不需要匹配
}
}
}
}
#添加广告标记
POST /hotel/_update/2056126831
{
"doc": {
"isAD": true
}
}
POST /hotel/_update/1989806195
{
"doc": {
"isAD": true
}
}
POST /hotel/_update/2056105938
{
"doc": {
"isAD": true
}
}
GET /hotel/_doc/2056105938
###############聚合
#对品牌排序
GET /hotel/_search
{
"size": 0,
"aggs": {
"brandAggs": {
"terms": {
"field": "brand"
"size": 10
}
}
}
}
#对品牌排序 自定义排序,按升序排列
GET /hotel/_search
{
"size": 0,
"aggs": {
"brandAggs": {
"terms": {
"field": "brand",
"size": 10,
"order": {
"_count": "asc"
}
}
}
}
}
#对品牌排序 限定聚合范围-限定范围小于200
GET /hotel/_search
{
"query": {
"range": {
"price": {
"lte": 200
}
}
},
"size": 0,
"aggs": {
"brandAggs": {
"terms": {
"field": "brand",
"size": 10,
"order": {
"_count": "asc"
}
}
}
}
}
#Metric聚合
GET /hotel/_search
{
"size": 0,
"aggs": {
"brandAgg": {
"terms": {
"field": "brand",
"size": 20,
"order": {
"scoreAgg.avg": "asc"
}
},
"aggs": {
"scoreAgg": {
"stats": {
"field": "score"
}
}
}
}
}
}
# 多字段聚合
GET /hotel/_search
{
"size": 0,
"aggs": {
"brandAggs": {
"terms": {
"field": "brand",
"size": 3
}
},
"cityAgg": {
"terms": {
"field": "city",
"size": 3
}
},
"starNameAgg": {
"terms": {
"field": "starName",
"size": 10
}
}
}
}
#### 拼音分词器
POST /_analyze
{
"text": ["如家酒店还不错"],
"analyzer": "pinyin"
}
## 自动补全测
## 自动补全的索引库
PUT test
{
"mappings": {
"properties": {
"title":{
"type": "completion"
}
}
}
}
## 示例数据
POST test/_doc
{
"title": ["Sony", "WH-1000XM3"]
}
POST test/_doc
{
"title": ["SK-II", "PITERA"]
}
POST test/_doc
{
"title": ["Nintendo", "switch"]
}
##自动补全查询
POST /test/_search
{
"suggest": {
"title_suggest": {
"text": "s", // 关键字
"completion": {
"field": "title", // 补全字段
"skip_duplicates": true, // 跳过重复的
"size": 10 // 获取前10条结果
}
}
}
}
#创建带自动补全功能的 酒店数据索引库
PUT /hotel
{
"settings": {
"analysis": {
"analyzer": {
"text_anlyzer": {//自定义分词器名称
"tokenizer": "ik_max_word",//中间用ik分割词语
"filter": "py"//最后用拼音分词器处理
},
"completion_analyzer": {
"tokenizer": "keyword",//中间 不用词语分割
"filter": "py" //最后用拼音分词器处理
}
},
"filter": {
"py": {
"type": "pinyin",
"keep_full_pinyin": false,// 刘德华> [liu,de,hua],默认为true
"keep_joined_full_pinyin": true,//刘德华> [liudehua],默认为false
"keep_original": true,//保留原始的汉字输入
"limit_first_letter_length": 16,
"remove_duplicated_term": true,//移除重复,[de的]>de
"none_chinese_pinyin_tokenize": false//保持拼音连续,不拆开
}
}
}
},
"mappings": {
"properties": {
"id":{
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "text_anlyzer",//创建索引时 使用自定义混合分词器
"search_analyzer": "ik_smart",//搜索时 使用ik分词器
"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": "text_anlyzer",
"search_analyzer": "ik_smart"
},
"suggestion":{//关键词,自动补全字段
"type": "completion",//必须是completion类型
"analyzer": "completion_analyzer"//不分词,直接转拼音
}
}
}
}
GET /hotel/_search
#自动补全测试
POST /hotel/_search
{
"suggest": {
"suggestions": {
"text": "ff", // 前缀词
"completion": {
"field": "suggestion", // 补全字段
"skip_duplicates": true, // 跳过重复的
"size": 10 // 获取前10条结果
}
}
}
}
DELETE /hotel/_doc/1586232717316648962
GET /hotel/_search
{
"query": {
"match": {
"all": "2"
}
}
}
上次更新: 2023 07 3
