elasticsearch在kibana中的操作代码
本文最后更新于 2024-07-15,文章内容可能已经过时。
elasticsearch在kibana中的操作代码
GET _search
{
"query": {
"match_all": {}
}
}
# 测试es是否连接
GET /
# 测试分词器
POST /_analyze
{
"text": "这是一首简单的歌曲songs嘤嘤嘤,哦买噶,白嫖简直就是奥利给蓝瘦香菇",
"analyzer": "ik_smart"
}
# 创建索引库
PUT /hgh
{
"mappings": {
"properties": {
"info":{
"type": "text",
"analyzer": "ik_smart"
},
"email":{
"type": "keyword",
"index": false
},
"name":{
"properties": {
"firstName": {
"type": "keyword"
}
}
}
}
}
}
# 查看索引库
GET /hgh
# 修改索引库
PUT /hgh/_mapping
{
"properties": {
"age":{
"type": "long"
}
}
}
# 删除索引库
DELETE /hgh
# 新增文档
POST /hgh/_doc/1
{
"info": "黑马程序员高级Java讲师",
"email": "zy@itcast.cn",
"name": {
"firstName": "云",
"lastName": "赵"
}
}
# 修改文档(全量修改)
PUT /hgh/_doc/1
{
"info": "黑马程序员高级Java讲师",
"email": "zy@itcast.cn",
"name": {
"firstName": "云",
"lastName": "赵"
}
}
# 修改文档(局部修改)
POST /hgh/_update/1
{
"doc": {
"email": "23718494529@qq.com"
}
}
# 查询文档
GET /hgh/_doc/1
#删除文档
DELETE /hgh/_doc/1
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",
"copy_to": "all"
},
"starName":{
"type": "keyword"
},
"business":{
"type": "keyword"
},
"location":{
"type": "geo_point"
},
"pic":{
"type": "keyword",
"index": false
},
"all":{
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
DELETE /hotel
GET /hotel
GET /hotel/_doc/38665
# 批量查询
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": "北京"
}
}
}
}
## rang查询 (greater than or equal 大于等于 less than or equal 小于等于)
GET /hotel/_search
{
"query": {
"range": {
"price": {
"gte": 100,
"lte": 500
}
}
}
}
GET /hotel/_search
{
"query": {
"range": {
"price": {
"gt": 100,
"lt": 500
}
}
}
}
# 地理查询
## 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": "2km",
"location": "31.21, 121.5"
}
}
}
# 复合查询
## function score查询
GET /hotel/_search
{
"query": {
"function_score": {
"query": {"match": {
"all": "外滩"
}},
"functions": [
{
"filter": {
"term": {
"brand": "7天酒店"
}
},
"weight": 2
}
],
"boost_mode": "sum"
}
}
}
## Boolean Query(布尔查询)
GET /hotel/_search
{
"query": {
"bool": {
"must": [
{"term": {"city": "上海" }}
],
"should": [
{"term": {"brand": "皇冠假日" }},
{"term": {"brand": "华美达" }}
],
"must_not": [
{ "range": { "price": { "lte": 500 } }}
],
"filter": [
{ "range": {"score": { "gte": 45 } }}
]
}
}
}
# sort排序
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"score": "desc"
}
]
}
GET /hotel/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"_geo_distance": {
"location": {
"lat": 39.906466,
"lon": 116.409279
},
"order": "asc",
"unit": "km"
}
}
]
}
# 分页查询
GET /hotel/_search
{
"query": {
"match_all": {}
},
"from": 9991,
"size": 100,
"sort": [
{
"_geo_distance": {
"location": {
"lat": 39.906466,
"lon": 116.409279
},
"order": "asc",
"unit": "km"
}
}
]
}
# 高亮查询,默认情况下,es搜索字段必须与高亮字段一致
GET /hotel/_search
{
"query": {
"match": {
"all": "如家"
}
},
"highlight": {
"fields": {
"name": {
"pre_tags": "<em>",
"post_tags": "</em>" ,
"require_field_match": "false"
}
}
}
}
POST /hotel/_update/60935
{
"doc": {
"isAD": true
}
}
# 聚合功能
## Bucket聚合
GET /hotel/_search
{
"size": 0,
"aggs": {
"brandAgg": {
"terms": {
"field": "brand",
"size": 50
}
}
}
}
## Bucket聚合(桶聚合),自定义排序规则
GET /hotel/_search
{
"size": 0,
"aggs": {
"brandAgg": {
"terms": {
"field": "brand",
"order": {
"_count": "asc"
},
"size": 50
}
}
}
}
## Bucket聚合,限定聚合范围
GET /hotel/_search
{
"query": {
"range": {
"price": {
"gte": 100,
"lte": 200
}
}
},
"size": 0,
"aggs": {
"brandAgg": {
"terms": {
"field": "brand",
"size": 50
}
}
}
}
## Metric聚合(度量聚合),与Bucket聚合嵌套使用,获取每个品牌的评分的min、avg等
GET /hotel/_search
{
"size": 0,
"aggs": {
"brandAgg": {
"terms": {
"field": "brand",
"size": 50
},
"aggs": {
"score_stats": {
"stats": {
"field": "score"
}
}
}
}
}
}
## Metric聚合(度量聚合),与Bucket聚合嵌套使用,根据avg进行排序
GET /hotel/_search
{
"size": 0,
"aggs": {
"brandAgg": {
"terms": {
"field": "brand",
"size": 50,
"order": {
"score_stats.avg": "desc"
}
},
"aggs": {
"score_stats": {
"stats": {
"field": "score"
}
}
}
}
}
}
# 拼音分词器测试
POST /_analyze
{
"text":["寻龙分金看缠山,一重缠是一重关;关门如有八重险,不出阴阳八卦形。"],
"analyzer": "pinyin"
}
# 自定义拼音分词器,搜索时也使用自定义拼音分词库
PUT /test
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "ik_max_word",
"filter": "py"
}
},
"filter": {
"py": {
"type": "pinyin",
"keep_full_pinyin": false,
"keep_joined_full_pinyin": true,
"keep_original": true,
"limit_first_letter_length": 16,
"remove_duplicated_term": true,
"none_chinese_pinyin_tokenize": false
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
# 自定义拼音分词器测试
POST /test/_analyze
{
"text":["寻龙分金看缠山,一重缠是一重关;关门如有八重险,不出阴阳八卦形。"],
"analyzer": "my_analyzer"
}
POST /test/_doc/1
{
"id": 1,
"name": "狮子"
}
POST /test/_doc/2
{
"id": 2,
"name": "虱子"
}
GET /test/_search
{
"query": {
"match": {
"name": "掉入狮子笼咋办"
}
}
}
# 删除自定义分词器
DELETE /test
# 自定义拼音分词器,搜索时ik分词库(搜索时应避免使用拼音分词器)
PUT /test
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "ik_max_word",
"filter": "py"
}
},
"filter": {
"py": {
"type": "pinyin",
"keep_full_pinyin": false,
"keep_joined_full_pinyin": true,
"keep_original": true,
"limit_first_letter_length": 16,
"remove_duplicated_term": true,
"none_chinese_pinyin_tokenize": false
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
# 自动补全
## 自动补全的索引库(类型为completion类型)
PUT /test2
{
"mappings": {
"properties": {
"title":{
"type": "completion"
}
}
}
}
## 示例数据(字段值为多词条的数组)
POST test2/_doc
{
"title": ["Sony", "WH-1000XM3"]
}
POST test2/_doc
{
"title": ["SK-II", "PITERA"]
}
POST test2/_doc
{
"title": ["Nintendo", "switch"]
}
## 自动补全查询
GET /test2/_search
{
"suggest": {
"titleSuggest": {
"text": "s",
"completion": {
"field": "title",
"skip_duplicates": true,
"size": 10
}
}
}
}
DELETE /hotel
# 酒店数据索引库
PUT /hotel
{
"settings": {
"analysis": {
"analyzer": {
"text_anlyzer": {
"tokenizer": "ik_max_word",
"filter": "py"
},
"completion_analyzer": {
"tokenizer": "keyword",
"filter": "py"
}
},
"filter": {
"py": {
"type": "pinyin",
"keep_full_pinyin": false,
"keep_joined_full_pinyin": true,
"keep_original": true,
"limit_first_letter_length": 16,
"remove_duplicated_term": true,
"none_chinese_pinyin_tokenize": false
}
}
}
},
"mappings": {
"properties": {
"id":{
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "text_anlyzer",
"search_analyzer": "ik_smart",
"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",
"analyzer": "completion_analyzer"
}
}
}
}
# 查看索引库所有数据
GET /hotel/_search
{
"query": {
"match_all": {}
}
}
# 再次测试自动补全功能
GET /hotel/_search
{
"suggest": {
"suggestion": {
"text": "hq",
"completion": {
"field": "suggestion",
"skip_duplicates": true,
"size": 100
}
}
}
}
- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 程序员Graypigeon
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果