elasticsearch 数据 添加,更新,删除,查询

张映 发表于 2018-05-09

分类目录: elasticsearch, 服务器相关

标签:, , , ,

上篇文章说了,elasticsearch mapping字段的增,删,更新。如果把mapping的修改理解成对数据结构的修改,那这篇文章就可以理解成对数据的修改。

1,添加数据

$ curl -XPOST "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"  -H "Content-Type: application/json" -d '
{
    "id" : 3,
    "username" :  "测试测试",
    "description" :  "测试测试"
}'

2,更新数据

2.1,部分数据更新

$ curl -XPOST 'localhost:9200/ik_v2/chinese/3/_update?pretty' -H "Content-Type: application/json" -d '{
    "doc" : {
            "username" : "testtest"
        }
    }
}'

$ curl -XGET "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"
{
  "_index" : "ik_v2",
  "_type" : "chinese",
  "_id" : "3",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "id" : 3,
    "username" : "testtest",  //部分更新了
    "description" : "测试测试"
  }
}

2.2,全部更新

curl -XPOST "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"  -H "Content-Type: application/json" -d '
{
    "id" : 4,
    "username" :  "111111111",
    "description" :  "222222222"
}'

//id为3的数据全部更新了
$ curl -XGET "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"
{
  "_index" : "ik_v2",
  "_type" : "chinese",
  "_id" : "3",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "id" : 4,
    "username" : "111111111",
    "description" : "222222222"
  }
}

2.3,拼接更新

$ curl -XPOST 'localhost:9200/ik_v2/chinese/3/_update?pretty' -H "Content-Type: application/json" -d '{
    "script" : {
        "inline" : "ctx._source.description += params.content", //因为description是string型,所以是拼
        "params" : {
            "content" : 333
        }
    }
}'

$ curl -XPOST 'localhost:9200/ik_v2/chinese/3/_update?pretty' -H "Content-Type: application/json" -d '{
    "script" : {
        "inline" : "ctx._source.id += params.num",   //因为id是int型,所以是加
        "params" : {
            "num" : 2
        }
    }
}'

$ curl -XGET "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"
{
  "_index" : "ik_v2",
  "_type" : "chinese",
  "_id" : "3",
  "_version" : 3,
  "found" : true,
  "_source" : {
    "id" : 6,   //加了2
    "username" : "111111111",
    "description" : "222222222333"   //拼了333
  }
}

2.4,添加字段,并更新数据

//添加一个字段为sex值为1
$ curl -XPOST 'localhost:9200/ik_v2/chinese/3/_update?pretty' -H "Content-Type: application/json" -d '{
 "script" : "ctx._source.sex = 1"
}'

//删除sex这个字段
$ curl -XPOST 'localhost:9200/ik_v2/chinese/3/_update?pretty' -H "Content-Type: application/json" -d '{
 "script" : "ctx._source.remove(\"sex\")"
}'

在这里要注意,用这个方法,mapping结构会改变。

注:以上的更新操作都是单条数据更新

2.5,多条数据更新

$ curl -XPOST 'localhost:9200/ik_v2/test/_update_by_query?pretty' -H "Content-Type: application/json" -d '{
>  "query": {
>         "bool": {
>             "should": {
>                 "match": {
>                     "username": "高铁"
>                 }
>             }
>         }
>     },
>     "script" : {
>         "inline" : "ctx._source.username = \"666666\""
>     }
> }'
{
  "took" : 6,
  "timed_out" : false,
  "total" : 3,
  "updated" : 2,  //更新了二条
  "deleted" : 0,
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

注意,这个条件字段,最好不要用分词字段,因为不可控。上面我只是为了测试用。

3,删除数据

3.1,单条删除

$ curl -XDELETE "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"

3.2,多条数据删除

$ curl -XPOST 'http://127.0.0.1:9200/ik_v2/_delete_by_query?pretty' -H "Content-Type: application/json" -d '{
> "query": {
>         "term": {
>             "username": "666666"
>         }
>     }
> }'
{
  "took" : 6,
  "timed_out" : false,
  "total" : 3,
  "deleted" : 2,  //删除了二条
  "batches" : 1,
  "version_conflicts" : 0,
  "noops" : 0,
  "retries" : {
    "bulk" : 0,
    "search" : 0
  },
  "throttled_millis" : 0,
  "requests_per_second" : -1.0,
  "throttled_until_millis" : 0,
  "failures" : [ ]
}

注意,这个条件字段,最好不要用分词字段,因为不可控。

4,查询

$ curl -XPOST "http://127.0.0.1:9200/ik,ik_v2/chinese/_search?pretty"  -H "Content-Type: application/json"  -d '
{
    "query": {
        "multi_match": {
            "query": "中国高铁",
            "fields": [ "username", "description"]
        }
    }
}
'

查询的操作,非常多,后面会单独的详细说。查询总的来说,支持多索引多字段查询。新版es不支持一个索引多个mapping,老版还支持多mapping查询。



转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/server/1916.html