ElasticSearch API

DELETE /school
PUT  /school
{
    "settings": {
        "index": {
                "number_of_shards": 5,
                "number_of_replicas": 1
        }
    }
}

创建文档

注意时间格式是ISO_8601日期格式标准

PUT /school/student/1
{
   "name": "zhangsan",
   "age": 25,
   "course": "elasticsearch",
   "study_date": "2017-06-15T20:30:50+0800"
}

自动生成ID

POST /school/student/
{
   "name": "lisi",
   "age": 26,
   "course": "springboot",
   "study_date": "2017-06-17T20:30:50+0800"
}

创建一个完全新的文档,如果文档存在,则报错.PUT或POST都可以

PUT /school/student/1?op_type=create
{
   "name": "zhangsan",
   "age": 25,
   "course": "elasticsearch",
   "study_date": "2017-06-15T20:30:50+0800"
}

或者写成

PUT /school/student/1/_create
{
   "name": "zhangsan",
   "age": 25,
   "course": "elasticsearch",
   "study_date": "2017-06-15T20:30:50+0800"
}

获取文档文档信息

GET /school/student/1
GET /school/student/AVxEcYVoY8sx3YdxzF0u

检查文档是否存在,有返回200,没有返回404

HEAD /school/student/1

获取文档,指定字段,逗号分隔

GET /school/student/1?_source=name,age

仅获取source字段内容

GET /school/student/1/_source

更新文档,注意version的变化

在 Elasticsearch 中文档是 不可改变 的,不能修改它们。 相反,如果想要更新现有的文档,需要 重建索引 或者进行替换.
在内部,Elasticsearch 已将旧文档标记为已删除,并增加一个全新的文档。
尽管你不能再对旧版本的文档进行访问,但它并不会立即消失。
当继续索引更多的数据,Elasticsearch 会在后台清理这些已删除文档。

PUT /school/student/1
{
   "name": "zhangsan",
   "age": 26,
   "course": "elasticsearch",
   "study_date": "2017-06-15T20:30:50+0800"
}
GET /school/student/1

通过 _update API的方式更新部分字段

POST /school/student/1/_update
{
  "doc": {
    "age": 26,
    "sex": "male"
  }
}
GET /school/student/1

通过脚本更新,脚本语言可以是painless(默认),Groovy,类似javascript

POST /school/student/1/_update
{
   "script" : {
      "lang": "painless",
      "inline":"ctx._source.age+=1"
   }
}

通过脚本添加或修改某个字段

内置支持的语言:painless、groovy
javascript插件安装
sudo bin/elasticsearch-plugin install lang-javascript
python插件安装
sudo bin/elasticsearch-plugin install lang-python

POST /school/student/1/_update
{
   "script" : {
      "lang": "painless",
      "inline":"ctx._source.sex='male'"
   }
}

通过脚本移除某个字段

POST /school/student/1/_update
{
   "script" : "ctx._source.remove('sex')"
}

传参数

POST /school/student/1/_update
{
  "script": {
    "lang": "painless",
    "inline": "ctx._source.name = params.name ",
    "params": {
      "name": "zhangsan"
    }
  }
}

并发控制

通过指定版本号进行更新,只更新指定版本的文档

PUT /school/student/1?version=1
{
   "name": "zhangsan",
   "age": 26,
   "course": "elasticsearch",
   "study_date": "2017-06-15T20:30:50+0800"
}

采用外部版本号,可以更新比当前版本号大的文档

外部版本号,必须是大于0的long型整数
external or external_gte

PUT /school/student/1?version=10&version_type=external
{
   "name": "zhangsan",
   "age": 26,
   "course": "elasticsearch",
   "study_date": "2017-06-15T20:30:50+0800"
}

新版本增加update by query,默认是1000条

POST /school/_update_by_query?scroll_size=1000&conflicts=proceed
{
  "script": {
    "inline": "ctx._source.name6='abc'",
    "lang": "painless"
  },
  "query": {
    "term": {
      "name": "zhangsan"
    }
  }
}

删除文档

注意delete也有version字段,即使文档不存在,version也会增加,这样的好处就是跨多个分布式节点时,保证结果的正确性

DELETE /school/student/1

不可以删除一个type,下面的命令是错误的

DELETE /school/student

可以直接删除一个索引

DELETE /school
GET /school/student/1

新版本重新加入了delete by query

POST school/student/_delete_by_query?conflicts=proceed
{
  "query": {
    "match_all": {}
  }
}

空搜索

GET /_search

获取所有文档,默认返回前10条。

GET /school/student/_search

分页查询

第1页
GET /school/student/_search?size=2
第2页:
GET /school/student/_search?size=2&from=2

轻量搜索

GET /school/student/_search?q=-name:zhangsan
GET /school/student/_search?q=elasticsearch
GET /school/student/_search?q=%2Bstudy_date:<2018-06-17+%2Bname:(zhangsan lisi)

批量获取scoll scan

GET school/_search?scroll=1m
{
  "query": {
    "match_all": {}
  },
  "sort": [
    "_doc"
  ],
  "size": 2
}

GET /_search/scroll
{
    "scroll" : "1m", 
    "scroll_id" : "xxxxxxxxxx" 
}

搜索某一个字段存在值

GET school/_search
{
    "query": {
        "exists" : { "field" : "name" }
    }
}

搜索某一个字段不存在值

GET school/_search
{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "name"
                }
            }
        }
    }
}

你可能感兴趣的:(ElasticSearch API)