ES常用操作

ES常用操作

1. 查看基本配置

1.1 查看配置

# 获取单个index(库的配置)
GET /index/_settings   

# 获取全部配置,全部库的配置
GET /_settings/_all     

1.2 查看所有索引

GET /_cat/indices?v

1.2 查看索引中所有字段

GET /index/type/_mapping

2. query DSL(Domain Sepcified Language)

2.1 简单查询

GET /index/type/_search?q=id:300421

2.2 获取所有记录

# 获取所有记录
POST /index/type/_search
{
    "query":{"match_all":{}}
}

# 查询 es 中所有的文档,并按照某一个条件排序
GET /index/type/_search?q=*&sort=_id:asc&pretty

2.3 根据条件查询

GET /index/type/_search
{
    "query":{
          "match":{
                    "name":"zhangsan"
          }
    },
    "sort":[
        {
             "age":"desc"
       }
   ]
}

2.4 分页查询

GET /index/type/_search
{
    "query":{"match_all":{}},
    "from":1,
    "size":2
}

2.5 指定查询结果的字段

GET /index/type/_search
{
    "query":{"match_all":{}},
    "_source":["name","age"]
}

2.6 query filter

GET /index/type/_search
{
    "query":{
          "bool":{
                    "must":{
                             "match":{
                                   "name":"zhangsan"
                             }
                     },
                     "filter":{
                              "range":{
                                      "age":{"gt":25}
                             }
                    }
          }
    },
    "sort":[
        {
             "age":"desc"
       }
   ]
}
GET /index/type/_search
{ 
    "query":{
          "match":{
                  "name":"zhangsan"
            }              
    } 
}

2.8 phrase search(短语搜索:完全匹配)

GET /index/type/_search
{ 
    "query":{
          "match_phrase":{
                  "name":"zhangsan"
            }              
    } 
}

2.9 highlight search(高亮搜索)

GET /index/type/_search
{ 
    "query":{
          "match_phrase":{
                  "name":"zhangsan"
            }              
    } ,
   "highlight":{
              "fields":{
                    "name":{}
               }
   }
}

3.新增数据

3.1 插入数据:指定id

PUT /index/type/1 
{
	"name": "张三",
	"age": 20,
	"date": "2019-10-29"
}

3.2 插入数据:使用es默认创建的id

POST /index/type
{
	"name": "李四",
	"age": 29,
	"date": "2019-10-31"
}

4.修改数据

4.1 向已有索引中添加新字段

PUT /index/type/_mapping
{
    "properties": {
        "name": {
            "type": "text", # 这里为增量更新,原有字段一定要保留,否则失败
            "fields": { # 新增的部分
                "standard": {
                    "type": "text",
                    "analyzer": "standard"
                }
            },
            "analyzer": "my_ik_max_word",
            "search_analyzer": "my_ik_smart"
        }
}

4.2 批量更新某个字段的值

POST /index/type/_update_by_query
{
   "script": {
        "inline": "ctx._source.name = params.name;ctx._source.age=params.name", 
       "params": {
           "name": "zhangsan",
           "age": ctx._source.age + 1 # 这里也可以为通过本字段中某个值通过计算后再赋值
       },
        "lang": "painless" # painless 高性能模式
    },
    "query": { # 这里为筛选条件,会更新满足条件的值,不写或match_all会更新全部
        "terms": {
            "_id": ["1"]
        }
    }
}

5. 删除数据

5.1 删除指定id

POST /index/type/_delete_by_query
{
    "query": {
        "terms": {
            "_id": ["123"]
        }
    }
}

备注:这里只写了部分情况的,后续会根据学习工作过程中遇到的新需求不断补充完善。

posted @ 2020-10-20 16:16  油饼er  阅读(504)  评论(0编辑  收藏  举报