1.创建索引
PUT /test_index
---这种方始会初始化一个主分片一个副本,不可修改
2.创建索引和分片及副本
PUT /test_index3
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
3.添加索引映射
POST /test_index3/_mapping
{
"properties":{
"name":{
"type":"keyword"
},
"age":{
"type":"integer"
}
}
}
4.创建 索引 分片和副本 映射
PUT /test_index2/
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties":{
"name":{
"type":"keyword"
},
"age":{
"type":"integer"
}
}
}
}
5.关闭索引
POST /test_index/_close
6.打开索引
POST /test_index/_open
文档操作
7.插入文档
//不指定id,es生成
POST /test_index2/_doc
{
"name":"hello","age":10
}
//指定id
PUT /test_index2/_doc/0012
{
"name":"liyongan",
"age":18
}
8.更新文档
//_update操作,必需post 精确修改,不覆盖的方始
POST test_index2/_update/4003
{
"doc": {
"title": "4、CentOS 7.x基本设置123121"
}
}
//覆盖的方始可以为post put
PUT test_index2/_doc/4003
{
"title" : "4、CentOS 7",
"author" : "chengyuqiang",
"content" : "CentOS 7.x基本设置",
"url" : "123"
}
9.查询文档
//match_all搜索,直接返回所有文档 GET/POST
GET test_index2/_search
{
"query": {
"match_all": {}
}
}
//只显示name和age GET/POST
GET test_index2/_search
{
"query": {
"match_all": {}
},
"_source": ["name","age"]
}
//查询name=liyongan GET/POST text类型字段会导致分词
GET test_index2/_search
{
"query": {"match":{"name":"liyongan"}}
}
//精确匹配,不要这种方始查询text字段,因为被分词 GET/POST
GET test_index2/_search
{
"query":{
"term": {
"name": {
"value": "liyongan"
}
}
}
}
GET test_index2/_search
{
"query":{
"terms": {
"name": ["liyongan","hello"]
}
}
}
//通配符查询,返回与通配符模式匹配的文档,?代表匹配任何单个字符,*代表零个或多个字符,通配符不要用在开头,会降低搜索性能,与MySQL中like对应
GET test_index2/_search
{
"query":{
"wildcard": {
"name": {
"value": "liyong*"
}
}
}
}
//前缀查询,返回在提供的字段中包含特定前缀的文档
GET test_index2/_search
{
"query":{
"prefix": {
"name": "liyong"
}
}
}
//模糊查询,返回包含与搜索字词相似的字词的文档
GET test_index2/_search
{
"query":{
"fuzzy": {
"name": "liyong"
}
}
}
//范围查询,返回包含提供范围内的术语的文档,注意日期格式,与MySQL中between and对应
GET test_index2/_search
{
"query":{
"range": {
"age": {
"gte": 120,
"lte": 2000
}
}
}
}
//复合查询
GET test_index2/_search
{
"query":{
"bool": {
"must": {
"term":{
"name":"liyongan"
}
},
"must_not": [
{
"term":{
"age":"19"
}
}
,
{
"term":{
"age":"20"
}
}
],
"should": [
{
"term":{
"age":"19"
}
}
]
}
}
}
10.批量操作文档
//新增
POST /_bulk
{ "create": { "_index": "test_index2", "_id": "11" }}
{ "name":"hello1","age":101 }
{ "create": { "_index": "test_index2", "_id": "22" }}
{"name":"hello11","age":120}
{ "create": { "_index": "test_index2", "_id": "33" }}
{"name":"baobei","age":10}
//删除
POST /_bulk
{ "delete": { "_index": "test_index2", "_id": "11" }}
{ "delete": { "_index": "test_index2", "_id": "22"}}
{ "delete": { "_index": "test_index2", "_id": "33" }}
//修改
POST /_bulk
{ "update": { "_index": "test_index2", "_id": "111" }}
{ "doc":{"name":"hello1","age":1011 }}
{ "update": { "_index": "test_index2", "_id": "212" }}
{ "doc":{"name":"hello21","age":1011 }}
{ "update": { "_index": "test_index2", "_id": "313" }}
{ "doc":{"name":"hello21","age":10111 }}
//查询
GET test_index2/_mget
{
"ids" : ["12","23","123"]
}
//新增or修改
POST /_bulk
{ "index": { "_index": "test_index2", "_id": "1211" }}
{ "doc":{"name":"hello1","age":1011 }}
{ "index": { "_index": "test_index2", "_id": "2122" }}
{ "doc":{"name":"hello21","age":1011 }}
{ "index": { "_index": "test_index2", "_id": "3123" }}
{ "doc":{"name":"hello21","age":10111 }}
POST /uri 创建
DELETE /uri/xxx 删除
PUT /uri/xxx 更新或创建
GET /uri/xxx 查看
POST不用加具体的id,它是作用在一个集合资源之上的(/uri),而PUT操作是作用在一个具体资源之上的(/uri/xxx)。