更多查询可以参考
Elasticsearch笔记(九) term terms exists 查询案例
Elasticsearch笔记(十一) ES term terms prefix 搜索 聚合查询 详细总结
Elasticsearch笔记(十二) elasticsearch 桶聚合 Query DSL
GET /_cat/indices?v
#新增一个名为pigg的index
PUT /pigg
#删除pigg这个index,产线千万别这么做,删了就完了
DELETE /pigg
#指定了文档ID=1
PUT /pigg/_doc/1
{
"name": "王冬冬",
"ename": "winter",
"age": 32,
"about": "I am a good coder",
"interest": ["eat", "coding"],
"interest_count": 2
}
#指定了文档ID=2
PUT /pigg/_doc/2
{
"name": "朱大旬",
"ename": "vissy",
"age": 29,
"about": "I am a tester",
"interest": ["eat", "testing"],
"interest_count": 2,
"job": null
}
#指定了文档ID=3
PUT /pigg/_doc/3
{
"name": "王佳冬",
"ename": "micoo",
"age": 3,
"about": "I am a baby",
"interest": ["eat", "play", "sleep"],
"interest_count": 3,
"job": []
}
用post新增文档,ES自动为文档生成20位的ID
POST /pigg/_doc
{
"name": "小波波",
"age": 2,
"about": "I am a cat",
"interest": ["eat"],
"interest_count": 1,
"job": [null, "get mouse"]
}
返回结果如下:
{
"_index" : "pigg",
"_type" : "_doc",
"_id" : "PYiliXABQi7JywF4bswF",//这个就是es自动生成的ID
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
新增文档时,可以同时指定op_type,当es里已经存在相同ID的文档时,就会新增失败。
#==指定操作类型op_type=create
PUT /pigg/_doc/3?op_type=create
{
"name": "王佳彤",
"first_name": "jia tong",
"last_name": "wang",
"age": 3,
"about": "I am a baby",
"interest": ["eat", "play", "sleep"],
"interest_count": 3,
"job": []
}
上面等同于:
#指定是create文档
PUT /pigg/_doc/3/_create
{
"name": "王佳彤",
"first_name": "jia tong",
"last_name": "wang",
"age": 3,
"about": "I am a baby",
"interest": ["eat", "play", "sleep"],
"interest_count": 3,
"job": []
}
返回异常信息提示:
"reason": "[_doc][3]: version conflict, document already exists (current version [1])"
每个文档都有一个版本号version,新增后version=1,以后每次修改,version自动加1,也可以指定version,比如让它从1一下子变成10这样。
#先新增一个ID=100的文档
PUT /pigg/_doc/100
{
"name": "三爷"
}
#返回结果如下,注意这个版本号_version=1
{
"_index" : "pigg",
"_type" : "_doc",
"_id" : "100",
"_version" : 1,//注意这个版本号
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
但我们要修改文档前,我们已经知道当前version=1,如果文档被别人修改过,那version肯定大于1。
当我们要修改时,带上version参数(值是我们认定的更新前,当前文档的version值),如果我们指定的与ES里文档相等,则能成功,否则报异常。
#指定跟新前文档现有version=1
PUT /pigg/_doc/100?version=1
{
"name": "三爷2"
}
#因为中间没有别人操作过id=100的文档,所以修改成功,version变成2
{
"_index" : "pigg",
"_type" : "_doc",
"_id" : "100",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
#指定了version_type=external,必须version>当前的version
PUT /pigg/_doc/100?version=10&version_type=external
{
"name": "三爷3"
}
#返回结果如下,version变成指定的10
{
"_index" : "pigg",
"_type" : "_doc",
"_id" : "100",
"_version" : 10,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 4,
"_primary_term" : 1
}
更多查询可以参考ES Query DSL 词项查询
GET /pigg/_search
GET /pigg/_doc/1?pretty
返回结果:
{
"_index" : "pigg",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "王冬冬",
"ename" : "winter",
"age" : 32,
"about" : "I am a good coder",
"interest" : [
"eat",
"coding"
],
"interest_count" : 2
}
}
#对age进行倒序查询
GET /pigg/_search
{
"query": {"match_all": {}},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
#查询前2条数据, from是从0开始的
GET /pigg/_search
{
"query": {"match_all": {}},
"sort": [
{
"age": {
"order": "desc"
}
}
],
"from": 0,
"size": 2
}
存在返回200,不存在返回404
HEAD /pigg/_doc/1
#不返回_source
GET /pigg/_doc/1?_source=false
#只返回_source
GET /pigg/_doc/1/_source
#只返回ID=1文档的name,age
GET /pigg/_doc/1?_source=name,age
#只返回_source中某些字段
GET /pigg/_search
{
"_source": ["name", "age"]
}
#带上查询条件和_source过滤
GET /pigg/_search
{
"query": {
"bool": {
"filter": {
"term": {
"age": 3
}
}
}
},
"_source": ["name", "age"]
}
#只排除某些字段
GET /pigg/_doc/1?_source_exclude=about,interest
#判断_source是否存在
HEAD pigg/_doc/1/_source
查询当前pigg表里id=1的文档
GET /pigg/_doc/1?pretty
返回如下:
{
"_index": "pigg",
"_type": "_doc",
"_id": "1",
"_version": 4,
"found": true,
"_source": {
"name": "三爷",
"age": 29,
"hometown": "盐城",
"gender": "male"
}
}
用put方式更新id=1的文档
PUT /pigg/_doc/1
{
"name": "盐城三爷"
}
再次查询id=1的文档
{
"_index": "pigg",
"_type": "_doc",
"_id": "1",
"_version": 5,
"found": true,
"_source": {
"name": "盐城三爷"
}
}
通过上面发现用put是替换了整个文档,而不是更新name这一个字段
先恢复id=1的文档为一开始的数据,然后执行如下语句
修改name,并新增interesting这个字段
POST /pigg/_doc/1/_update?pretty
{
"doc":{
"name": "盐城冬冬",
"interesting": "watching TV"
}
}
再次查询id=1的文档
{
"_index": "pigg",
"_type": "_doc",
"_id": "1",
"_version": 8,
"found": true,
"_source": {
"name": "盐城冬冬",
"age": 29,
"hometown": "盐城",
"gender": "male",
"interesting": "watching TV"
}
}
这时发现用post更新的是文档的局部字段,原来有的字段更新,没有的字段则新增这个字段
查询当前id=1的人的age是29,现在要对age加1
POST /pigg/_doc/1/_update
{
"script": "ctx._source.age += 1"
}
再次查询id=1的文档,发现age已经是30了
{
"_index": "pigg",
"_type": "_doc",
"_id": "1",
"_version": 9,
"found": true,
"_source": {
"name": "盐城冬冬",
"age": 30,
"hometown": "盐城",
"gender": "male",
"interesting": "watching TV"
}
}
#age-1
POST /pigg/_doc/1/_update
{
"script": {
"source": "ctx._source.age -= 1"
}
}
#age=30
POST /pigg/_doc/1/_update
{
"script": {
"source": "ctx._source.age = 30"
}
}
#name='witerking'
POST /pigg/_doc/1/_update
{
"script": {
"source": "ctx._source.name = 'witerking'"
}
}
#给数组添加一个值,就算存在也添加,语音用painless
POST /pigg/_doc/1/_update
{
"script": {
"source": "ctx._source.interest.add(params.interest)",
"lang": "painless",
"params": {
"interest": "sleep"
}
}
}
#给数组添加一个值,不存在才添加,语音用painless
POST /pigg/_doc/1/_update
{
"script": {
"source": "if(!ctx._source.interest.contains(params.interest)) {ctx._source.interest.add(params.interest)}",
"lang": "painless",
"params": {
"interest": "sleeping"
}
}
}
#给文档添加一个新字段new_name
POST /pigg/_doc/1/_update
{
"script": {
"source": "ctx._source.new_name = '傻瓜'",
"lang": "painless"
}
}
#字段直接复制
POST /pigg/_doc/1/_update
{
"script": {
"source": "ctx._source.new_name = ctx._source.name",
"lang": "painless"
}
}
#删除一个字段,不修改mapping
POST /pigg/_doc/1/_update
{
"script": "ctx._source.remove('new_name')"
}
DELETE /pigg/_doc/1
#==delete_by_query==
POST /pig/_delete_by_query
{
"query": {
"term": {
"_id": "5"
}
}
}
#忽略版本冲突,继续执行删除操作
POST /pig/_delete_by_query?conflicts=proceed
{
"query": {
"term": {
"_id": "5"
}
}
}
如果本文对您有帮助,就点个赞吧