Elasticsearch笔记(一) Query DSL入门

更多查询可以参考
Elasticsearch笔记(九) term terms exists 查询案例
Elasticsearch笔记(十一) ES term terms prefix 搜索 聚合查询 详细总结
Elasticsearch笔记(十二) elasticsearch 桶聚合 Query DSL

1. index

1.1 查询所有index

GET /_cat/indices?v

1.2 新增index

#新增一个名为pigg的index
PUT /pigg

1.3 删除index

#删除pigg这个index,产线千万别这么做,删了就完了
DELETE /pigg

2. document

2.1 新增document

2.1.1 新增时指定ID

#指定了文档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": []
}

2.1.2 由ES自动生成ID

用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
}

2.1.3 指定操作类型参数:op_type

新增文档时,可以同时指定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])"

2.1.4 指定文档版本号version

每个文档都有一个版本号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
}

2.1.5 指定了version_type=external

#指定了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
}

2.2 查询document

更多查询可以参考ES Query DSL 词项查询

2.2.1 查询index的所有document

GET /pigg/_search

2.2.1 根据id查询document

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
  }
}

2.2.2 用sort排序查询

#对age进行倒序查询
GET /pigg/_search
{
  "query": {"match_all": {}},
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

2.2.3 用from和size分页查询

#查询前2条数据, from是从0开始的
GET /pigg/_search
{
  "query": {"match_all": {}},
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 2
}

2.2.4 根据ID判断是否存在

存在返回200,不存在返回404

HEAD /pigg/_doc/1

2.2.5 用_source参数过滤返回的字段

#不返回_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

2.3 修改document

2.3.1 用put替换document

查询当前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这一个字段

2.3.2 用post更新document

先恢复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更新的是文档的局部字段,原来有的字段更新,没有的字段则新增这个字段

2.3.3 用script更新document

查询当前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"
  }
}

2.3.4 用script更新document其他例子

#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')"
}

2.4 删除document

2.4.1 根据ID删除

DELETE /pigg/_doc/1

2.4.2 delete_by_query

#==delete_by_query==
POST /pig/_delete_by_query
{
  "query": {
    "term": {
      "_id": "5"
    }
  }
}

#忽略版本冲突,继续执行删除操作
POST /pig/_delete_by_query?conflicts=proceed
{
  "query": {
    "term": {
      "_id": "5"
    }
  }
}

如果本文对您有帮助,就点个赞吧

你可能感兴趣的:(Elasticsearch)