Elasticsearch 学习笔记 索引创建、插入、查询、修改、删除
1、索引创建
方式一、在Elasticsearch - head的索引界面中新建
方式二、使用POSTMAN工具发送PUT请求创建
- {
- "settings":{
- "number_of_shards": 3,
- "number_of_replicas": 1
- },
- "mappings":{
- "man":{
- "properties":{
- "word_count":{
- "type": "integer"
- },
- "author":{
- "type": "keyword"
- },
- "title":{
- "type": "text"
- },
- "publish_date":{
- "type": "date",
- "format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
- }
- }
- },
- "woman":{
- }
- }
- }
观察返回结果已经成功。
2、插入
2.1 指定ID值的插入
PUT 127.0.0.1:9200/people/man/1
- {
- "name": "chenjie",
- "country": "China",
- "age": 30,
- "date": "1994-09-27"
- }
2.2 不指定ID的插入
3、修改
方式一、POST127.0.0.1:9200/people/man/1/_update
- {
- "doc":{
- "name":"chenjie_update"
- }
- }
方式二:通过内置脚本修改 POST127.0.0.1:9200/people/man/1/_update
- {
- "script":{
- "lang":"painless",
- "inline":"ctx._source.age += 10"
- }
- }
方式3:在内置脚本中使用参数
- {
- "script":{
- "lang":"painless",
- "inline":"ctx._source.age = params.age",
- "params":{
- "age":100
- }
- }
- }
4、删除
DELETE127.0.0.1:9200/people/man/1
5、查询
以一个新的索引为例:
使用PUT127.0.0.1:9200/book 创建BOOK索引,并随便插入几条数据
- {
- "settings":{
- "number_of_shards": 3,
- "number_of_replicas": 1
- },
- "mappings":{
- "novel":{
- "properties":{
- "word_count":{
- "type": "integer"
- },
- "author":{
- "type": "keyword"
- },
- "title":{
- "type": "text"
- },
- "publish_date":{
- "type": "date",
- "format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
- }
- }
- },
- "woman":{
- }
- }
- }
POST 127.0.0.1:9200/book/
- {
- "word_count": "1000",
- "author": "chenjie",
- "title": "Java从入门到放弃",
- "publish_date": "1995-08-19"
- }
5.1 查询指定ID
GET127.0.0.1:9200/book/novel/1
5.2 查询所有的
POST127.0.0.1:9200/book/_search
- 127.0.0.1:9200/book/_search
5.3 查询所有的,指定from和size
POST127.0.0.1:9200/book/_search
- {
- "query":{
- "match_all":{}
- },
- "from":1,
- "size":1
- }
5.4 指定条件查询
- {
- <span style="white-space:pre;"> </span>"query":{
- <span style="white-space:pre;"> </span>"match":{
- <span style="white-space:pre;"> </span>"title":"活着"
- <span style="white-space:pre;"> </span>}
- <span style="white-space:pre;"> </span>},
- <span style="white-space:pre;"> </span>"size":1
- }