使用ES进行增删改查操作

增:

1、逐个增加

/*
put /索引/类型/唯一编号id
     {
          json串
     }
*/
PUT abc/article/1
{
  "id":"1",
  "title":"标题1",
  "content":"content"
}

2、 post /_bulk
批量增加

/*
post  /_bulk   ----批量进行增删改操作
--必须写在同一行
{“create”:{"_index":索引,“_type”:类型,“_id”:编号}}
{json串}
*/
POST /_bulk
{"create":{"_index":"def","_type":"stu","_id":"2"}}
{"id": 2,"studentNo": "BU-POPO-2014-D211", "name": "Tom","major":"Politics", "gpa": 5.8, "yearofBorn": 1745,"classOf": 2000,"interst":"drive"}
{"create":{"_index":"def","_type":"stu","_id":"3"}}
{"id": 3,"studentNo": "BU-POPO-2014-D211", "name": "Faker","major":"Politics", "gpa": 4.5, "yearofBorn": 1777,"classOf": 1999,"interst":"drive"}

批量修改

{“update”:{"_index":索引,“_type”:类型,“_id”:编号}}
“doc”:{json串}

批量删除

{delete:{"_index":索引,“_type”:类型,“_id”:编号}}

改:

/*
post /索引/类型/id/_update
{
   "doc":{
       列名:值//精准修改其中某个列
		}
}
*/
POST abc/article/1/_update
{
  "doc":{"content":"content123"}
}
//会将索引为abc 类型是article 编号为1的  里面content的内容改为content123

删:

// DELETE /索引/类型/唯一id
DELETE abc/article/1

查:

1、用于在特定字段中查找特定值
match 查询
term 查询
range 查询

2、可以包含叶子或者其它的复杂查询语句
bool 查询

例:

1// GET /索引/类型/_search   查询所有
GET abc/article/_search

2//match_all:返回所有文档
GET stu/_search
{
    "query": {
        "match_all": {}
    }
}

3//match:布尔匹配查询:对查询字符串进行分词,根据分词结果构造布尔查询
GET stu/_search
{
    "query": {
        "match": {
          "name":"John Kerry"
        }
    }
}
//查询词为john和kerry,
//只要name字段值中包含有其中任意一个,那么返回该文档

4//match_phrase:短语匹配查询
GET stu/_search
{
    "query": {
        "match_phrase": {
          "name":"John Kerry"
        }
    }
}
// 只要name字段值中包含john和kerry其中任意一个,那么返回该文档

5//match_phrase_prefix:短语前缀匹配查询
GET stu/_search
{
    "query": {
        "match_phrase_prefix": {
          "name":"John Ke"
        }
    }
}
//最后一个分词作为前缀匹配

6//multi_match:多字段匹配查询
GET stu/_search
{
    "query": {
        "multi_match": {
          "query": "John like cooking",
          "fields": ["name","interest"]
        }
    }
}

7//term:词条查询:按照存储在倒排索引中的确切字词,对字段进行匹配
GET stu/_search
{
    "query": {
        "term":{
          "name":"john"
        }
    }
}

8//terms:多词条查询:按照存储在倒排索引中的确切字词,对字段进行多词条匹配
GET stu/_search
{
    "query": {
        "terms":{
          "name":["john","da"]
        }
    }
}

9//range:范围查询
GET stu/_search
{
    "query": {
        "range" : {
            "yearOfBorn" : {
                "gte" : 1995,
                "lte" : 2000
            }
        }
    }
}

10//bool:布尔查询
GET stu/_search
{
    "query": {
      "bool": {
        "must": {
            "match": { "interest": "cooking"} },
        "must_not": {
            "range": { "yearOfBorn": { "gte": 1995, "lte": 2000 }}}
      }
    }
}
//查询喜欢“cooking”且不在1995-2000间出生的学生

你可能感兴趣的:(使用ES进行增删改查操作)