es的操作语句
# ----------- 索引操作
# es的mapping遵循RestFul风格
# 查看所有的索引 ,如果想要显示详细信息,那么就在命令的后边再加上 ?v来显示详细信息
GET /_cat/indices
get /_cat/indices?v
# 查看elasticsearch中的分词器
GET _cat/plugins
# 创建索引,es中的索引相当于数据库中的表。es中没有数据库的概念
# 这里创建的索引设置了副本数为0,因为搭建的es是单点集群,副本没有es服务器可以再分配了
PUT /myindex
{
"settings": {
"number_of_replicas": 0
}
}
# 删除索引
DELETE /myindex
# 查看单个索引的细节 GET /索引名
GET /myindex
# 创建
PUT /my_index
{
"settings": {
"analysis": {
"analyzer": {
"default": {
"type": "ik_smart"
}
}
}
},
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
# 创建一个副本数为0并且默认分词器为ik分词器的索引
PUT /myindex
{
"settings": {
"number_of_replicas": 0,
"analysis": {
"analyzer": {
"default": {
"type": "ik_smart"
}
}
}
},
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
# --------------- 文档操作
# 向索引中添加文档
PUT /myindex/_doc/1
{
"title": "华为手机",
"category": "华为",
"imageUrl": "https://www.huaweicloud.com/",
"price": 5000
}
PUT /myindex/_doc/2
{
"title": "飒爽英姿闯江湖,诗酒茶话莫孤独。"
}
# 查看索引中的文档: 解释 GET /索引名/文档类型/{文档id}
GET /myindex/_doc/1
GET /myindex/_doc/2
# 查询一个索引中所有的文档
GET /myindex/_search
# 更改文档内容 ,
# (全局修改)如果路径中不带 _update 那么默认就是修改所有属性(覆盖)
PUT /myindex/_doc/1
{
"title": "华为mate60手机",
"category": "华为",
"imageUrl": "https://www.huaweicloud.com/",
"price": 5000
}
# (修改部分属性)
POST /myindex/_update/1
{
"doc": {
"title": "华为mate60手机,遥遥领先"
}
}
# 删除文档 DELETE /{索引名}/{文档类型}/{文档id}
DELETE /myindex/_doc/2
# ----- 索引映射操作
# 1.查看索引映射
GET /myindex/_mapping
# 2. 创建索引的时候并且指定静态索引
PUT /my_index
{
"settings": {
"number_of_replicas": 0,
"analysis": {
"analyzer": {
"ik_smart": {
"type": "custom",
"tokenizer": "ik_smart"
},
"ik_max_word": {
"type": "custom",
"tokenizer": "ik_max_word"
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"index": true,
"store": true,
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"category": {
"type": "keyword",
"index": true,
"store": true
},
"images": {
"type": "keyword",
"index": true,
"store": true
},
"price": {
"type": "integer",
"index": true,
"store": true
}
}
}
}
# ---------- 操作DSL语句
# 1.准备数据
PUT /my_index/_doc/1
{"id":1,"title":"华为笔记本电脑","category":"华为","images":"http://www.gulixueyuan.com/xm.jpg","price":5388}
PUT /my_index/_doc/2
{"id":2,"title":"华为手机","category":"华为","images":"http://www.gulixueyuan.com/xm.jpg","price":5500}
PUT /my_index/_doc/3
{"id":3,"title":"VIVO手机","category":"vivo","images":"http://www.gulixueyuan.com/xm.jpg","price":3600}
# 2.无条件查询文档
GET /my_index/_search
# 3.查询所有文档
POST /my_index/_search
{
"query": {
"match_all": {}
}
}
# 4.条件查询文档
#4.1 查询名为title的field中包含 华为 的文档
POSt /my_index/_search
{
"query": {
"match": {
"title": "华为"
}
}
}
#4.2 多字段匹配
POST /my_index/_search
{
"query":{
"multi_match": {
"query": "华为智能手机",
"fields": ["title","category"]
}
}
}
#4.3 关键字查询(term不会进行分词,所以这个时候你要搜索的关键字更多后反而不好匹配)
POST /my_index/_search
{
"query": {
"term": {
"title": {
"value": "华为"
}
}
}
}
# 4.4 关键字查询(多字段关键字查询)
POST /my_index/_search
{
"query": {
"terms": {
"title": [
"华为",
"手机"
]
}
}
}
# 范围查询
POST /my_index/_search
{
"query": {
"range": {
"price": {
"gte": 3000,
"lte": 5000
}
}
}
}
# 指定返回查询字段 (使用_source来指定需要的返回字段)
POST /my_index/_search
{
"query": {
"range": {
"price": {
"gte": 3000,
"lte": 5000
}
}
},
"_source": [
"id","title"
]
}
# ------------- 5组合查询(多添加查询,也就是或与非)
# 5.1 must (与关系)
POST /my_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "华为"
}
},
{
"range": {
"price": {
"gte": 3000,
"lte": 6000
}
}
}
]
}
}
}
# 5.2 should (或关系)
POST /my_index/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "华为"
}
},
{
"range": {
"price": {
"gte": 2000,
"lte": 3000
}
}
}
]
}
}
}
# 5.3 must_not (非关系)
POST /my_index/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"title": "华为"
}
},
{
"range": {
"price": {
"gte": 2000,
"lte": 3000
}
}
}
]
}
}
}
# 5.4 filter(对文档进行过滤,同样也可以直接使用来检索(检索的话与must作用大致相同)。但是filter检索不会对结果进行评分,效率更改)。
# 内部的使用(与关系)
POST /my_index/_search
{
"query": {
"bool": {
"filter": [
{
"match": {
"title": "华为"
}
},
{
"range": {
"price": {
"gte": 2000,
"lte": 6000
}
}
}
]
}
}
}
# 外部的使用(过滤)
POST /albuminfo/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"albumTitle": "小说"
}
},
{
"match": {
"albumIntro": "小说"
}
}
],
"filter": [
{
"term": {
"category3Id": "1152"
}
}
]
}
}
}
# -------------- 6 聚合查询 (类似与mysql中的group by)
# 6.1 得出搜索结果中的最大值 (max)
POST /my_index/_search
{
"query": {
"match_all": {}
},
"size": 1,
"aggs": {
"max_price": {
"max": {
"field": "price"
}
}
}
}
# 6.2 得出搜索结果中的最小值(min)
POST /my_index/_search
{
"query": {
"match_all": {}
},"size": 3,
"aggs": {
"min_price": {
"min": {
"field": "price"
}
}
}
}
# 6.3 得出搜索结果中的某个字段的平均值(avg)
POST /my_index/_search
{
"query": {
"match_all": {}
},
"size": 3,
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
# 6.4 得出搜索结果中某个字段的总和(sum)
POST /my_index/_search
{
"query": {
"match_all": {}
},
"size": 20,
"aggs": {
"sum_price": {
"sum": {
"field": "price"
}
}
}
}
# 6.5 得出搜索结果的统计信息 (stats,这个会统计返回结果集中对应字段的总个数、最小值、最大值、平均值、综合的信息)
POST /my_index/_search
{
"query": {
"match_all": {}
},
"size": 20,
"aggs": {
"stat_price": {
"stats": {
"field": "price"
}
}
}
}
# 6.6 桶聚合 相当于sql中的 group by 语句 (terms)
# 这个在指定要根据哪一个字段进行分组的时候,那个字段必须是不可以被分词的,不然没办法进行指定
POST /my_index/_search
{
"query": {
"match_all": {}
},
"aggs": {
"trems": {
"terms": {
"field": "category",
"size": 10
}
}
}
}
# ---------- 7 排序 (sort -> order [desc|asc])
# 7.1 排序
POST /my_index/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"price": {
"order": "desc"
},
"_score": {
"order": "asc"
}
}
]
}
# ----------- 8 分页查询 (from、size)
POST /my_index/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
# ----------- 9 检索高亮显示(highlight)
# 对搜索结果中的字段进行高亮显示
GET /my_index/_search
{
"query": {
"match": {
"title": "华为"
}
},
"highlight": {
"fields": {
"title": {}
},
"pre_tags": ["<font color:#e4393c>"],
"post_tags": ["</font>"]
}
}
版权声明:
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自
寻梦!
喜欢就支持一下吧